File tree Expand file tree Collapse file tree 2 files changed +7
-14
lines changed Expand file tree Collapse file tree 2 files changed +7
-14
lines changed Original file line number Diff line number Diff line change 4
4
5
5
import bisect
6
6
import math
7
- import numbers
8
- import warnings
9
7
10
8
from ...util import infer_base_unit
11
9
from ...compat import (
@@ -100,18 +98,8 @@ def to_compact(
100
98
<Quantity(10.0, 'millinewton')>
101
99
"""
102
100
103
- if not isinstance (quantity .magnitude , numbers .Number ):
104
- msg = "to_compact applied to non numerical types " "has an undefined behavior."
105
- w = RuntimeWarning (msg )
106
- warnings .warn (w , stacklevel = 2 )
107
- return quantity
108
101
109
- if (
110
- quantity .unitless
111
- or quantity .magnitude == 0
112
- or math .isnan (quantity .magnitude )
113
- or math .isinf (quantity .magnitude )
114
- ):
102
+ if quantity .unitless :
115
103
return quantity
116
104
117
105
SI_prefixes : dict [int , str ] = {}
@@ -137,6 +125,11 @@ def to_compact(
137
125
q_base = quantity .to (unit )
138
126
139
127
magnitude = q_base .magnitude
128
+ # Support uncertainties
129
+ if hasattr (magnitude , 'nominal_value' ):
130
+ magnitude = magnitude .nominal_value
131
+ if magnitude == 0 or math .isnan (magnitude ) or math .isinf (magnitude ):
132
+ return quantity
140
133
141
134
units = list (q_base ._units .items ())
142
135
units_numerator = [a for a in units if a [1 ] > 0 ]
Original file line number Diff line number Diff line change @@ -831,7 +831,7 @@ def test_limits_magnitudes(self):
831
831
def test_nonnumeric_magnitudes (self ):
832
832
ureg = self .ureg
833
833
x = "some string" * ureg .m
834
- with pytest .warns ( RuntimeWarning ):
834
+ with pytest .raises ( TypeError ):
835
835
self .compare_quantity_compact (x , x )
836
836
837
837
def test_very_large_to_compact (self ):
You can’t perform that action at this time.
0 commit comments