Skip to content

Commit

Permalink
fix default format dimensionless (#2012)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgsavage committed Jun 21, 2024
1 parent 4c25f32 commit 2493241
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Pint Changelog

- Fix custom formatter needing the registry object. (PR #2011)
- Support python 3.9 following difficulties installing with NumPy 2. (PR #2019)
- Fix default formatting of dimensionless unit issue. (PR #2012)

0.24 (2024-06-07)
-----------------
Expand Down
6 changes: 6 additions & 0 deletions pint/delegates/formatter/_compound_unit_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ def prepare_compount_unit(

# out: unit_name, unit_exponent

if len(out) == 0:
if "~" in spec:
return ([], [])
else:
return ([("dimensionless", 1)], [])

if "~" in spec:
if registry is None:
raise ValueError(
Expand Down
2 changes: 2 additions & 0 deletions pint/delegates/formatter/_format_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ def join_mu(joint_fstring: str, mstr: str, ustr: str) -> str:
This avoids that `3 and `1 / m` becomes `3 1 / m`
"""
if ustr == "":
return mstr
if ustr.startswith("1 / "):
return joint_fstring.format(mstr, ustr[2:])
return joint_fstring.format(mstr, ustr)
Expand Down
18 changes: 18 additions & 0 deletions pint/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -1255,3 +1255,21 @@ def test_issue1949(registry_empty):
def test_issue1772(given, expected):
ureg = UnitRegistry(non_int_type=decimal.Decimal)
assert f"{ureg(given):Lx}" == expected


def test_issue2007():
ureg = UnitRegistry()
q = ureg.Quantity(1, "")
assert f"{q:P}" == "1 dimensionless"
assert f"{q:C}" == "1 dimensionless"
assert f"{q:D}" == "1 dimensionless"
assert f"{q:H}" == "1 dimensionless"

assert f"{q:L}" == "1\\ \\mathrm{dimensionless}"
# L returned '1\\ dimensionless' in pint 0.23

assert f"{q:Lx}" == "\\SI[]{1}{}"
assert f"{q:~P}" == "1"
assert f"{q:~C}" == "1"
assert f"{q:~D}" == "1"
assert f"{q:~H}" == "1"

0 comments on commit 2493241

Please sign in to comment.