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

UnitRegistry(system="imperial") fetches the wrong volume measures #2048

Open
nickovs opened this issue Aug 21, 2024 · 0 comments
Open

UnitRegistry(system="imperial") fetches the wrong volume measures #2048

nickovs opened this issue Aug 21, 2024 · 0 comments

Comments

@nickovs
Copy link

nickovs commented Aug 21, 2024

When fetching imperial gallons via a registry created just as reg1 = UnitRegistry() using reg1.sys.imperial.gallon it returns the correct unit. When fetching the same unit using reg2 = UnitRegistry(system="imperial") and then referencing reg2.gallon the wrong (US) unit is returned.

For example, this works:

import pint
print(pint.__version__)

plain_reg = pint.UnitRegistry()

print("Default system: ", plain_reg.default_system)
imp_pint = 1 * plain_reg.sys.imperial.pint
us_pint = 1 * plain_reg.sys.US.pint
print("Pints via plain registry:", imp_pint.to("litre").m, imp_pint.to("litre").m / us_pint.to("litre").m)

imp_gallon = 1 * plain_reg.sys.imperial.gallon
us_gallon = 1 * plain_reg.sys.US.gallon
print("Gallons via plain registry:", imp_gallon.to("litre").m, imp_gallon.to("litre").m / us_gallon.to("litre").m)

It gives the expected:

0.24.3
Default system:  mks
Pints via plain registry: 0.5682612500000002 1.2009499255048557
Gallons via plain registry: 4.546090000000001 1.2009499255048557

This does not work correctly:

import pint
print(pint.__version__)

imperial_reg = pint.UnitRegistry(system="imperial")
us_reg = pint.UnitRegistry(system="US")
print("Imperial system: ", imperial_reg.default_system)
print("US system: ", us_reg.default_system)

imp_reg_pint = 1 * imperial_reg.pint
us_reg_pint = 1 * us_reg.pint
print("Pints via system registries:", imp_reg_pint.to("litre").m, imp_reg_pint.to("litre").m / us_reg_pint.to("litre").m)

imp_reg_gallon = 1 * imperial_reg.gallon
us_reg_gallon = 1 * us_reg.gallon
print("Gallons via system registries:", imp_reg_gallon.to("litre").m, imp_reg_gallon.to("litre").m / us_reg_gallon.to("litre").m)

The result is:

0.24.3
Imperial system:  imperial
US system:  US
Pints via system registries: 0.4731764729999999 1.0
Gallons via system registries: 3.785411783999999 1.0

In the later case, even though a system was specified when creating the registry and is shown as being the default, it is not being used when fetching the volume measures. Instead it is fetching the US volume measurements.

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