You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Traceback (most recent call last):
File "<the test file>", line 9, in <module>
arr[0] = Q_(0.0, 'm')
File "...\anaconda2\lib\site-packages\pint\quantity.py", line 1425, in __setitem__
'`obj.magnitude[%s] = %s`' % (key, value))
pint.errors.DimensionalityError: Cannot convert from '0.0 meter' to 'meter'. Assign a quantity with the same dimensionality or access the magnitude directly as `obj.magnitude[0] = 0.0 meter`
The error message is not very helpful. Inspecting the two objects reveals they share the unit registry, so it is not a case of unit systems.
What happens
The subclassing causes the __setitem__ method to take a certain conditional branch. Relevant excerpt for reference
# self is the array we set an item of, value is what we put inifisinstance(value, self.__class__):
factor=self.__class__(value.magnitude, value._units/self._units).to_root_units()
else:
factor=self.__class__(value, self._units** (-1)).to_root_units()
ifisinstance(factor, self.__class__): # not sure that test is ever False BTWifnotfactor.dimensionless:
raiseDimensionalityError(value, self.units,
extra_msg='. Assign a quantity with the same dimensionality or ''access the magnitude directly as ''`obj.magnitude[%s] = %s`'% (key, value))
Because the value we try to put in is of the parent class, it is not an instance of the child class the array has, and isinstance(value, self.__class__) == False. It causes factor to become a dimensional quantity and fail the subsequent test. The resulting error message is nonsensical ("cannot convert some_unit to some_unit" is hardly helpful for debugging).
What could be done
Option 1 : nothing. (Whoever subclasses the pint classes is responsible for making them work, redefining methods etc.)
Option 2 : if the test is actually here to detect whether value is a dimensional quantity, check the relevant attributes directly (EAFP): replace the first part of the snippet by
I do not know enough about pint's design to confidently suggest a pull request for option 2 before asking first. There might also be another option I missed.
The text was updated successfully, but these errors were encountered:
The problem
Code to reproduce: (pint 0.9)
causes the error
The error message is not very helpful. Inspecting the two objects reveals they share the unit registry, so it is not a case of unit systems.
What happens
The subclassing causes the
__setitem__
method to take a certain conditional branch. Relevant excerpt for referenceBecause the value we try to put in is of the parent class, it is not an instance of the child class the array has, and
isinstance(value, self.__class__) == False
. It causesfactor
to become a dimensional quantity and fail the subsequent test. The resulting error message is nonsensical ("cannot convert some_unit to some_unit" is hardly helpful for debugging).What could be done
Option 1 : nothing. (Whoever subclasses the pint classes is responsible for making them work, redefining methods etc.)
Option 2 : if the test is actually here to detect whether
value
is a dimensional quantity, check the relevant attributes directly (EAFP): replace the first part of the snippet byI do not know enough about pint's design to confidently suggest a pull request for option 2 before asking first. There might also be another option I missed.
The text was updated successfully, but these errors were encountered: