From 21383769c866522672d7d8942e584ede37df84c0 Mon Sep 17 00:00:00 2001 From: Hernan Grecco Date: Sun, 3 Dec 2023 13:32:04 -0300 Subject: [PATCH] Add parse_units_as_container to homogeneize input/ouput in registry functions public and private functions --- pint/facets/nonmultiplicative/registry.py | 4 ++-- pint/facets/plain/registry.py | 23 +++++++++++++++++++---- pint/util.py | 3 +-- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/pint/facets/nonmultiplicative/registry.py b/pint/facets/nonmultiplicative/registry.py index 7d783de11..67250ea48 100644 --- a/pint/facets/nonmultiplicative/registry.py +++ b/pint/facets/nonmultiplicative/registry.py @@ -57,7 +57,7 @@ def __init__( # plain units on multiplication and division. self.autoconvert_offset_to_baseunit = autoconvert_offset_to_baseunit - def _parse_units( + def parse_units_as_container( self, input_string: str, as_delta: Optional[bool] = None, @@ -67,7 +67,7 @@ def _parse_units( if as_delta is None: as_delta = self.default_as_delta - return super()._parse_units(input_string, as_delta, case_sensitive) + return super().parse_units_as_container(input_string, as_delta, case_sensitive) def _add_unit(self, definition: UnitDefinition) -> None: super()._add_unit(definition) diff --git a/pint/facets/plain/registry.py b/pint/facets/plain/registry.py index 420179408..39a058e58 100644 --- a/pint/facets/plain/registry.py +++ b/pint/facets/plain/registry.py @@ -1185,14 +1185,29 @@ def parse_units( """ - units = self._parse_units(input_string, as_delta, case_sensitive) - return self.Unit(units) + return self.Unit( + self.parse_units_as_container(input_string, as_delta, case_sensitive) + ) - def _parse_units( + def parse_units_as_container( self, input_string: str, - as_delta: bool = True, + as_delta: Optional[bool] = None, case_sensitive: Optional[bool] = None, + ) -> UnitsContainer: + as_delta = ( + as_delta if as_delta is not None else True + ) # TODO This only exists in nonmultiplicative + case_sensitive = ( + case_sensitive if case_sensitive is not None else self.case_sensitive + ) + return self._parse_units_as_container(input_string, as_delta, case_sensitive) + + def _parse_units_as_container( + self, + input_string: str, + as_delta: bool = True, + case_sensitive: bool = True, ) -> UnitsContainer: """Parse a units expression and returns a UnitContainer with the canonical names. diff --git a/pint/util.py b/pint/util.py index d14722a04..1f7defc50 100644 --- a/pint/util.py +++ b/pint/util.py @@ -1043,8 +1043,7 @@ def to_units_container( # TODO: document how to whether to lift preprocessing loop out to caller for p in registry.preprocessors: unit_like = p(unit_like) - # TODO: Why not parse.units here? - return registry._parse_units(unit_like) + return registry.parse_units_as_container(unit_like) else: return ParserHelper.from_string(unit_like) elif dict in mro: