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

TypeError when combining auto_reduce_dimensions=True and non_int_type=Decimal #1845

Closed
jonemo opened this issue Sep 13, 2023 · 0 comments · Fixed by #1853
Closed

TypeError when combining auto_reduce_dimensions=True and non_int_type=Decimal #1845

jonemo opened this issue Sep 13, 2023 · 0 comments · Fixed by #1853

Comments

@jonemo
Copy link
Contributor

jonemo commented Sep 13, 2023

To reproduce run:

import pint
from decimal import Decimal

reg = pint.UnitRegistry(
    auto_reduce_dimensions=True,
    non_int_type=Decimal,
)
reg("km / h * m")

Resulting output is:

Traceback (most recent call last):
  File "/path/to/my/pintrepro.py", line 10, in <module>
    print(reg("km / h * m"))
          ^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.11/site-packages/pint/facets/plain/registry.py", line 1313, in parse_expression
    return build_eval_tree(gen).evaluate(_define_op)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.11/site-packages/pint/pint_eval.py", line 146, in evaluate
    return bin_op[op_text](
           ^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.11/site-packages/pint/facets/plain/quantity.py", line 998, in __mul__
    return self._mul_div(other, operator.mul)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.11/site-packages/pint/facets/plain/quantity.py", line 100, in wrapped
    return f(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.11/site-packages/pint/facets/plain/quantity.py", line 83, in wrapped
    result.ito_reduced_units()
  File "/path/to/my/venv/lib/python3.11/site-packages/pint/facets/plain/qto.py", line 61, in ito_reduced_units
    return quantity.ito(new_units)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.11/site-packages/pint/facets/plain/quantity.py", line 492, in ito
    self._magnitude = self._convert_magnitude(other, *contexts, **ctx_kwargs)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.11/site-packages/pint/facets/plain/quantity.py", line 469, in _convert_magnitude
    return self._REGISTRY.convert(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.11/site-packages/pint/facets/plain/registry.py", line 965, in convert
    return self._convert(value, src, dst, inplace)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.11/site-packages/pint/facets/context/registry.py", line 403, in _convert
    return super()._convert(value, src, dst, inplace)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.11/site-packages/pint/facets/nonmultiplicative/registry.py", line 254, in _convert
    return super()._convert(value, src, dst, inplace)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.11/site-packages/pint/facets/plain/registry.py", line 999, in _convert
    dst_dim = self._get_dimensionality(dst)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/venv/lib/python3.11/site-packages/pint/facets/plain/registry.py", line 690, in _get_dimensionality
    self._get_dimensionality_recurse(input_units, 1, accumulator)
  File "/path/to/my/venv/lib/python3.11/site-packages/pint/facets/plain/registry.py", line 720, in _get_dimensionality_recurse
    self._get_dimensionality_recurse(reg.reference, exp2, accumulator)
  File "/path/to/my/venv/lib/python3.11/site-packages/pint/facets/plain/registry.py", line 708, in _get_dimensionality_recurse
    exp2 = exp * ref[key]
           ~~~~^~~~~~~~~~
TypeError: unsupported operand type(s) for *: 'float' and 'decimal.Decimal'

Observed with the latest version of pint (0.22).

This doesn't get triggered by every formula, and I have not figured out the minimal necessary conditions to trigger the error. These examples result in the error:

km / h * m
kW / min * W

... but these do not:

m / h * m
km / s * m
m / h * m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant