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

Fix converting to offset units of higher dimension e.g. gauge pressure #1952

Merged
merged 4 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Pint Changelog
(PR #1926, fixes Issue #1841)
- Fix LaTeX siuntix formatting when using non_int_type=decimal.Decimal.

- Fix converting to offset units of higher dimension e.g. gauge pressure (#1949).
-

0.23 (2023-12-08)
-----------------
Expand Down
7 changes: 6 additions & 1 deletion pint/facets/nonmultiplicative/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def _add_ref_of_log_or_offset_unit(
self, offset_unit: str, all_units: UnitsContainer
) -> UnitsContainer:
slct_unit = self._units[offset_unit]
if slct_unit.is_logarithmic or (not slct_unit.is_multiplicative):
if slct_unit.is_logarithmic:
# Extract reference unit
slct_ref = slct_unit.reference

Expand All @@ -204,6 +204,11 @@ def _add_ref_of_log_or_offset_unit(
(u, e) = [(u, e) for u, e in slct_ref.items()].pop()
# Add it back to the unit list
return all_units.add(u, e)

if not slct_unit.is_multiplicative: # is offset unit
# Extract reference unit
return slct_unit.reference

# Otherwise, return the units unmodified
return all_units

Expand Down
12 changes: 11 additions & 1 deletion pint/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ def test_issue1725(registry_empty):
assert registry_empty.get_compatible_units("dollar") == set()


def test_issues_1505():
def test_issue1505():
ur = UnitRegistry(non_int_type=decimal.Decimal)

assert isinstance(ur.Quantity("1m/s").magnitude, decimal.Decimal)
Expand Down Expand Up @@ -1203,6 +1203,16 @@ def test_issues_1841_xfail():
# print(q)


def test_issue1949(registry_empty):
ureg = UnitRegistry()
ureg.define(
"in_Hg_gauge = 3386389 * gram / metre / second ** 2; offset:101325000 = inHg_g = in_Hg_g = inHg_gauge"
)
q = ureg.Quantity("1 atm").to("inHg_gauge")
assert q.units == ureg.in_Hg_gauge
assert_equal(q.magnitude, 0.0)


@pytest.mark.parametrize(
"given,expected",
[
Expand Down
Loading