diff --git a/pint/facets/plain/qto.py b/pint/facets/plain/qto.py index 9cd8a780a..8a38032f7 100644 --- a/pint/facets/plain/qto.py +++ b/pint/facets/plain/qto.py @@ -4,8 +4,6 @@ import bisect import math -import numbers -import warnings from ...util import infer_base_unit from ...compat import ( @@ -100,18 +98,8 @@ def to_compact( """ - if not isinstance(quantity.magnitude, numbers.Number): - msg = "to_compact applied to non numerical types " "has an undefined behavior." - w = RuntimeWarning(msg) - warnings.warn(w, stacklevel=2) - return quantity - if ( - quantity.unitless - or quantity.magnitude == 0 - or math.isnan(quantity.magnitude) - or math.isinf(quantity.magnitude) - ): + if quantity.unitless: return quantity SI_prefixes: dict[int, str] = {} @@ -137,6 +125,11 @@ def to_compact( q_base = quantity.to(unit) magnitude = q_base.magnitude + # Support uncertainties + if hasattr(magnitude, 'nominal_value'): + magnitude = magnitude.nominal_value + if magnitude == 0 or math.isnan(magnitude) or math.isinf(magnitude): + return quantity units = list(q_base._units.items()) units_numerator = [a for a in units if a[1] > 0] diff --git a/pint/testsuite/test_quantity.py b/pint/testsuite/test_quantity.py index f13aaf868..d35c44bbd 100644 --- a/pint/testsuite/test_quantity.py +++ b/pint/testsuite/test_quantity.py @@ -831,7 +831,7 @@ def test_limits_magnitudes(self): def test_nonnumeric_magnitudes(self): ureg = self.ureg x = "some string" * ureg.m - with pytest.warns(RuntimeWarning): + with pytest.raises(TypeError): self.compare_quantity_compact(x, x) def test_very_large_to_compact(self):