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

format dimensionless #2012

Merged
merged 12 commits into from
Jun 21, 2024
Prev Previous commit
Next Next commit
format
  • Loading branch information
andrewgsavage committed Jun 21, 2024
commit 9f8ba4f0c035672e76314499cef1d1529f25aab3
8 changes: 7 additions & 1 deletion pint/delegates/formatter/_compound_unit_helpers.py
Original file line number Diff line number Diff line change
@@ -253,9 +253,15 @@ def prepare_compount_unit(
out = unit._units.items()
else:
out = 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(
20 changes: 13 additions & 7 deletions pint/testsuite/test_issues.py
Original file line number Diff line number Diff line change
@@ -1260,10 +1260,16 @@ def test_issue1772(given, 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\\ dimensionless'
assert f"{q:Lx}" == '\\SI[]{1}{}'
assert f"{q:~P}" == '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"