Skip to content

Commit

Permalink
Fix default_sort_func
Browse files Browse the repository at this point in the history
The default sort function needs to be able to handle a registry passed to it, so to make `sorted` the default behavior, we have to create a lambda that strips the registry parameter before calling `sorted`.

Signed-off-by: Michael Tiemann <[email protected]>
  • Loading branch information
MichaelTiemannOSC committed Jan 23, 2024
1 parent 7b9bc34 commit a47b5e9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
14 changes: 11 additions & 3 deletions pint/delegates/formatter/full.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
from ._to_register import REGISTERED_FORMATTERS

if TYPE_CHECKING:
from ...facets.plain import PlainQuantity, PlainUnit, MagnitudeT
from ...facets.plain import (
GenericPlainRegistry,
PlainQuantity,
PlainUnit,
MagnitudeT,
)
from ...facets.measurement import Measurement
from ...compat import Locale

Expand All @@ -50,8 +55,11 @@ class FullFormatter:
"[temperature]",
)
default_sort_func: Optional[
Callable[Iterable[tuple[str, Number]]], Iterable[tuple[str, Number]]
] = None
Callable[
[Iterable[tuple[str, Number]], GenericPlainRegistry],
Iterable[tuple[str, Number]],
]
] = lambda self, x, registry: sorted(x)

locale: Optional[Locale] = None
babel_length: Literal["short", "long", "narrow"] = "long"
Expand Down
11 changes: 3 additions & 8 deletions pint/delegates/formatter/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ def format_unit(
self, unit: PlainUnit, uspec: str = "", **babel_kwds: Unpack[BabelKwds]
) -> str:
units = format_compound_unit(unit, uspec, **babel_kwds)
if unit._REGISTRY.formatter.default_sort_func is not None:
sort_func = lambda x: unit._REGISTRY.formatter.default_sort_func(
x, unit._REGISTRY
)
else:
sort_func = None

return formatter(
units,
as_ratio=True,
Expand All @@ -93,7 +86,9 @@ def format_unit(
division_fmt=r"{}/{}",
power_fmt=r"{}<sup>{}</sup>",
parentheses_fmt=r"({})",
sort_func=sort_func,
sort_func=lambda x: unit._REGISTRY.formatter.default_sort_func(
x, unit._REGISTRY
),
)

def format_quantity(
Expand Down
5 changes: 2 additions & 3 deletions pint/delegates/formatter/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,8 @@ def format_unit(
self, unit: PlainUnit, uspec: str = "", **babel_kwds: Unpack[BabelKwds]
) -> str:
units = format_compound_unit(unit, uspec, **babel_kwds)
if unit._REGISTRY.formatter.default_sort_func:
# Lift the sorting by dimensions b/c the preprocessed units are unrecognizeable
units = unit._REGISTRY.formatter.default_sort_func(units, unit._REGISTRY)
# Lift the sorting by dimensions b/c the preprocessed units are unrecognizeable
units = unit._REGISTRY.formatter.default_sort_func(units, unit._REGISTRY)

preprocessed = {rf"\mathrm{{{latex_escape(u)}}}": p for u, p in units}
formatted = formatter(
Expand Down
11 changes: 3 additions & 8 deletions pint/delegates/formatter/plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,6 @@ def format_unit(
self, unit: PlainUnit, uspec: str = "", **babel_kwds: Unpack[BabelKwds]
) -> str:
units = format_compound_unit(unit, uspec, **babel_kwds)
if unit._REGISTRY.formatter.default_sort_func is not None:
sort_func = lambda x: unit._REGISTRY.formatter.default_sort_func(
x, unit._REGISTRY
)
else:
sort_func = None

return formatter(
units,
as_ratio=True,
Expand All @@ -275,7 +268,9 @@ def format_unit(
power_fmt="{}{}",
parentheses_fmt="({})",
exp_call=pretty_fmt_exponent,
sort_func=sort_func,
sort_func=lambda x: unit._REGISTRY.formatter.default_sort_func(
x, unit._REGISTRY
),
)

def format_quantity(
Expand Down

0 comments on commit a47b5e9

Please sign in to comment.