Error assigning quantity with pint quantity in section constructor
Problem:
There is an error when trying to assign a nomad quantity value with a pint Quantity
in the section constructor.
Minimum breaking example:
Adding a class A
to, for example, nomad.datamodel.metainfo.basesections
(same error in other locations and in plugins):
class A(ArchiveSection):
a = Quantity(
type=float,
unit='meter',
)
and trying to construct an instance in a normalizer or even in a python console gives the following error
(.pyenv) [hampusnasstrom@fedora nomad]$ python
Python 3.9.18 (main, Aug 28 2023, 00:00:00)
[GCC 12.3.1 20230508 (Red Hat 12.3.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from nomad.datamodel.metainfo.basesections import A, ureg
>>> A(a=ureg.Quantity(3.14, 'meter'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/hampusnasstrom/repositories/nomad/nomad/metainfo/metainfo.py", line 1248, in __init__
self.m_update(**other_kwargs)
File "/home/hampusnasstrom/repositories/nomad/nomad/metainfo/metainfo.py", line 2075, in m_update
self.m_set(prop, value)
File "/home/hampusnasstrom/repositories/nomad/nomad/metainfo/metainfo.py", line 1588, in m_set
value = self.__set_normalize(quantity_def, value)
File "/home/hampusnasstrom/repositories/nomad/nomad/metainfo/metainfo.py", line 1508, in __set_normalize
return MTypes.primitive[target_type](value) # type: ignore
File "/home/hampusnasstrom/repositories/nomad/nomad/metainfo/util.py", line 201, in <lambda>
float: lambda v: None if v is None else float(v),
File "/home/hampusnasstrom/repositories/nomad/.pyenv/lib64/python3.9/site-packages/pint/quantity.py", line 833, in __float__
raise DimensionalityError(self._units, "dimensionless")
pint.errors.DimensionalityError: Cannot convert from 'meter' to 'dimensionless'
If the quantity is instead set with the setter it works like expected:
>>> a = A()
>>> a.a = ureg.Quantity(3.14, 'meter')
>>> print(a.a)
3.14 meter