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

Find out why pint doesn't allow registry mixing #112

Open
seanpearsonuk opened this issue Sep 25, 2023 · 5 comments
Open

Find out why pint doesn't allow registry mixing #112

seanpearsonuk opened this issue Sep 25, 2023 · 5 comments
Assignees

Comments

@seanpearsonuk
Copy link
Collaborator

We can ask pint in github

We think it might be to enforce consistent mixing of units (e.g. GB vs US gallons)

@seanpearsonuk seanpearsonuk self-assigned this Sep 25, 2023
@hpohekar
Copy link
Collaborator

hpohekar commented Oct 5, 2023

@seanpearsonuk @dnwillia @ansaboutin

ValueError: Cannot operate with Unit and Unit of different registries.

------------------------------------------------------

image

------------------------------------------------------

Sharing response given by hgrecco, he has written Pint from scratch.

image

hgrecco/pint#1692

@dnwillia-work
Copy link
Collaborator

dnwillia-work commented Oct 5, 2023

So reading that it does allow some comparisons but it’s not considering their units equivalent for some reason. It’s still weird, and their answers don’t really help me.

@hpohekar
Copy link
Collaborator

hpohekar commented Oct 6, 2023

@seanpearsonuk @dnwillia-work

image

----------------------------------------------------

Here, one approach is mentioned using __init__.py changes and another is using set_application_registry().

image

https://pint.readthedocs.io/en/0.7.2/tutorial.html#using-pint-in-your-projects

hgrecco/pint#445

@dnwillia-work
Copy link
Collaborator

dnwillia-work commented Oct 6, 2023

Right, so you can instantiate multiple registries, but they isolate the units to a registry and explicitly disallow combining them perhaps because of this design decision: "There are no global units in Pint". The behaviour does seem to be as in your screenshots (below) but I'm still not sure why they have imposed this limitation other than just establishing it as a convention (i.e. this is just a rule we have and it's the way it is).

>>> from pint import UnitRegistry
>>> q1 = UnitRegistry().meter
>>> q2 = UnitRegistry().meter
>>> q1 + q1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'Unit' and 'Unit'
>>> q1
<Unit('meter')>
>>> q2
<Unit('meter')>
>>> q1 == q2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/dnwillia/Developer/CubicEosLib/venv/lib/python3.11/site-packages/pint/facets/plain/unit.py", line 188, in __eq__
    if self._check(other):
       ^^^^^^^^^^^^^^^^^^
  File "/Users/dnwillia/Developer/CubicEosLib/venv/lib/python3.11/site-packages/pint/util.py", line 987, in _check
    raise ValueError(
ValueError: Cannot operate with Unit and Unit of different registries.
>>> q3 = q1 + q2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'Unit' and 'Unit'

@hpohekar
Copy link
Collaborator

hpohekar commented Oct 6, 2023

Yes, right.

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

3 participants