Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of units string with same canonalized name #1476

Merged
merged 1 commit into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Pint Changelog
- Add `pint.testing` with functions to compare pint objects in tests (Issue #1421).
- Fix handling modulo & floordiv operator in pint_eval (Issue #1470)
- Fix `to_compact` and `infer_base_unit` for non-float non_int_type.
- Fix parsing of units string with same canonalized name (Issue #1441 & #1142)

### Breaking Changes

Expand Down
13 changes: 8 additions & 5 deletions pint/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,12 @@ def parse_units(
units = self._parse_units(input_string, as_delta, case_sensitive)
return self.Unit(units)

def _parse_units(self, input_string, as_delta=True, case_sensitive=None):
def _parse_units(
self,
input_string: str,
as_delta: bool = True,
case_sensitive: Optional[bool] = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as far as I can tell (I don't really use type hints myself), in newer versions of python, type hints with = None imply Optional:

Suggested change
case_sensitive: Optional[bool] = None,
case_sensitive: bool = None,

) -> UnitsContainerT:
"""Parse a units expression and returns a UnitContainer with
the canonical names.
"""
Expand All @@ -1215,7 +1220,7 @@ def _parse_units(self, input_string, as_delta=True, case_sensitive=None):
if units.scale != 1:
raise ValueError("Unit expression cannot have a scaling factor.")

ret = {}
ret = self.UnitsContainer({})
many = len(units) > 1
for name in units:
cname = self.get_name(name, case_sensitive=case_sensitive)
Expand All @@ -1226,9 +1231,7 @@ def _parse_units(self, input_string, as_delta=True, case_sensitive=None):
definition = self._units[cname]
if not definition.is_multiplicative:
cname = "delta_" + cname
ret[cname] = value

ret = self.UnitsContainer(ret)
ret = ret.add(cname, value)

if as_delta:
cache[input_string] = ret
Expand Down
11 changes: 11 additions & 0 deletions pint/testsuite/test_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,17 @@ def test_to_reduced_units(self):
q = self.Q_(0.5, "g*t/kg")
helpers.assert_quantity_equal(q.to_reduced_units(), self.Q_(0.5, "kg"))

@pytest.mark.parametrize(
("unit_str", "expected_unit"),
[
("hour/hr", {}),
("cm centimeter cm centimeter", {"centimeter": 4}),
],
)
def test_unit_canonical_name_parsing(self, unit_str, expected_unit):
q = self.Q_(1, unit_str)
assert q._units == UnitsContainer(expected_unit)


# TODO: do not subclass from QuantityTestCase
class TestQuantityToCompact(QuantityTestCase):
Expand Down