refactor: replace non‑test assert calls with explicit ValueError checks (Closes #28) #113
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR addresses issue #28 by systematically replacing
assertstatements with explicit exception raising (primarilyValueError) in non-test production code within thesmart_controldirectory.assertstatements can be optimized away in Python when run with the-Oflag, leading to silent failures. Replacing them with explicitraisestatements ensures that critical preconditions and validation checks are always enforced, improving the robustness and reliability of the application.Key Changes and Files Modified:
smart_control/environment/environment.py:assertstatements withValueErrorchecks in_get_observation_spec_histogram_reducerand_commit_reward_metricsto ensure proper configuration of internal components.smart_control/reward/natural_gas_energy_cost.py:assertwithValueErrorin__init__to validate thatgas_price_by_monthhas exactly 12 values.smart_control/reward/setpoint_energy_carbon_regret.py:assertwithValueErrorin__init__to ensuremax_productivity_personhour_usdis greater thanmin_productivity_personhour_usd.smart_control/simulator/boiler.py:assertwithValueErrorincompute_thermal_dissipation_rateto validate thatwater_tempis not less thanoutside_temp, including values in the error message for debugging.smart_control/simulator/randomized_arrival_departure_occupancy.py:assertwithValueErrorinZoneOccupant.__init__and_get_event_probabilityto ensure chronological order and validity of time bounds, including values in error messages.smart_control/simulator/simulator.py:assertstatements withValueErrorchecks in_get_corner_cv_temp_estimate,_get_edge_cv_temp_estimate, and_get_interior_cv_temp_estimateto validate the number of neighbors for different Control Volume types, including neighbor lists for debugging.smart_control/simulator/stochastic_occupancy.py:assertwithValueErrorinZoneOccupant.__init__to validate chronological order of time bounds and the lunch break hours.smart_control/simulator/vav.py:assertwithValueErrorinmax_air_flow_ratesetter.asserts with a single, combinedValueErrorcheck incompute_zone_supply_tempto ensure the calculatedair_flow_rateis positive, preventing division by zero.smart_control/utils/controller_reader.py:assertwithValueErrorinProtoReader._select_shards._read_timestamp.IndexErrorif the timestamp regex finds no matches in the filepath, raising a more descriptiveValueError.smart_control/utils/conversion_utils.py:asserts withValueErrorinnormalize_dowandnormalize_hodto validate input ranges.zone_id_to_coordinates: Replaced the original regex with a stricter, anchored regex (^...$) and improved the error message to clearly state the expected format and the received invalid input ({zone_id!r}).smart_control/utils/energy_utils.py:asserts withValueErrorfor sequence length validation inget_humidity_ratioandget_air_conditioning_energy_rate.forloops withValueErrorchecks inget_humidity_ratioandget_air_conditioning_energy_rateto validate the values ofrelative_humidities(must be(0,1]) andpressures(must be>0), preventing physical impossibilities and potentialZeroDivisionErrororMathDomainErrordownstream.get_fan_powerto correctly handlebrake_hpbeing0by changingif brake_hp:toif brake_hp is not None:.*_test.pyfiles): Confirmed thatsmart_control/observers/replay_buffer/replay_buffer_test.pyandsmart_control/utils/observation_normalizer_test.pydid not require changes, asassertstatements are appropriate and expected within test suites.This comprehensive set of changes ensures that the codebase provides more informative error messages and is more robust against invalid inputs and internal inconsistencies.
Closes #28