Skip to content

Commit

Permalink
Fix to_reduced_units with dimensionless units
Browse files Browse the repository at this point in the history
Fix #919
  • Loading branch information
jules-ch committed Mar 2, 2022
1 parent 47bbe5b commit ee198f0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
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 `to_reduced_units` to work with dimensionless units. (Issue #919)

### Breaking Changes

Expand Down
2 changes: 2 additions & 0 deletions pint/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,8 @@ def _get_dimensionality_ratio(self, unit1, unit2):
return 1

dim1, dim2 = (self.get_dimensionality(unit) for unit in (unit1, unit2))
if dim1 == dim2:
return 1
if not dim1 or not dim2 or dim1.keys() != dim2.keys(): # not comparable
return None

Expand Down
7 changes: 7 additions & 0 deletions pint/testsuite/test_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,13 @@ 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"))

def test_to_reduced_units_dimensionless(self):
ureg = UnitRegistry(preprocessors=[lambda x: x.replace("%", " percent ")])
ureg.define("percent = 0.01 count = %")
Q_ = ureg.Quantity
reduced_quantity = (Q_("1 s") * Q_("5 %") / Q_("1 count")).to_reduced_units()
assert reduced_quantity == ureg.Quantity(0.05, ureg.second)


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

0 comments on commit ee198f0

Please sign in to comment.