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 Custom formatters not working with modifiers #2021

Merged
merged 4 commits into from
Jun 21, 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
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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)
-----------------
Expand Down
8 changes: 5 additions & 3 deletions pint/delegates/formatter/full.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
32 changes: 31 additions & 1 deletion pint/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1257,6 +1262,31 @@ def test_issue1772(given, expected):
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"


def test_issue2007():
ureg = UnitRegistry()
q = ureg.Quantity(1, "")
Expand Down
Loading