diff --git a/pint/facets/plain/registry.py b/pint/facets/plain/registry.py index e45388513..f48c93438 100644 --- a/pint/facets/plain/registry.py +++ b/pint/facets/plain/registry.py @@ -897,29 +897,35 @@ def _get_root_units( except KeyError: pass - accumulators: dict[str | None, int] = defaultdict(int) - fraction: dict[str, dict[Decimal|Float, Decimal|int]] = dict(numerator=dict(), denominator=dict()) + fraction: dict[str, dict[Decimal | float, Decimal | int]] = dict( + numerator=dict(), denominator=dict() + ) self._get_root_units_recurse(input_units, 1, accumulators, fraction) - + # Identify if terms appear in both numerator and denominator def terms_are_unique(fraction): for n_factor, n_exp in fraction["numerator"].items(): if n_factor in fraction["denominator"]: return False return True + # Cancel out terms where factor matches while not terms_are_unique(fraction): for n_factor, n_exponent in fraction["numerator"].items(): if n_factor in fraction["denominator"]: if n_exponent >= fraction["denominator"][n_factor]: - fraction["numerator"][n_factor] -= fraction["denominator"][n_factor] + fraction["numerator"][n_factor] -= fraction["denominator"][ + n_factor + ] del fraction["denominator"][n_factor] continue for d_factor, d_exponent in fraction["denominator"].items(): if d_factor in fraction["numerator"]: if d_exponent >= fraction["numerator"][d_factor]: - fraction["denominator"][d_factor] -= fraction["numerator"][d_factor] + fraction["denominator"][d_factor] -= fraction["numerator"][ + d_factor + ] del fraction["numerator"][d_factor] continue @@ -981,7 +987,11 @@ def get_base_units( # TODO: accumulators breaks typing list[int, dict[str, int]] # So we have changed the behavior here def _get_root_units_recurse( - self, ref: UnitsContainer, exp: Scalar, accumulators: dict[str | None, int], fraction: dict[str, dict[Decimal|float, Decimal|int]] + self, + ref: UnitsContainer, + exp: Scalar, + accumulators: dict[str | None, int], + fraction: dict[str, dict[Decimal | float, Decimal | int]], ) -> None: """ @@ -996,13 +1006,18 @@ def _get_root_units_recurse( accumulators[key] += exp2 else: # Build numerator and denominator - #print(reg.converter.scale, exp2) if exp2 < 0: - fraction['denominator'][reg.converter.scale] = fraction['denominator'].get(reg.converter.scale, 0)-exp2 + fraction["denominator"][reg.converter.scale] = ( + fraction["denominator"].get(reg.converter.scale, 0) - exp2 + ) else: - fraction['numerator'][reg.converter.scale] = fraction['numerator'].get(reg.converter.scale, 0)+exp2 + fraction["numerator"][reg.converter.scale] = ( + fraction["numerator"].get(reg.converter.scale, 0) + exp2 + ) if reg.reference is not None: - self._get_root_units_recurse(reg.reference, exp2, accumulators, fraction) + self._get_root_units_recurse( + reg.reference, exp2, accumulators, fraction + ) def get_compatible_units(self, input_units: QuantityOrUnitLike) -> frozenset[UnitT]: """ """ diff --git a/pint/testsuite/test_issues.py b/pint/testsuite/test_issues.py index 2b7629195..60af8416a 100644 --- a/pint/testsuite/test_issues.py +++ b/pint/testsuite/test_issues.py @@ -1355,8 +1355,8 @@ def test_issue2107(): # Use decimal ureg = UnitRegistry(non_int_type=decimal.Decimal) # 2 L/h is equal to 48 L/day - flow = decimal.Decimal('2')*ureg.L/ureg.h - assert flow.to(ureg.L/ureg.day).magnitude == 48.0 + flow = decimal.Decimal("2") * ureg.L / ureg.h + assert flow.to(ureg.L / ureg.day).magnitude == 48.0 # 1 inch is equal to 1000 thou distance = ureg.Quantity(decimal.Decimal("1.0"), ureg.inch) assert distance.to(ureg.thou).magnitude == 1000.0 @@ -1364,8 +1364,8 @@ def test_issue2107(): # Perform the same conversions without decimal ureg = UnitRegistry() # 2 L/h is equal to 48 L/day - flow = 2*ureg.L/ureg.h - assert flow.to(ureg.L/ureg.day).magnitude == 48.0 + flow = 2 * ureg.L / ureg.h + assert flow.to(ureg.L / ureg.day).magnitude == 48.0 # 1 inch is equal to 1000 thou distance = ureg.Quantity(1, ureg.inch) assert distance.to(ureg.thou).magnitude == 1000.0