Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError when using Quantity.check("dimensionless") #1994

Open
twiese-gravitics opened this issue May 23, 2024 · 0 comments
Open

KeyError when using Quantity.check("dimensionless") #1994

twiese-gravitics opened this issue May 23, 2024 · 0 comments

Comments

@twiese-gravitics
Copy link

twiese-gravitics commented May 23, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant