Skip to content

Commit 7b9bc34

Browse files
Ensure called default_sort_func is not None
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]>
1 parent 4551586 commit 7b9bc34

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

pint/delegates/formatter/html.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ def format_unit(
7878
self, unit: PlainUnit, uspec: str = "", **babel_kwds: Unpack[BabelKwds]
7979
) -> str:
8080
units = format_compound_unit(unit, uspec, **babel_kwds)
81+
if unit._REGISTRY.formatter.default_sort_func is not None:
82+
sort_func = lambda x: unit._REGISTRY.formatter.default_sort_func(
83+
x, unit._REGISTRY
84+
)
85+
else:
86+
sort_func = None
8187

8288
return formatter(
8389
units,
@@ -87,9 +93,7 @@ def format_unit(
8793
division_fmt=r"{}/{}",
8894
power_fmt=r"{}<sup>{}</sup>",
8995
parentheses_fmt=r"({})",
90-
sort_func=lambda x: unit._REGISTRY.formatter.default_sort_func(
91-
x, unit._REGISTRY
92-
),
96+
sort_func=sort_func,
9397
)
9498

9599
def format_quantity(

pint/delegates/formatter/plain.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ def format_unit(
259259
self, unit: PlainUnit, uspec: str = "", **babel_kwds: Unpack[BabelKwds]
260260
) -> str:
261261
units = format_compound_unit(unit, uspec, **babel_kwds)
262+
if unit._REGISTRY.formatter.default_sort_func is not None:
263+
sort_func = lambda x: unit._REGISTRY.formatter.default_sort_func(
264+
x, unit._REGISTRY
265+
)
266+
else:
267+
sort_func = None
262268

263269
return formatter(
264270
units,
@@ -269,9 +275,7 @@ def format_unit(
269275
power_fmt="{}{}",
270276
parentheses_fmt="({})",
271277
exp_call=pretty_fmt_exponent,
272-
sort_func=lambda x: unit._REGISTRY.formatter.default_sort_func(
273-
x, unit._REGISTRY
274-
),
278+
sort_func=sort_func,
275279
)
276280

277281
def format_quantity(

0 commit comments

Comments
 (0)