diff options
Diffstat (limited to 'pint/facets/system/registry.py')
-rw-r--r-- | pint/facets/system/registry.py | 80 |
1 files changed, 54 insertions, 26 deletions
diff --git a/pint/facets/system/registry.py b/pint/facets/system/registry.py index 6e0878e..30921bd 100644 --- a/pint/facets/system/registry.py +++ b/pint/facets/system/registry.py @@ -9,10 +9,14 @@ from __future__ import annotations from numbers import Number -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Generic, Any from ... import errors +from ...compat import TypeAlias + +from ..plain import QuantityT, UnitT + if TYPE_CHECKING: from ..._typing import Quantity, Unit @@ -22,13 +26,14 @@ from ...util import ( create_class_with_registry, to_units_container, ) -from ..group import GroupRegistry +from ..group import GenericGroupRegistry from .definitions import SystemDefinition -from .objects import Lister, System from . import objects -class SystemRegistry(GroupRegistry): +class GenericSystemRegistry( + Generic[QuantityT, UnitT], GenericGroupRegistry[QuantityT, UnitT] +): """Handle of Systems. Conversion between units with different dimensions according @@ -46,24 +51,24 @@ class SystemRegistry(GroupRegistry): # TODO: Change this to System: System to specify class # and use introspection to get system class as a way # to enjoy typing goodies - System = objects.System + System: type[objects.System] - def __init__(self, system=None, **kwargs): + def __init__(self, system: str | None = None, **kwargs): super().__init__(**kwargs) #: Map system name to system. #: :type: dict[ str | System] - self._systems: dict[str, System] = {} + self._systems: dict[str, objects.System] = {} #: Maps dimensionality (UnitsContainer) to Dimensionality (UnitsContainer) - self._base_units_cache = {} + self._base_units_cache: dict[UnitsContainerT, UnitsContainerT] = {} - self._default_system = system + self._default_system_name: str | None = system def _init_dynamic_classes(self) -> None: """Generate subclasses on the fly and attach them to self""" super()._init_dynamic_classes() - self.System = create_class_with_registry(self, self.System) + self.System = create_class_with_registry(self, objects.System) def _after_init(self) -> None: """Invoked at the end of ``__init__``. @@ -74,7 +79,7 @@ class SystemRegistry(GroupRegistry): super()._after_init() #: System name to be used by default. - self._default_system = self._default_system or self._defaults.get( + self._default_system_name = self._default_system_name or self._defaults.get( "system", None ) @@ -82,7 +87,7 @@ class SystemRegistry(GroupRegistry): super()._register_definition_adders() self._register_adder(SystemDefinition, self._add_system) - def _add_system(self, sd: SystemDefinition): + def _add_system(self, sd: SystemDefinition) -> None: if sd.name in self._systems: raise ValueError(f"System {sd.name} already present in registry") @@ -96,29 +101,29 @@ class SystemRegistry(GroupRegistry): @property def sys(self): - return Lister(self._systems) + return objects.Lister(self._systems) @property - def default_system(self) -> System: - return self._default_system + def default_system(self) -> str | None: + return self._default_system_name @default_system.setter - def default_system(self, name): + def default_system(self, name: str) -> None: if name: if name not in self._systems: raise ValueError("Unknown system %s" % name) self._base_units_cache = {} - self._default_system = name + self._default_system_name = name - def get_system(self, name: str, create_if_needed: bool = True) -> System: + def get_system(self, name: str, create_if_needed: bool = True) -> objects.System: """Return a Group. Parameters ---------- name : str - Name of the group to be + Name of the group to be. create_if_needed : bool If True, create a group if not found. If False, raise an Exception. (Default value = True) @@ -141,7 +146,7 @@ class SystemRegistry(GroupRegistry): self, input_units: UnitLike | Quantity, check_nonmult: bool = True, - system: str | System | None = None, + system: str | objects.System | None = None, ) -> tuple[Number, Unit]: """Convert unit or dict of units to the plain units. @@ -179,15 +184,15 @@ class SystemRegistry(GroupRegistry): self, input_units: UnitsContainerT, check_nonmult: bool = True, - system: str | System | None = None, + system: str | objects.System | None = None, ): if system is None: - system = self._default_system + system = self._default_system_name # The cache is only done for check_nonmult=True and the current system. if ( check_nonmult - and system == self._default_system + and system == self._default_system_name and input_units in self._base_units_cache ): return self._base_units_cache[input_units] @@ -220,16 +225,32 @@ class SystemRegistry(GroupRegistry): return base_factor, destination_units - def _get_compatible_units(self, input_units, group_or_system) -> frozenset[Unit]: + def get_compatible_units( + self, input_units: UnitsContainerT, group_or_system: str | None = None + ) -> frozenset[Unit]: + """ """ + + group_or_system = group_or_system or self._default_system_name + if group_or_system is None: - group_or_system = self._default_system + return super().get_compatible_units(input_units) + + input_units = to_units_container(input_units) + + equiv = self._get_compatible_units(input_units, group_or_system) + + return frozenset(self.Unit(eq) for eq in equiv) + def _get_compatible_units( + self, input_units: UnitsContainerT, group_or_system: str | None = None + ) -> frozenset[Unit]: if group_or_system and group_or_system in self._systems: members = self._systems[group_or_system].members # group_or_system has been handled by System - return frozenset(members & super()._get_compatible_units(input_units, None)) + return frozenset(members & super()._get_compatible_units(input_units)) try: + # This will be handled by groups return super()._get_compatible_units(input_units, group_or_system) except ValueError as ex: # It might be also a system @@ -238,3 +259,10 @@ class SystemRegistry(GroupRegistry): "Unknown Group o System with name '%s'" % group_or_system ) from ex raise ex + + +class SystemRegistry( + GenericSystemRegistry[objects.SystemQuantity[Any], objects.SystemUnit] +): + Quantity: TypeAlias = objects.SystemQuantity[Any] + Unit: TypeAlias = objects.SystemUnit |