Skip to content

Commit

Permalink
Ensure called default_sort_func is not None
Browse files Browse the repository at this point in the history
It would be easier to just default to `sorted` instead of `None`, but since `None` is an option, we have to test for it anyway.

Signed-off-by: Michael Tiemann <[email protected]>
  • Loading branch information
MichaelTiemannOSC committed Jan 23, 2024
1 parent 4551586 commit 7b9bc34
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 7 additions & 3 deletions pint/delegates/formatter/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ 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,
Expand All @@ -87,9 +93,7 @@ def format_unit(
division_fmt=r"{}/{}",
power_fmt=r"{}<sup>{}</sup>",
parentheses_fmt=r"({})",
sort_func=lambda x: unit._REGISTRY.formatter.default_sort_func(
x, unit._REGISTRY
),
sort_func=sort_func,
)

def format_quantity(
Expand Down
10 changes: 7 additions & 3 deletions pint/delegates/formatter/plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ 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,
Expand All @@ -269,9 +275,7 @@ def format_unit(
power_fmt="{}{}",
parentheses_fmt="({})",
exp_call=pretty_fmt_exponent,
sort_func=lambda x: unit._REGISTRY.formatter.default_sort_func(
x, unit._REGISTRY
),
sort_func=sort_func,
)

def format_quantity(
Expand Down

0 comments on commit 7b9bc34

Please sign in to comment.