diff options
Diffstat (limited to 'pint/facets')
-rw-r--r-- | pint/facets/context/objects.py | 18 | ||||
-rw-r--r-- | pint/facets/context/registry.py | 10 | ||||
-rw-r--r-- | pint/facets/group/definitions.py | 3 | ||||
-rw-r--r-- | pint/facets/group/objects.py | 8 | ||||
-rw-r--r-- | pint/facets/group/registry.py | 7 | ||||
-rw-r--r-- | pint/facets/nonmultiplicative/objects.py | 4 | ||||
-rw-r--r-- | pint/facets/nonmultiplicative/registry.py | 8 | ||||
-rw-r--r-- | pint/facets/plain/definitions.py | 12 | ||||
-rw-r--r-- | pint/facets/plain/qto.py | 7 | ||||
-rw-r--r-- | pint/facets/plain/quantity.py | 31 | ||||
-rw-r--r-- | pint/facets/plain/registry.py | 68 | ||||
-rw-r--r-- | pint/facets/plain/unit.py | 4 | ||||
-rw-r--r-- | pint/facets/system/definitions.py | 7 | ||||
-rw-r--r-- | pint/facets/system/objects.py | 6 | ||||
-rw-r--r-- | pint/facets/system/registry.py | 19 |
15 files changed, 118 insertions, 94 deletions
diff --git a/pint/facets/context/objects.py b/pint/facets/context/objects.py index c63fd8d..4ab2f1d 100644 --- a/pint/facets/context/objects.py +++ b/pint/facets/context/objects.py @@ -10,7 +10,7 @@ from __future__ import annotations import weakref from collections import ChainMap, defaultdict -from typing import Any, Callable, Protocol, Generic +from typing import Any, Callable, Protocol, Generic, Optional from collections.abc import Iterable from ...facets.plain import UnitDefinition, PlainQuantity, PlainUnit, MagnitudeT @@ -91,11 +91,11 @@ class Context: def __init__( self, - name: str | None = None, + name: Optional[str] = None, aliases: tuple[str, ...] = tuple(), - defaults: dict[str, Any] | None = None, + defaults: Optional[dict[str, Any]] = None, ) -> None: - self.name: str | None = name + self.name: Optional[str] = name self.aliases: tuple[str, ...] = aliases #: Maps (src, dst) -> transformation function @@ -150,7 +150,7 @@ class Context: def from_lines( cls, lines: Iterable[str], - to_base_func: ToBaseFunc | None = None, + to_base_func: Optional[ToBaseFunc] = None, non_int_type: type = float, ) -> Context: context_definition = ContextDefinition.from_lines(lines, non_int_type) @@ -162,7 +162,7 @@ class Context: @classmethod def from_definition( - cls, cd: ContextDefinition, to_base_func: ToBaseFunc | None = None + cls, cd: ContextDefinition, to_base_func: Optional[ToBaseFunc] = None ) -> Context: ctx = cls(cd.name, cd.aliases, cd.defaults) @@ -241,7 +241,7 @@ class Context: def hashable( self, ) -> tuple[ - str | None, + Optional[str], tuple[str, ...], frozenset[tuple[SrcDst, int]], frozenset[tuple[str, Any]], @@ -273,7 +273,7 @@ class ContextChain(ChainMap[SrcDst, Context]): super().__init__() self.contexts: list[Context] = [] self.maps.clear() # Remove default empty map - self._graph: dict[SrcDst, set[UnitsContainer]] | None = None + self._graph: Optional[dict[SrcDst, set[UnitsContainer]]] = None def insert_contexts(self, *contexts: Context): """Insert one or more contexts in reversed order the chained map. @@ -287,7 +287,7 @@ class ContextChain(ChainMap[SrcDst, Context]): self.maps = [ctx.relation_to_context for ctx in reversed(contexts)] + self.maps self._graph = None - def remove_contexts(self, n: int | None = None): + def remove_contexts(self, n: Optional[int] = None): """Remove the last n inserted contexts from the chain. Parameters diff --git a/pint/facets/context/registry.py b/pint/facets/context/registry.py index 746e79c..85682d1 100644 --- a/pint/facets/context/registry.py +++ b/pint/facets/context/registry.py @@ -11,7 +11,7 @@ from __future__ import annotations import functools from collections import ChainMap from contextlib import contextmanager -from typing import Any, Callable, Generator, Generic +from typing import Any, Callable, Generator, Generic, Optional, Union from ...compat import TypeAlias from ..._typing import F, Magnitude @@ -74,7 +74,7 @@ class GenericContextRegistry( super()._register_definition_adders() self._register_adder(ContextDefinition, self.add_context) - def add_context(self, context: objects.Context | ContextDefinition) -> None: + def add_context(self, context: Union[objects.Context, ContextDefinition]) -> None: """Add a context object to the registry. The context will be accessible by its name and aliases. @@ -197,7 +197,7 @@ class GenericContextRegistry( self.define(definition) def enable_contexts( - self, *names_or_contexts: str | objects.Context, **kwargs: Any + self, *names_or_contexts: Union[str, objects.Context], **kwargs: Any ) -> None: """Enable contexts provided by name or by object. @@ -244,7 +244,7 @@ class GenericContextRegistry( self._active_ctx.insert_contexts(*contexts) self._switch_context_cache_and_units() - def disable_contexts(self, n: int | None = None) -> None: + def disable_contexts(self, n: Optional[int] = None) -> None: """Disable the last n enabled contexts. Parameters @@ -403,7 +403,7 @@ class GenericContextRegistry( return super()._convert(value, src, dst, inplace) def _get_compatible_units( - self, input_units: UnitsContainer, group_or_system: str | None = None + self, input_units: UnitsContainer, group_or_system: Optional[str] = None ): src_dim = self._get_dimensionality(input_units) diff --git a/pint/facets/group/definitions.py b/pint/facets/group/definitions.py index f1ee0bc..0a22b50 100644 --- a/pint/facets/group/definitions.py +++ b/pint/facets/group/definitions.py @@ -10,6 +10,7 @@ from __future__ import annotations from collections.abc import Iterable from dataclasses import dataclass +from typing import Optional from ...compat import Self from ... import errors @@ -30,7 +31,7 @@ class GroupDefinition(errors.WithDefErr): @classmethod def from_lines( cls: type[Self], lines: Iterable[str], non_int_type: type - ) -> Self | None: + ) -> Optional[Self]: # TODO: this is to keep it backwards compatible from ...delegates import ParserConfig, txt_defparser diff --git a/pint/facets/group/objects.py b/pint/facets/group/objects.py index 64d91c1..dbd7ecf 100644 --- a/pint/facets/group/objects.py +++ b/pint/facets/group/objects.py @@ -8,7 +8,7 @@ from __future__ import annotations -from typing import Callable, Any, TYPE_CHECKING, Generic +from typing import Callable, Any, TYPE_CHECKING, Generic, Optional from collections.abc import Generator, Iterable from ...util import SharedRegistryObject, getattr_maybe_raise @@ -81,7 +81,7 @@ class Group(SharedRegistryObject): #: A cache of the included units. #: None indicates that the cache has been invalidated. - self._computed_members: frozenset[str] | None = None + self._computed_members: Optional[frozenset[str]] = None @property def members(self) -> frozenset[str]: @@ -195,7 +195,9 @@ class Group(SharedRegistryObject): @classmethod def from_definition( - cls, group_definition: GroupDefinition, add_unit_func: AddUnitFunc | None = None + cls, + group_definition: GroupDefinition, + add_unit_func: Optional[AddUnitFunc] = None, ) -> Group: grp = cls(group_definition.name) diff --git a/pint/facets/group/registry.py b/pint/facets/group/registry.py index f130e61..da068c5 100644 --- a/pint/facets/group/registry.py +++ b/pint/facets/group/registry.py @@ -8,7 +8,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Generic, Any +from typing import TYPE_CHECKING, Generic, Any, Optional from ...compat import TypeAlias from ... import errors @@ -47,7 +47,6 @@ class GenericGroupRegistry( def __init__(self, **kwargs): super().__init__(**kwargs) #: Map group name to group. - #: :type: dict[ str | Group] self._groups: dict[str, objects.Group] = {} self._groups["root"] = self.Group("root") @@ -122,7 +121,7 @@ class GenericGroupRegistry( return self.Group(name) def get_compatible_units( - self, input_units: UnitsContainer, group: str | None = None + self, input_units: UnitsContainer, group: Optional[str] = None ) -> frozenset[Unit]: """ """ if group is None: @@ -135,7 +134,7 @@ class GenericGroupRegistry( return frozenset(self.Unit(eq) for eq in equiv) def _get_compatible_units( - self, input_units: UnitsContainer, group: str | None = None + self, input_units: UnitsContainer, group: Optional[str] = None ) -> frozenset[str]: ret = super()._get_compatible_units(input_units) diff --git a/pint/facets/nonmultiplicative/objects.py b/pint/facets/nonmultiplicative/objects.py index 8b944b1..8ebe8f8 100644 --- a/pint/facets/nonmultiplicative/objects.py +++ b/pint/facets/nonmultiplicative/objects.py @@ -8,7 +8,7 @@ from __future__ import annotations -from typing import Generic +from typing import Generic, Optional from ..plain import PlainQuantity, PlainUnit, MagnitudeT @@ -42,7 +42,7 @@ class NonMultiplicativeQuantity(Generic[MagnitudeT], PlainQuantity[MagnitudeT]): self._get_unit_definition(d).reference == offset_unit_dim for d in deltas ) - def _ok_for_muldiv(self, no_offset_units: int | None = None) -> bool: + def _ok_for_muldiv(self, no_offset_units: Optional[int] = None) -> bool: """Checks if PlainQuantity object can be multiplied or divided""" is_ok = True diff --git a/pint/facets/nonmultiplicative/registry.py b/pint/facets/nonmultiplicative/registry.py index 505406c..7d783de 100644 --- a/pint/facets/nonmultiplicative/registry.py +++ b/pint/facets/nonmultiplicative/registry.py @@ -8,7 +8,7 @@ from __future__ import annotations -from typing import Any, TypeVar, Generic +from typing import Any, TypeVar, Generic, Optional from ...compat import TypeAlias from ...errors import DimensionalityError, UndefinedUnitError @@ -60,8 +60,8 @@ class GenericNonMultiplicativeRegistry( def _parse_units( self, input_string: str, - as_delta: bool | None = None, - case_sensitive: bool | None = None, + as_delta: Optional[bool] = None, + case_sensitive: Optional[bool] = None, ) -> UnitsContainer: """ """ if as_delta is None: @@ -136,7 +136,7 @@ class GenericNonMultiplicativeRegistry( except KeyError: raise UndefinedUnitError(unit_name) - def _validate_and_extract(self, units: UnitsContainer) -> str | None: + def _validate_and_extract(self, units: UnitsContainer) -> Optional[str]: """Used to check if a given units is suitable for a simple conversion. diff --git a/pint/facets/plain/definitions.py b/pint/facets/plain/definitions.py index 5fa822c..44bf298 100644 --- a/pint/facets/plain/definitions.py +++ b/pint/facets/plain/definitions.py @@ -13,7 +13,7 @@ import numbers import typing as ty from dataclasses import dataclass from functools import cached_property -from typing import Any +from typing import Any, Optional from ..._typing import Magnitude from ... import errors @@ -81,7 +81,7 @@ class PrefixDefinition(NamedDefinition, errors.WithDefErr): #: scaling value for this prefix value: numbers.Number #: canonical symbol - defined_symbol: str | None = "" + defined_symbol: Optional[str] = "" #: additional names for the same prefix aliases: ty.Tuple[str, ...] = () @@ -118,7 +118,7 @@ class UnitDefinition(NamedDefinition, errors.WithDefErr): """Definition of a unit.""" #: canonical symbol - defined_symbol: str | None + defined_symbol: Optional[str] #: additional names for the same unit aliases: tuple[str, ...] #: A functiont that converts a value in these units into the reference units @@ -126,15 +126,15 @@ class UnitDefinition(NamedDefinition, errors.WithDefErr): # Briefly, in several places converter attributes like as_multiplicative were # accesed. So having a generic function is a no go. # I guess this was never used as errors where not raised. - converter: Converter | None + converter: Optional[Converter] #: Reference units. - reference: UnitsContainer | None + reference: Optional[UnitsContainer] def __post_init__(self): if not errors.is_valid_unit_name(self.name): raise self.def_err(errors.MSG_INVALID_UNIT_NAME) - # TODO: check why reference: UnitsContainer | None + # TODO: check why reference: Optional[UnitsContainer] assert isinstance(self.reference, UnitsContainer) if not any(map(errors.is_dim, self.reference.keys())): diff --git a/pint/facets/plain/qto.py b/pint/facets/plain/qto.py index 72b8157..0508e9a 100644 --- a/pint/facets/plain/qto.py +++ b/pint/facets/plain/qto.py @@ -1,12 +1,13 @@ from __future__ import annotations -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Optional import bisect import math import numbers -from ...util import infer_base_unit import warnings + +from ...util import infer_base_unit from ...compat import ( mip_INF, mip_INTEGER, @@ -81,7 +82,7 @@ def to_reduced_units( def to_compact( - quantity: PlainQuantity, unit: UnitsContainer | None = None + quantity: PlainQuantity, unit: Optional[UnitsContainer] = None ) -> PlainQuantity: """ "Return PlainQuantity rescaled to compact, human-readable units. diff --git a/pint/facets/plain/quantity.py b/pint/facets/plain/quantity.py index c06d02f..5841a9a 100644 --- a/pint/facets/plain/quantity.py +++ b/pint/facets/plain/quantity.py @@ -14,7 +14,16 @@ import datetime import locale import numbers import operator -from typing import TYPE_CHECKING, Any, Callable, overload, Generic, TypeVar +from typing import ( + TYPE_CHECKING, + Any, + Callable, + overload, + Generic, + TypeVar, + Optional, + Union, +) from collections.abc import Iterator, Sequence from ..._typing import UnitLike, QuantityOrUnitLike, Magnitude, Scalar @@ -155,23 +164,25 @@ class PlainQuantity(Generic[MagnitudeT], PrettyIPython, SharedRegistryObject): @overload def __new__( - cls, value: MagnitudeT, units: UnitLike | None = None + cls, value: MagnitudeT, units: Optional[UnitLike] = None ) -> PlainQuantity[MagnitudeT]: ... @overload - def __new__(cls, value: str, units: UnitLike | None = None) -> PlainQuantity[Any]: + def __new__( + cls, value: str, units: Optional[UnitLike] = None + ) -> PlainQuantity[Any]: ... @overload def __new__( # type: ignore[misc] - cls, value: Sequence[ScalarT], units: UnitLike | None = None + cls, value: Sequence[ScalarT], units: Optional[UnitLike] = None ) -> PlainQuantity[Any]: ... @overload def __new__( - cls, value: PlainQuantity[Any], units: UnitLike | None = None + cls, value: PlainQuantity[Any], units: Optional[UnitLike] = None ) -> PlainQuantity[Any]: ... @@ -311,7 +322,7 @@ class PlainQuantity(Generic[MagnitudeT], PrettyIPython, SharedRegistryObject): return not bool(tmp.dimensionality) - _dimensionality: UnitsContainerT | None = None + _dimensionality: Optional[UnitsContainerT] = None @property def dimensionality(self) -> UnitsContainerT: @@ -406,7 +417,7 @@ class PlainQuantity(Generic[MagnitudeT], PrettyIPython, SharedRegistryObject): return self._REGISTRY.get_compatible_units(self._units) def is_compatible_with( - self, other: Any, *contexts: str | Context, **ctx_kwargs: Any + self, other: Any, *contexts: Union[str, Context], **ctx_kwargs: Any ) -> bool: """check if the other object is compatible @@ -463,7 +474,7 @@ class PlainQuantity(Generic[MagnitudeT], PrettyIPython, SharedRegistryObject): ) def ito( - self, other: QuantityOrUnitLike | None = None, *contexts, **ctx_kwargs + self, other: Optional[QuantityOrUnitLike] = None, *contexts, **ctx_kwargs ) -> None: """Inplace rescale to different units. @@ -484,7 +495,7 @@ class PlainQuantity(Generic[MagnitudeT], PrettyIPython, SharedRegistryObject): return None def to( - self, other: QuantityOrUnitLike | None = None, *contexts, **ctx_kwargs + self, other: Optional[QuantityOrUnitLike] = None, *contexts, **ctx_kwargs ) -> PlainQuantity: """Return PlainQuantity rescaled to different units. @@ -1257,7 +1268,7 @@ class PlainQuantity(Generic[MagnitudeT], PrettyIPython, SharedRegistryObject): def __abs__(self) -> PlainQuantity[MagnitudeT]: return self.__class__(abs(self._magnitude), self._units) - def __round__(self, ndigits: int | None = 0) -> PlainQuantity[MagnitudeT]: + def __round__(self, ndigits: Optional[int] = 0) -> PlainQuantity[MagnitudeT]: return self.__class__(round(self._magnitude, ndigits=ndigits), self._units) def __pos__(self) -> PlainQuantity[MagnitudeT]: diff --git a/pint/facets/plain/registry.py b/pint/facets/plain/registry.py index 3b26342..a6d7a13 100644 --- a/pint/facets/plain/registry.py +++ b/pint/facets/plain/registry.py @@ -30,6 +30,7 @@ from typing import ( Union, Generic, Generator, + Optional, ) from collections.abc import Iterable, Iterator @@ -80,7 +81,7 @@ _BLOCK_RE = re.compile(r"[ (]") @functools.lru_cache -def pattern_to_regex(pattern: str | re.Pattern[str]) -> re.Pattern[str]: +def pattern_to_regex(pattern: Union[str, re.Pattern[str]]) -> re.Pattern[str]: # TODO: This has been changed during typing improvements. # if hasattr(pattern, "finditer"): if not isinstance(pattern, str): @@ -188,7 +189,7 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): """ #: Babel.Locale instance or None - fmt_locale: Locale | None = None + fmt_locale: Optional[Locale] = None Quantity: type[QuantityT] Unit: type[UnitT] @@ -203,12 +204,12 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): force_ndarray_like: bool = False, on_redefinition: str = "warn", auto_reduce_dimensions: bool = False, - preprocessors: list[PreprocessorType] | None = None, - fmt_locale: str | None = None, + preprocessors: Optional[list[PreprocessorType]] = None, + fmt_locale: Optional[str] = None, non_int_type: NON_INT_TYPE = float, case_sensitive: bool = True, - cache_folder: str | pathlib.Path | None = None, - separate_format_defaults: bool | None = None, + cache_folder: Optional[Union[str, pathlib.Path]] = None, + separate_format_defaults: Optional[bool] = None, mpl_formatter: str = "{:P}", ): #: Map a definition class to a adder methods. @@ -265,7 +266,7 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): #: Map dimension name (string) to its definition (DimensionDefinition). self._dimensions: dict[ - str, DimensionDefinition | DerivedDimensionDefinition + str, Union[DimensionDefinition, DerivedDimensionDefinition] ] = {} #: Map unit name (string) to its definition (UnitDefinition). @@ -373,7 +374,7 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): """ return iter(sorted(self._units.keys())) - def set_fmt_locale(self, loc: str | None) -> None: + def set_fmt_locale(self, loc: Optional[str]) -> None: """Change the locale used by default by `format_babel`. Parameters @@ -402,7 +403,7 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): self.Measurement.default_format = value @property - def cache_folder(self) -> pathlib.Path | None: + def cache_folder(self) -> Optional[pathlib.Path]: if self._diskcache: return self._diskcache.cache_folder return None @@ -411,7 +412,7 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): def non_int_type(self): return self._non_int_type - def define(self, definition: str | type) -> None: + def define(self, definition: Union[str, type]) -> None: """Add unit to the registry. Parameters @@ -453,7 +454,7 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): self, definition: NamedDefinition, target_dict: dict[str, Any], - casei_target_dict: dict[str, Any] | None, + casei_target_dict: Optional[dict[str, Any]], ) -> None: """Helper function to store a definition in the internal dictionaries. It stores the definition under its name, symbol and aliases. @@ -479,7 +480,7 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): key: str, value: NamedDefinition, target_dict: dict[str, Any], - casei_target_dict: dict[str, Any] | None, + casei_target_dict: Optional[dict[str, Any]], ) -> None: """Helper function to store a definition in the internal dictionaries. @@ -529,7 +530,7 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): self._helper_adder(definition, self._units, self._units_casei) def load_definitions( - self, file: Iterable[str] | str | pathlib.Path, is_resource: bool = False + self, file: Union[Iterable[str], str, pathlib.Path], is_resource: bool = False ): """Add units and prefixes defined in a definition text file. @@ -600,7 +601,9 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): logger.warning(f"Could not resolve {unit_name}: {exc!r}") return self._cache - def get_name(self, name_or_alias: str, case_sensitive: bool | None = None) -> str: + def get_name( + self, name_or_alias: str, case_sensitive: Optional[bool] = None + ) -> str: """Return the canonical name of a unit.""" if name_or_alias == "dimensionless": @@ -637,7 +640,9 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): return unit_name - def get_symbol(self, name_or_alias: str, case_sensitive: bool | None = None) -> str: + def get_symbol( + self, name_or_alias: str, case_sensitive: Optional[bool] = None + ) -> str: """Return the preferred alias for a unit.""" candidates = self.parse_unit_name(name_or_alias, case_sensitive) if not candidates: @@ -666,7 +671,9 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): return self._get_dimensionality(input_units) - def _get_dimensionality(self, input_units: UnitsContainer | None) -> UnitsContainer: + def _get_dimensionality( + self, input_units: Optional[UnitsContainer] + ) -> UnitsContainer: """Convert a UnitsContainer to plain dimensions.""" if not input_units: return self.UnitsContainer() @@ -801,7 +808,7 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): except KeyError: pass - accumulators: dict[str | None, int] = defaultdict(int) + accumulators: dict[Optional[str], int] = defaultdict(int) accumulators[None] = 1 self._get_root_units_recurse(input_units, 1, accumulators) @@ -819,7 +826,10 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): return factor, units def get_base_units( - self, input_units: UnitsContainer | str, check_nonmult: bool = True, system=None + self, + input_units: Union[UnitsContainer, str], + check_nonmult: bool = True, + system=None, ) -> tuple[Number, UnitT]: """Convert unit or dict of units to the plain units. @@ -849,7 +859,7 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): # TODO: accumulators breaks typing list[int, dict[str, int]] # So we have changed the behavior here def _get_root_units_recurse( - self, ref: UnitsContainer, exp: Scalar, accumulators: dict[str | None, int] + self, ref: UnitsContainer, exp: Scalar, accumulators: dict[Optional[str], int] ) -> None: """ @@ -887,7 +897,7 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): # TODO: remove context from here def is_compatible_with( - self, obj1: Any, obj2: Any, *contexts: str | Context, **ctx_kwargs + self, obj1: Any, obj2: Any, *contexts: Union[str, Context], **ctx_kwargs ) -> bool: """check if the other object is compatible @@ -1008,7 +1018,7 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): return value def parse_unit_name( - self, unit_name: str, case_sensitive: bool | None = None + self, unit_name: str, case_sensitive: Optional[bool] = None ) -> tuple[tuple[str, str, str], ...]: """Parse a unit to identify prefix, unit name and suffix by walking the list of prefix and suffix. @@ -1033,7 +1043,7 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): ) def _parse_unit_name( - self, unit_name: str, case_sensitive: bool | None = None + self, unit_name: str, case_sensitive: Optional[bool] = None ) -> Generator[tuple[str, str, str], None, None]: """Helper of parse_unit_name.""" case_sensitive = ( @@ -1087,8 +1097,8 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): def parse_units( self, input_string: str, - as_delta: bool | None = None, - case_sensitive: bool | None = None, + as_delta: Optional[bool] = None, + case_sensitive: Optional[bool] = None, ) -> UnitT: """Parse a units expression and returns a UnitContainer with the canonical names. @@ -1121,7 +1131,7 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): self, input_string: str, as_delta: bool = True, - case_sensitive: bool | None = None, + case_sensitive: Optional[bool] = None, ) -> UnitsContainer: """Parse a units expression and returns a UnitContainer with the canonical names. @@ -1165,7 +1175,7 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): def _eval_token( self, token: TokenInfo, - case_sensitive: bool | None = None, + case_sensitive: Optional[bool] = None, **values: QuantityArgument, ): """Evaluate a single token using the following rules: @@ -1215,9 +1225,9 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): self, input_string: str, pattern: str, - case_sensitive: bool | None = None, + case_sensitive: Optional[bool] = None, many: bool = False, - ) -> list[str] | str | None: + ) -> Optional[Union[list[str], str]]: """Parse a string with a given regex pattern and returns result. Parameters @@ -1266,7 +1276,7 @@ class GenericPlainRegistry(Generic[QuantityT, UnitT], metaclass=RegistryMeta): def parse_expression( self: Self, input_string: str, - case_sensitive: bool | None = None, + case_sensitive: Optional[bool] = None, **values: QuantityArgument, ) -> QuantityT: """Parse a mathematical expression including units and return a quantity object. diff --git a/pint/facets/plain/unit.py b/pint/facets/plain/unit.py index 64a7d3c..4c5c04a 100644 --- a/pint/facets/plain/unit.py +++ b/pint/facets/plain/unit.py @@ -12,7 +12,7 @@ import copy import locale import operator from numbers import Number -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING, Any, Union from ..._typing import UnitLike from ...compat import NUMERIC_TYPES @@ -96,7 +96,7 @@ class PlainUnit(PrettyIPython, SharedRegistryObject): return self._REGISTRY.get_compatible_units(self) def is_compatible_with( - self, other: Any, *contexts: str | Context, **ctx_kwargs: Any + self, other: Any, *contexts: Union[str, Context], **ctx_kwargs: Any ) -> bool: """check if the other object is compatible diff --git a/pint/facets/system/definitions.py b/pint/facets/system/definitions.py index c334e9a..008abac 100644 --- a/pint/facets/system/definitions.py +++ b/pint/facets/system/definitions.py @@ -10,6 +10,7 @@ from __future__ import annotations from collections.abc import Iterable from dataclasses import dataclass +from typing import Optional from ...compat import Self from ... import errors @@ -24,7 +25,7 @@ class BaseUnitRule: new_unit_name: str #: name of the unit to be kicked out to make room for the new base uni #: If None, the current base unit with the same dimensionality will be used - old_unit_name: str | None = None + old_unit_name: Optional[str] = None # Instead of defining __post_init__ here, # it will be added to the container class @@ -46,7 +47,7 @@ class SystemDefinition(errors.WithDefErr): @classmethod def from_lines( cls: type[Self], lines: Iterable[str], non_int_type: type - ) -> Self | None: + ) -> Optional[Self]: # TODO: this is to keep it backwards compatible # TODO: check when is None returned. from ...delegates import ParserConfig, txt_defparser @@ -59,7 +60,7 @@ class SystemDefinition(errors.WithDefErr): return definition @property - def unit_replacements(self) -> tuple[tuple[str, str | None], ...]: + def unit_replacements(self) -> tuple[tuple[str, Optional[str]], ...]: # TODO: check if None can be dropped. return tuple((el.new_unit_name, el.old_unit_name) for el in self.rules) diff --git a/pint/facets/system/objects.py b/pint/facets/system/objects.py index cf6a24f..912094d 100644 --- a/pint/facets/system/objects.py +++ b/pint/facets/system/objects.py @@ -11,7 +11,7 @@ from __future__ import annotations import numbers -from typing import Any +from typing import Any, Optional from collections.abc import Iterable @@ -73,7 +73,7 @@ class System(SharedRegistryObject): #: Names of the _used_groups in used by this system. self._used_groups: set[str] = set() - self._computed_members: frozenset[str] | None = None + self._computed_members: Optional[frozenset[str]] = None # Add this system to the system dictionary self._REGISTRY._systems[self.name] = self @@ -154,7 +154,7 @@ class System(SharedRegistryObject): def from_definition( cls: type[System], system_definition: SystemDefinition, - get_root_func: GetRootUnits | None = None, + get_root_func: Optional[GetRootUnits] = None, ) -> System: if get_root_func is None: # TODO: kept for backwards compatibility diff --git a/pint/facets/system/registry.py b/pint/facets/system/registry.py index 30921bd..04aaea7 100644 --- a/pint/facets/system/registry.py +++ b/pint/facets/system/registry.py @@ -9,7 +9,7 @@ from __future__ import annotations from numbers import Number -from typing import TYPE_CHECKING, Generic, Any +from typing import TYPE_CHECKING, Generic, Any, Union, Optional from ... import errors @@ -53,17 +53,16 @@ class GenericSystemRegistry( # to enjoy typing goodies System: type[objects.System] - def __init__(self, system: str | None = None, **kwargs): + def __init__(self, system: Optional[str] = None, **kwargs): super().__init__(**kwargs) #: Map system name to system. - #: :type: dict[ str | System] self._systems: dict[str, objects.System] = {} #: Maps dimensionality (UnitsContainer) to Dimensionality (UnitsContainer) self._base_units_cache: dict[UnitsContainerT, UnitsContainerT] = {} - self._default_system_name: str | None = system + self._default_system_name: Optional[str] = system def _init_dynamic_classes(self) -> None: """Generate subclasses on the fly and attach them to self""" @@ -104,7 +103,7 @@ class GenericSystemRegistry( return objects.Lister(self._systems) @property - def default_system(self) -> str | None: + def default_system(self) -> Optional[str]: return self._default_system_name @default_system.setter @@ -144,9 +143,9 @@ class GenericSystemRegistry( def get_base_units( self, - input_units: UnitLike | Quantity, + input_units: Union[UnitLike, Quantity], check_nonmult: bool = True, - system: str | objects.System | None = None, + system: Optional[Union[str, objects.System]] = None, ) -> tuple[Number, Unit]: """Convert unit or dict of units to the plain units. @@ -184,7 +183,7 @@ class GenericSystemRegistry( self, input_units: UnitsContainerT, check_nonmult: bool = True, - system: str | objects.System | None = None, + system: Optional[Union[str, objects.System]] = None, ): if system is None: system = self._default_system_name @@ -226,7 +225,7 @@ class GenericSystemRegistry( return base_factor, destination_units def get_compatible_units( - self, input_units: UnitsContainerT, group_or_system: str | None = None + self, input_units: UnitsContainerT, group_or_system: Optional[str] = None ) -> frozenset[Unit]: """ """ @@ -242,7 +241,7 @@ class GenericSystemRegistry( return frozenset(self.Unit(eq) for eq in equiv) def _get_compatible_units( - self, input_units: UnitsContainerT, group_or_system: str | None = None + self, input_units: UnitsContainerT, group_or_system: Optional[str] = None ) -> frozenset[Unit]: if group_or_system and group_or_system in self._systems: members = self._systems[group_or_system].members |