summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorConstantine Evans <const@costi.eu>2022-10-08 21:16:27 +0100
committerConstantine Evans <const@costi.eu>2022-10-09 15:45:35 +0100
commit09fe089733a18bab1f827caad737b5c74f274dac (patch)
tree1b5e3595de0d7d7055eb06d5fc618ddefe7cd848
parentf2fc47994b104a39409f573278598420bc325124 (diff)
downloadpint-09fe089733a18bab1f827caad737b5c74f274dac.tar.gz
String NaN creation with correct non_int_type
Creating NaN-value quantities should take into account the non_int_type, and create a magnitude of the correct type. Not all common non_int_type choices support NaN (eg, Fraction does not); this will likely result in a ValueError in that case.
-rw-r--r--CHANGES1
-rw-r--r--pint/facets/plain/registry.py4
-rw-r--r--pint/testsuite/test_non_int.py24
-rw-r--r--pint/util.py2
4 files changed, 28 insertions, 3 deletions
diff --git a/CHANGES b/CHANGES
index f3a85a3..6cccf06 100644
--- a/CHANGES
+++ b/CHANGES
@@ -14,6 +14,7 @@ Pint Changelog
- Fix a recursion error that would be raised when passing quantities to `cond` and `x`.
(Issue #1510, #1530)
- Update test_non_int tests for pytest.
+- Create NaN-value quantities of appropriate non-int-type (Issue #1570).
0.19.2 (2022-04-23)
-------------------
diff --git a/pint/facets/plain/registry.py b/pint/facets/plain/registry.py
index 8572fec..4c62ad1 100644
--- a/pint/facets/plain/registry.py
+++ b/pint/facets/plain/registry.py
@@ -1226,9 +1226,9 @@ class PlainRegistry(metaclass=RegistryMeta):
if token_text == "dimensionless":
return 1 * self.dimensionless
elif token_text.lower() in ("inf", "infinity"):
- return float("inf")
+ return self.non_int_type("inf")
elif token_text.lower() == "nan":
- return float("nan")
+ return self.non_int_type("nan")
elif token_text in values:
return self.Quantity(values[token_text])
else:
diff --git a/pint/testsuite/test_non_int.py b/pint/testsuite/test_non_int.py
index 5ca6c52..f616622 100644
--- a/pint/testsuite/test_non_int.py
+++ b/pint/testsuite/test_non_int.py
@@ -77,6 +77,27 @@ class _TestBasic(NonIntTypeTestCase):
== "Creating new PlainQuantity using a non unity PlainQuantity as units."
)
+ def test_nan_creation(self):
+ if self.SUPPORTS_NAN:
+ value = self.kwargs["non_int_type"]("nan")
+
+ for args in (
+ (value, "meter"),
+ (value, UnitsContainer(meter=1)),
+ (value, self.ureg.meter),
+ ("NaN*meter",),
+ ("nan/meter**(-1)",),
+ (self.Q_(value, "meter"),),
+ ):
+ x = self.Q_(*args)
+ assert math.isnan(x.magnitude)
+ assert type(x.magnitude) == self.kwargs["non_int_type"]
+ assert x.units == self.ureg.UnitsContainer(meter=1)
+
+ else:
+ with pytest.raises(ValueError):
+ self.Q_("NaN meters")
+
def test_quantity_comparison(self):
x = self.QP_("4.2", "meter")
y = self.QP_("4.2", "meter")
@@ -1137,6 +1158,7 @@ class _TestOffsetUnitMath(NonIntTypeTestCase):
class TestNonIntTypeQuantityFloat(_TestBasic):
kwargs = dict(non_int_type=float)
+ SUPPORTS_NAN = True
class TestNonIntTypeQuantityBasicMathFloat(_TestQuantityBasicMath):
@@ -1152,6 +1174,7 @@ class TestNonIntTypeOffsetUnitMathFloat(_TestOffsetUnitMath):
class TestNonIntTypeQuantityDecimal(_TestBasic):
kwargs = dict(non_int_type=Decimal)
+ SUPPORTS_NAN = True
class TestNonIntTypeQuantityBasicMathDecimal(_TestQuantityBasicMath):
@@ -1167,6 +1190,7 @@ class TestNonIntTypeOffsetUnitMathDecimal(_TestOffsetUnitMath):
class TestNonIntTypeQuantityFraction(_TestBasic):
kwargs = dict(non_int_type=Fraction)
+ SUPPORTS_NAN = False
class TestNonIntTypeQuantityBasicMathFraction(_TestQuantityBasicMath):
diff --git a/pint/util.py b/pint/util.py
index 54a7755..0152e66 100644
--- a/pint/util.py
+++ b/pint/util.py
@@ -641,7 +641,7 @@ class ParserHelper(UnitsContainer):
for k in list(ret):
if k.lower() == "nan":
del ret._d[k]
- ret.scale = math.nan
+ ret.scale = non_int_type(math.nan)
return ret