From 7b9bc340b3db1ed49ea26d98fa89875efc3c8dde Mon Sep 17 00:00:00 2001 From: Michael Tiemann <72577720+MichaelTiemannOSC@users.noreply.github.com> Date: Tue, 23 Jan 2024 21:02:23 +1300 Subject: [PATCH] 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 <72577720+MichaelTiemannOSC@users.noreply.github.com> --- pint/delegates/formatter/html.py | 10 +++++++--- pint/delegates/formatter/plain.py | 10 +++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/pint/delegates/formatter/html.py b/pint/delegates/formatter/html.py index cc7d9ad07..98329bcca 100644 --- a/pint/delegates/formatter/html.py +++ b/pint/delegates/formatter/html.py @@ -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, @@ -87,9 +93,7 @@ def format_unit( division_fmt=r"{}/{}", power_fmt=r"{}{}", parentheses_fmt=r"({})", - sort_func=lambda x: unit._REGISTRY.formatter.default_sort_func( - x, unit._REGISTRY - ), + sort_func=sort_func, ) def format_quantity( diff --git a/pint/delegates/formatter/plain.py b/pint/delegates/formatter/plain.py index 37150f9d2..850e2eab5 100644 --- a/pint/delegates/formatter/plain.py +++ b/pint/delegates/formatter/plain.py @@ -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, @@ -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(