pint
pint copied to clipboard
KeyError when using Quantity.check("dimensionless")
When checking dimensionality, it is occasionally useful to check if a particular Quantity is dimensionless. Although using "dimensionless" to create a Quantity works without issue, attempting to use Quantity.check("dimensionless") or similar syntax unexpectedly raises a KeyError, as seen below.
from pint import Quantity
a = Quantity(5,"dimensionless")
print(f"{a = }, {a.dimensionality = }, {a.units = }")
test_strings = ["","[]","dimensionless","[dimensionless]","[None]", None]
for test_string in test_strings:
try:
print(f"a.check({test_string.__repr__()}) = {a.check(test_string)}")
except KeyError as e:
print(f"a.check({test_string.__repr__()}) failed (KeyError: {(e)})")
output:
a = <Quantity(5, 'dimensionless')>, a.dimensionality = <UnitsContainer({})>, a.units = <Unit('dimensionless')>
a.check('') = True
a.check('[]') = True
a.check('dimensionless') failed (KeyError: '')
a.check('[dimensionless]') failed (KeyError: '[dimensionless]')
a.check('[None]') failed (KeyError: '[None]')
a.check(None) = True
I'm not sure about [None], but "dimensionless" and "[dimensionless" should both probably work.
+1