rayference/pinttrs

units_compatible does not take pint.Unit as arguments type

Closed this issue · 3 comments

The signature of units_compatible() says that its arguments must be of type pint.Unit, however the following code:

import pint
from pinttr.util import units_compatible

units_compatible(pint.Unit("m"), pint.Unit("km"))

raises

AttributeError: 'Unit' object has no attribute '_get_non_multiplicative_units'

whereas:

ureg = pint.UnitRegistry()
units_compatible(ureg("m"), ureg("km"))

works fine, which suggests that the arguments type of units_compatible() is pint.Quantity, instead of pint.Unit?

ureg("m") and ureg("km") are both pint.Unit instances with registry ureg. If you check the Pint docs, you'll find out that you actually should never use pint.Unit or pint.Quantity right on; instead, you must create them from a registry.

So my answer is: you simply should not do something like pint.Unit("m"): it will not be compatible with any other unit.

To be more concrete, this will raise the exception you mentioned as well:

1. * pint.Unit("m")

and this will work:

assert isinstance(ureg.Unit("m"), pint.Unit)

Ah, I'm sorry, I have not read that part of the Pint docs... Thank you for the explanation!