Skip to content

Commit

Permalink
to_compact: support uncertainties' Magnitudes , keeping warning
Browse files Browse the repository at this point in the history
Closing #584, #1910, #1911
  • Loading branch information
andrewgsavage authored Jan 3, 2024
1 parent 6c3716f commit 0f24b6f
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pint/facets/plain/qto.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ def to_compact(
<Quantity(10.0, 'millinewton')>
"""

if not isinstance(quantity.magnitude, numbers.Number):
if not isinstance(quantity.magnitude, numbers.Number) and not hasattr(
quantity.magnitude, "nominal_value"
):
msg = "to_compact applied to non numerical types " "has an undefined behavior."
w = RuntimeWarning(msg)
warnings.warn(w, stacklevel=2)
Expand Down Expand Up @@ -137,6 +139,9 @@ def to_compact(
q_base = quantity.to(unit)

magnitude = q_base.magnitude
# Support uncertainties
if hasattr(magnitude, "nominal_value"):
magnitude = magnitude.nominal_value

units = list(q_base._units.items())
units_numerator = [a for a in units if a[1] > 0]
Expand Down

0 comments on commit 0f24b6f

Please sign in to comment.