Skip to content

Commit

Permalink
Repair silent truncation of config override
Browse files Browse the repository at this point in the history
Previously when apply_user_config was called with a float value
in user_config to be applied to a key with an int value in config,
the float value would be coerced to int potentially resulting in
truncation.

For the special case of a new float value for a config key with an int
value, the float value is now assigned directly to the config key so
no truncation occurs.
  • Loading branch information
Brad Lincoln authored and jpgrayson committed May 25, 2018
1 parent 1b59fb8 commit c6a93ff
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion desmod/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ def apply_user_config(config, user_config):
except KeyError:
raise ConfigError('Invalid config key: {}'.format(key))
current_type = type(current_value)
if not isinstance(value, current_type):
if not (isinstance(value, current_type) or
# allow new float value to replace integer default
# without truncation from int to float coercion
(isinstance(value, float) and issubclass(current_type, int))):
try:
value = current_type(value)
except (ValueError, TypeError):
Expand Down

0 comments on commit c6a93ff

Please sign in to comment.