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 calling to_base_units() for temperature. #1950

Closed
biagiodistefano opened this issue Mar 8, 2024 · 1 comment
Closed

TypeError when calling to_base_units() for temperature. #1950

biagiodistefano opened this issue Mar 8, 2024 · 1 comment

Comments

@biagiodistefano
Copy link

biagiodistefano commented Mar 8, 2024

Steps to reproduce

from decimal import Decimal
from pint import UnitRegistry

ureg = UnitRegistry()

qt = ureg.Quantity(Decimal(10.0), "°C")  # <Quantity(10, 'degree_Celsius')>
qt.ito_base_units()

Causes:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File ".venv/lib/python3.11/site-packages/pint/facets/plain/quantity.py", line 555, in to_base_units
    magnitude = self._convert_magnitude_not_inplace(other)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/pint/facets/plain/quantity.py", line 462, in _convert_magnitude_not_inplace
    return self._REGISTRY.convert(self._magnitude, self._units, other)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/pint/facets/plain/registry.py", line 961, in convert
    return self._convert(value, src, dst, inplace)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/pint/facets/context/registry.py", line 403, in _convert
    return super()._convert(value, src, dst, inplace)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/pint/facets/nonmultiplicative/registry.py", line 266, in _convert
    value = self._units[src_offset_unit].converter.to_reference(value, inplace)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/pint/facets/nonmultiplicative/definitions.py", line 33, in to_reference
    value = value * self.scale + self.offset
            ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
TypeError: unsupported operand type(s) for +: 'decimal.Decimal' and 'float'

Same with "°F".

Ideas

In ScaleConverter, scale: float, but the method to_reference() accepts value: Magnitude, and Magnitude = Union[Scalar, Array], Scalar: TypeAlias = Union[float, int, Decimal, Fraction].

My suggestion would be: instead of using self.offset for the operations in the methods, use a typecasted version of it that matches value's type.

If you find this okay, I will make a PR as soon as I have time.

For now the workaround is to just pass float instead of decimal when working with temperature units.

EDIT: scale -> offset

biagiodistefano added a commit to biagiodistefano/pint that referenced this issue Mar 9, 2024
biagiodistefano added a commit to biagiodistefano/pint that referenced this issue Mar 9, 2024
@andrewgsavage
Copy link
Collaborator

you need to use
ureg = UnitRegistry(non_int_type=Decimal)

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.

2 participants