Skip to content

Commit

Permalink
Add parse_units_as_container to homogeneize input/ouput in registry f…
Browse files Browse the repository at this point in the history
…unctions public and private functions
  • Loading branch information
hgrecco committed Dec 3, 2023
1 parent 7aa995c commit 2138376
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pint/facets/nonmultiplicative/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
23 changes: 19 additions & 4 deletions pint/facets/plain/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions pint/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 2138376

Please sign in to comment.