From 1e05f7a228e6323172d747b1e7559bb567792490 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 21 Jun 2024 15:52:52 +0100 Subject: [PATCH 1/3] test issue --- pint/delegates/formatter/full.py | 8 +++++--- pint/testsuite/test_issues.py | 32 +++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/pint/delegates/formatter/full.py b/pint/delegates/formatter/full.py index adc6f6c83..d5de43326 100644 --- a/pint/delegates/formatter/full.py +++ b/pint/delegates/formatter/full.py @@ -102,9 +102,11 @@ def get_formatter(self, spec: str): if k in spec: return v - try: - orphan_fmt = REGISTERED_FORMATTERS[spec] - except KeyError: + for k, v in REGISTERED_FORMATTERS.items(): + if k in spec: + orphan_fmt = REGISTERED_FORMATTERS[k] + break + else: return self._formatters["D"] try: diff --git a/pint/testsuite/test_issues.py b/pint/testsuite/test_issues.py index 2a0b7edf6..44caafbca 100644 --- a/pint/testsuite/test_issues.py +++ b/pint/testsuite/test_issues.py @@ -7,7 +7,12 @@ import pytest -from pint import Context, DimensionalityError, UnitRegistry, get_application_registry +from pint import ( + Context, + DimensionalityError, + UnitRegistry, + get_application_registry, +) from pint.compat import np from pint.delegates.formatter._compound_unit_helpers import sort_by_dimensionality from pint.facets.plain.unit import UnitsContainer @@ -1255,3 +1260,28 @@ 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_issue2017(): + ureg = UnitRegistry() + + from pint import formatting as fmt + + @fmt.register_unit_format("test") + def _test_format(unit, registry, **options): + print("format called") + proc = {u.replace("ยต", "u"): e for u, e in unit.items()} + return fmt.formatter( + proc.items(), + as_ratio=True, + single_denominator=False, + product_fmt="*", + division_fmt="/", + power_fmt="{}{}", + parentheses_fmt="({})", + **options, + ) + + base_unit = ureg.microsecond + assert f"{base_unit:~test}" == "us" + assert f"{base_unit:test}" == "microsecond" From d87d34b39cee665c0945c93317ae38580aae2960 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 21 Jun 2024 16:08:54 +0100 Subject: [PATCH 2/3] changes --- CHANGES | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES b/CHANGES index 52bdf1a8a..11df3542b 100644 --- a/CHANGES +++ b/CHANGES @@ -7,6 +7,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) +- Fix bug preventing custom formatters with modifiers working. (PR #2021) 0.24 (2024-06-07) ----------------- From 17e055a4f02a5a88cbf6f61e105f2682aeefcfac Mon Sep 17 00:00:00 2001 From: andrewgsavage Date: Fri, 21 Jun 2024 16:10:10 +0100 Subject: [PATCH 3/3] Update test_issues.py --- pint/testsuite/test_issues.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pint/testsuite/test_issues.py b/pint/testsuite/test_issues.py index ead3672e0..3f3d69e67 100644 --- a/pint/testsuite/test_issues.py +++ b/pint/testsuite/test_issues.py @@ -1286,7 +1286,7 @@ def _test_format(unit, registry, **options): assert f"{base_unit:~test}" == "us" assert f"{base_unit:test}" == "microsecond" - + def test_issue2007(): ureg = UnitRegistry() q = ureg.Quantity(1, "")