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

Start Frontend experiment #1882

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pint/facets/plain/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def dimensionality(self) -> UnitsContainerT:
Dimensionality of the PlainQuantity, e.g. ``{length: 1, time: -1}``
"""
if self._dimensionality is None:
self._dimensionality = self._REGISTRY._get_dimensionality(self._units)
self._dimensionality = self._REGISTRY.get_dimensionality(self._units)

return self._dimensionality

Expand Down
45 changes: 31 additions & 14 deletions pint/facets/plain/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
from collections import defaultdict
from decimal import Decimal
from fractions import Fraction
from numbers import Number
from token import NAME, NUMBER
from tokenize import TokenInfo

Expand Down Expand Up @@ -149,14 +148,14 @@ class RegistryMeta(type):
instead of asking the developer to do it when subclassing.
"""

def __call__(self, *args, **kwargs):
def __call__(self, *args: Any, **kwargs: Any):
obj = super().__call__(*args, **kwargs)
obj._after_init()
return obj


# Generic types used to mark types associated to Registries.
QuantityT = TypeVar("QuantityT", bound=PlainQuantity)
QuantityT = TypeVar("QuantityT", bound=PlainQuantity[Any])
UnitT = TypeVar("UnitT", bound=PlainUnit)


Expand Down Expand Up @@ -739,7 +738,9 @@ def _get_dimensionality_recurse(
if reg.reference is not None:
self._get_dimensionality_recurse(reg.reference, exp2, accumulator)

def _get_dimensionality_ratio(self, unit1: UnitLike, unit2: UnitLike):
def _get_dimensionality_ratio(
self, unit1: UnitLike, unit2: UnitLike
) -> Scalar | None:
"""Get the exponential ratio between two units, i.e. solve unit2 = unit1**x for x.

Parameters
Expand Down Expand Up @@ -773,7 +774,7 @@ def _get_dimensionality_ratio(self, unit1: UnitLike, unit2: UnitLike):

def get_root_units(
self, input_units: UnitLike, check_nonmult: bool = True
) -> tuple[Number, UnitT]:
) -> tuple[Scalar, UnitT]:
"""Convert unit or dict of units to the root units.

If any unit is non multiplicative and check_converter is True,
Expand Down Expand Up @@ -854,7 +855,7 @@ def get_base_units(
input_units: Union[UnitsContainer, str],
check_nonmult: bool = True,
system=None,
) -> tuple[Number, UnitT]:
) -> tuple[Scalar, UnitT]:
"""Convert unit or dict of units to the plain units.

If any unit is non multiplicative and check_converter is True,
Expand Down Expand Up @@ -1062,17 +1063,19 @@ def parse_unit_name(
tuple of tuples (str, str, str)
all non-equivalent combinations of (prefix, unit name, suffix)
"""

case_sensitive = (
self.case_sensitive if case_sensitive is None else case_sensitive
)
return self._dedup_candidates(
self._yield_unit_triplets(unit_name, case_sensitive=case_sensitive)
self._yield_unit_triplets(unit_name, case_sensitive)
)

def _yield_unit_triplets(
self, unit_name: str, case_sensitive: Optional[bool] = None
self, unit_name: str, case_sensitive: bool
) -> Generator[tuple[str, str, str], None, None]:
"""Helper of parse_unit_name."""
case_sensitive = (
self.case_sensitive if case_sensitive is None else case_sensitive
)

stw = unit_name.startswith
edw = unit_name.endswith
for suffix, prefix in itertools.product(self._suffixes, self._prefixes):
Expand Down Expand Up @@ -1147,11 +1150,25 @@ def parse_units(
pint.Unit

"""
return self.Unit(
self.parse_units_as_container(input_string, as_delta, case_sensitive)
)

units = self._parse_units(input_string, as_delta, case_sensitive)
return self.Unit(units)
def parse_units_as_container(
self,
input_string: str,
as_delta: Optional[bool] = None,
case_sensitive: Optional[bool] = None,
) -> UnitT:
as_delta = (
as_delta if as_delta is not None else self.default_as_delta
) # TODO This only exists in nonmultiplicative
case_sensitive = (
case_sensitive if case_sensitive is not None else self.case_sensitive
)
return self._parse_units_as_container(input_string, as_delta, case_sensitive)

def _parse_units(
def _parse_units_as_container(
self,
input_string: str,
as_delta: bool = True,
Expand Down
2 changes: 1 addition & 1 deletion pint/facets/plain/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def dimensionality(self) -> UnitsContainer:
try:
return self._dimensionality
except AttributeError:
dim = self._REGISTRY._get_dimensionality(self._units)
dim = self._REGISTRY.get_dimensionality(self._units)
self._dimensionality = dim

return self._dimensionality
Expand Down
Loading
Loading