Skip to content

Commit

Permalink
Backport PR #424: 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 e8be710 commit 1da3c48
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 @@ -2,6 +2,14 @@
FlexMeasures Changelog
**********************

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 @@ -149,6 +149,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 @@ -75,9 +75,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 1da3c48

Please sign in to comment.