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

Number comparison with quantity #2142

Conversation

simutisernestas
Copy link

  • Executed pre-commit run --all-files with no errors
  • The change is fully covered by automated unit tests
  • Documented in docs/ as appropriate
  • Added an entry to the CHANGES file

Haven't added tests yet, just a proposal enabling:

import pint

d = pint.Quantity(5, "m")
print(d > 6)
d = pint.Quantity(10, "m")
print(d > 6)

For now, pint gives an error:

ValueError: Cannot compare PlainQuantity and <class 'int'>

This is something of interest within the integration of pydantic and pint

@simutisernestas simutisernestas changed the title number comparison with quantity] Number comparison with quantity Feb 19, 2025
@dopplershift
Copy link
Contributor

I wouldn't expect this to ever "just work" because it violates the unit safety. The problem here is that e.g. d > 6 doesn't specify what the units of 6 are, so there's no way to know what the correct answer is. If you just want to compare the magnitudes (again, completely bypassing pint's unit safety), you can do d.m > 6.

@simutisernestas
Copy link
Author

True. I was more thinking of simple validation checks done by pydantic against plain numbers, for instance, making sure physical quantity is positive. In that case it requires some boilerplate to make this work rather than simply going through with the comparison. But in the majority of cases, the unit safety is going to be more important.

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

Successfully merging this pull request may close these issues.

2 participants