From 8d0439a8f1e70d84c68bfbfd1f1f9c7e47b3510c Mon Sep 17 00:00:00 2001 From: andrewgsavage Date: Tue, 11 Jun 2024 22:41:55 +0100 Subject: [PATCH] dimensionless --- pint/delegates/formatter/_compound_unit_helpers.py | 4 +++- pint/delegates/formatter/_format_helpers.py | 4 +++- pint/testsuite/test_issues.py | 5 +++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pint/delegates/formatter/_compound_unit_helpers.py b/pint/delegates/formatter/_compound_unit_helpers.py index 89bda87a2..2bef3faa8 100644 --- a/pint/delegates/formatter/_compound_unit_helpers.py +++ b/pint/delegates/formatter/_compound_unit_helpers.py @@ -254,7 +254,9 @@ def prepare_compount_unit( out = unit._units.items() else: out = unit - + print(out) + if len(out) == 0 and not "~" in spec: + out = {"dimensionless": 1}.items() # out: unit_name, unit_exponent if "~" in spec: diff --git a/pint/delegates/formatter/_format_helpers.py b/pint/delegates/formatter/_format_helpers.py index 95fad1758..8a2f37a59 100644 --- a/pint/delegates/formatter/_format_helpers.py +++ b/pint/delegates/formatter/_format_helpers.py @@ -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) @@ -211,7 +213,7 @@ def formatter( neg_terms.append(power_fmt.format(key, fun(value))) if not pos_terms and not neg_terms: - return "dimensionless" + return "" if not as_ratio: # Show as Product: positive * negative terms ** -1 diff --git a/pint/testsuite/test_issues.py b/pint/testsuite/test_issues.py index 9a453685e..eb286d6c7 100644 --- a/pint/testsuite/test_issues.py +++ b/pint/testsuite/test_issues.py @@ -1264,5 +1264,6 @@ def test_issue2007(): 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}{}' \ No newline at end of file + # assert f"{q:L}" == '1\\ dimensionless' + assert f"{q:Lx}" == '\\SI[]{1}{}' + assert f"{q:~P}" == '1' \ No newline at end of file