diff --git a/pint/facets/context/registry.py b/pint/facets/context/registry.py index 540481567..d1a0986b7 100644 --- a/pint/facets/context/registry.py +++ b/pint/facets/context/registry.py @@ -40,7 +40,6 @@ def __init__(self, registry_cache: RegistryCache) -> None: self.dimensional_equivalents = registry_cache.dimensional_equivalents self.root_units = {} self.dimensionality = registry_cache.dimensionality - self.parse_unit = registry_cache.parse_unit class GenericContextRegistry( diff --git a/pint/facets/plain/registry.py b/pint/facets/plain/registry.py index 903fad9d1..d63454ff9 100644 --- a/pint/facets/plain/registry.py +++ b/pint/facets/plain/registry.py @@ -128,9 +128,6 @@ def __init__(self) -> None: #: Maps dimensionality (UnitsContainer) to Units (UnitsContainer) self.dimensionality: dict[UnitsContainer, UnitsContainer] = {} - #: Cache the unit name associated to user input. ('mV' -> 'millivolt') - self.parse_unit: dict[str, UnitsContainer] = {} - def __eq__(self, other: Any): if not isinstance(other, self.__class__): return False @@ -138,7 +135,6 @@ def __eq__(self, other: Any): "dimensional_equivalents", "root_units", "dimensionality", - "parse_unit", ) return all(getattr(self, attr) == getattr(other, attr) for attr in attrs) @@ -589,6 +585,8 @@ def _build_cache(self, loaded_files=None) -> None: return self._cache = RegistryCache() + for func in (self._parse_single_unit, self._parse_units): + func.cache_clear() deps: dict[str, set[str]] = { name: set(definition.reference.keys()) if definition.reference else set() @@ -1164,6 +1162,7 @@ def parse_units( units = self._parse_units(input_string, as_delta, case_sensitive) return self.Unit(units) + @functools.cache def _parse_units( self, input_string: str, @@ -1174,12 +1173,9 @@ def _parse_units( the canonical names. """ - cache = self._cache.parse_unit # Issue #1097: it is possible, when a unit was defined while a different context # was active, that the unit is in self._cache.parse_unit but not in self._units. # If this is the case, force self._units to be repopulated. - if as_delta and input_string in cache and input_string in self._units: - return cache[input_string] for p in self.preprocessors: input_string = p(input_string) @@ -1207,9 +1203,6 @@ def _parse_units( cname = "delta_" + cname ret = ret.add(cname, value) - if as_delta: - cache[input_string] = ret - return ret def _eval_token(