Search code examples
pythonpint

KeyError with custom derived quantity


I have defined a new derived dimension with

[molar_energy] = [energy] / [substance]

However, if I do the following it complains:

>>> UR.get_compatible_units('[molar_energy]')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/cedric/.local/share/virtualenvs/MatDB--uGOYMXa/lib/python3.9/site-packages/pint/registry.py", line 881, in get_compatible_units
    equiv = self._get_compatible_units(input_units, group_or_system)
  File "/Users/cedric/.local/share/virtualenvs/MatDB--uGOYMXa/lib/python3.9/site-packages/pint/registry.py", line 2082, in _get_compatible_units
    ret = super()._get_compatible_units(input_units, group_or_system)
  File "/Users/cedric/.local/share/virtualenvs/MatDB--uGOYMXa/lib/python3.9/site-packages/pint/registry.py", line 1835, in _get_compatible_units
    ret = super()._get_compatible_units(input_units, group_or_system)
  File "/Users/cedric/.local/share/virtualenvs/MatDB--uGOYMXa/lib/python3.9/site-packages/pint/registry.py", line 891, in _get_compatible_units
    return self._cache.dimensional_equivalents[src_dim]
KeyError: <UnitsContainer({'[length]': 2, '[mass]': 1, '[substance]': -1, '[time]': -2})

I saw that there is a conversion included in a context but I don't use it. What I am doing wrong?

Thanks for your help

PS: logged issue https://github.com/hgrecco/pint/issues/1418


Solution

  • Just leaving the solution here for anyone who faces this issue as well.

    I just added a made-up unit and it worked

    # Molar Energy
    [molar_energy] = [energy] / [substance]
    mol_en = J / mol