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

Dimensional equivalents missing when loading from cache #1746

Open
camuthig opened this issue Apr 19, 2023 · 3 comments · May be fixed by #1747
Open

Dimensional equivalents missing when loading from cache #1746

camuthig opened this issue Apr 19, 2023 · 3 comments · May be fixed by #1747

Comments

@camuthig
Copy link

When instantiating a UnitRegistry from cached files, the dimensional equivalents are instantiated as an empty dictionary. This results in all calls to get_compatible_units to fail with a key error.

import pint
ureg = pint.UnitRegistry(cache_folder=tmp_path)
t = ureg.get_compatible_units("L")
assert len(t) > 0

ureg = pint.UnitRegistry(cache_folder=tmp_path)
t = ureg.get_compatible_units("L")  # raises an error

The error in this particular stack trace is

>       t = ureg.get_compatible_units('L')

pint/testsuite/test_diskcache.py:106:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
pint/facets/plain/registry.py:840: in get_compatible_units
    equiv = self._get_compatible_units(input_units, group_or_system)
pint/facets/system/registry.py:237: in _get_compatible_units
    return frozenset(members & super()._get_compatible_units(input_units, None))
pint/facets/group/registry.py:123: in _get_compatible_units
    ret = super()._get_compatible_units(input_units, group)
pint/facets/context/registry.py:398: in _get_compatible_units
    ret = super()._get_compatible_units(input_units, group_or_system)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <pint.registry.UnitRegistry object at 0x102fa6740>
input_units = <ParserHelper(1, {'L': 1})>, group_or_system = None

    def _get_compatible_units(self, input_units, group_or_system):
        """ """
        if not input_units:
            return frozenset()

        src_dim = self._get_dimensionality(input_units)
>       return self._cache.dimensional_equivalents[src_dim]
E       KeyError: <UnitsContainer({'[length]': 3})>

pint/facets/plain/registry.py:850: KeyError
@hgrecco
Copy link
Owner

hgrecco commented Apr 27, 2023

As I mentioned in the PR, I am not sure that storing this cache is a good idea, because they are SharedRegistryObjects. Anyway, we need to fix this.

@camuthig
Copy link
Author

camuthig commented May 4, 2023

I wasn't sure if the PR was a good solution or not for sure. Thanks for the response. If I have a chance to dig into this further I will give it a try.

@hgrecco
Copy link
Owner

hgrecco commented Jul 16, 2023

See #1820

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants