AIFDR/risk_in_a_box — Issue #106: Don't use assert to raise exceptions
Author: timlinux
Created: 2012-02-26 06:30:23+00:00
State: open

Using assert to raise exceptions in not test code is bad. Its bad because if python is run in optimised mode e.g. python -O, these lines are ignored and the program logic will no longer work as expected. On some platforms the use of python optimised code is mandated and we are likely to get hard to investigate bug reports from end users at some unspecified point in the future.

There are not many occurrences of assert in the source code and in my opinion it would be worth replacing these with raised exceptions. Here is an example of assert occurrence in the impact_functions package (ignoring tests).

core.py: assert name in plugins_dict, msg
earthquake/allen_fatality_model.py: assert gender_ratio_unit in ['percent', 'ratio'], msg
earthquake/allen_fatality_model.py: assert population is not None, msg
flood/flood_population_fatality.py: assert gender_ratio_unit in ['percent', 'ratio'], msg
flood/flood_population_fatality.py: assert population is not None, msg
flood/flood_population_evacuation.py: assert gender_ratio_unit in ['percent', 'ratio'], msg
flood/flood_population_evacuation.py: assert population is not None, msg
flood/flood_population_impact.py: assert gender_ratio_unit in ['percent', 'ratio'], msg
flood/flood_population_impact.py: assert population is not None, msg
mappings.py: assert attribute in actual, msg
mappings.py: assert numpy.allclose(attributes[i]['TestBLDGCl'],
mappings.py: assert attribute in actual, msg
mappings.py: assert numpy.allclose(attributes[i]['TestBLDGCl'],
mappings.py: assert attribute in actual, msg
mappings.py: assert attribute in actual, msg
mappings.py:# # assert attribute in actual, msg