Skip to content

Commit

Permalink
Except all errors when checking for valid unit (#424)
Browse files Browse the repository at this point in the history
* Add test case that lets pint throw an AttributeError

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Except all errors when checking for unit validity using pint

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Changelog entry

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Add inline note

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Update patch release date

Signed-off-by: F.N. Claessen <felix@seita.nl>
  • Loading branch information
Flix6x committed Apr 28, 2022
1 parent 4cb6359 commit db84393
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
8 changes: 8 additions & 0 deletions documentation/changelog.rst
Expand Up @@ -22,6 +22,14 @@ Infrastructure / Support
* Allow to filter data by source using a tuple instead of a list [see `PR #421 <http://www.github.com/FlexMeasures/flexmeasures/pull/421>`_]


v0.9.4 | April 28, 2022
===========================

Bugfixes
--------
* Support checking validity of custom units (i.e. non-SI, non-currency units) [see `PR #424 <http://www.github.com/FlexMeasures/flexmeasures/pull/424>`_]


v0.9.3 | April 15, 2022
===========================

Expand Down
1 change: 1 addition & 0 deletions flexmeasures/utils/tests/test_unit_utils.py
Expand Up @@ -151,6 +151,7 @@ def test_units_are_convertible():
("°C", False),
("", False),
("not-a-unit", False),
("#", False),
],
)
def test_is_power_unit(unit: str, power_unit: bool):
Expand Down
6 changes: 3 additions & 3 deletions flexmeasures/utils/unit_utils.py
Expand Up @@ -78,9 +78,9 @@ def is_valid_unit(unit: str) -> bool:
"""Return True if the pint library can work with this unit identifier."""
try:
ur.Quantity(unit)
except ValueError:
return False
except pint.errors.UndefinedUnitError:
except Exception: # noqa B902
# in practice, we encountered pint.errors.UndefinedUnitError, ValueError and AttributeError,
# but since there may be more, here we simply catch them all
return False
return True

Expand Down

0 comments on commit db84393

Please sign in to comment.