Skip to content

Commit

Permalink
more typing
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgsavage committed Jan 19, 2025
1 parent 5aee334 commit f6a7425
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 12 deletions.
146 changes: 146 additions & 0 deletions .mypy

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions pint/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@


class Handler(Protocol):
@overload
def __getitem__(self, Never, /) -> Never:
...

@overload
def __getitem__(self, item: type[T]) -> Callable[[T], None]:
...
4 changes: 3 additions & 1 deletion pint/delegates/txt_defparser/defparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ class DefParser:
plain.CommentDefinition,
)

def __init__(self, default_config: ParserConfig, diskcache: fc.DiskCache) -> None:
def __init__(
self, default_config: ParserConfig, diskcache: fc.DiskCache | None
) -> None:
self._default_config = default_config
self._diskcache = diskcache

Expand Down
2 changes: 2 additions & 0 deletions pint/facets/nonmultiplicative/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def _add_unit(self, definition: UnitDefinition) -> None:
"delta_" + alias for alias in definition.aliases
)

assert isinstance(definition.reference, UnitsContainer)
delta_reference = self.UnitsContainer(
{ref: value for ref, value in definition.reference.items()}
)
Expand Down Expand Up @@ -198,6 +199,7 @@ def _add_ref_of_log_or_offset_unit(

# TODO: Check that reference is None

assert isinstance(slct_ref, UnitsContainer)
# If reference unit is not dimensionless
if slct_ref != UnitsContainer():
# Extract reference unit
Expand Down
12 changes: 8 additions & 4 deletions pint/facets/plain/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import typing as ty
from dataclasses import dataclass
from functools import cached_property
from typing import Any
from typing import Any, no_type_check

from ... import errors
from ..._typing import Magnitude
Expand Down Expand Up @@ -121,7 +121,7 @@ class UnitDefinition(NamedDefinition, errors.WithDefErr):
defined_symbol: str | None
#: additional names for the same unit
aliases: tuple[str, ...]
#: A functiont that converts a value in these units into the reference units
#: A function that converts a value in these units into the reference units
# TODO: this has changed as converter is now annotated as converter.
# Briefly, in several places converter attributes like as_multiplicative were
# accesed. So having a generic function is a no go.
Expand Down Expand Up @@ -192,7 +192,7 @@ def is_base(self) -> bool:
"""Indicates if it is a base unit."""

# TODO: This is set in __post_init__
return self._is_base
return self._is_base # type: ignore[attr-defined]

@property
def is_multiplicative(self) -> bool:
Expand Down Expand Up @@ -283,8 +283,10 @@ def __post_init__(self):
class ScaleConverter(Converter):
"""A linear transformation without offset."""

scale: float
scale: numbers.Number

# typing non_int_type related
@no_type_check
def to_reference(self, value: Magnitude, inplace: bool = False) -> Magnitude:
if inplace:
value *= self.scale
Expand All @@ -293,6 +295,8 @@ def to_reference(self, value: Magnitude, inplace: bool = False) -> Magnitude:

return value

# typing non_int_type related
@no_type_check
def from_reference(self, value: Magnitude, inplace: bool = False) -> Magnitude:
if inplace:
value /= self.scale
Expand Down
7 changes: 4 additions & 3 deletions pint/facets/plain/qto.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import math
import numbers
import warnings
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, no_type_check

from ...compat import (
mip_INF,
Expand Down Expand Up @@ -193,7 +193,7 @@ def to_preferred(

def ito_preferred(
quantity: PlainQuantity, preferred_units: list[UnitLike] | None = None
) -> PlainQuantity:
) -> None:
"""Return Quantity converted to a unit composed of the preferred units.
Examples
Expand All @@ -211,9 +211,10 @@ def ito_preferred(
return quantity.ito(units)


@no_type_check
def _get_preferred(
quantity: PlainQuantity, preferred_units: list[UnitLike] | None = None
) -> PlainQuantity:
) -> UnitsContainer:
if preferred_units is None:
preferred_units = quantity._REGISTRY.default_preferred_units

Expand Down
1 change: 0 additions & 1 deletion pint/facets/plain/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,6 @@ def _build_cache(self, loaded_files=None) -> None:

except Exception as exc:
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:
"""Return the canonical name of a unit."""
Expand Down
1 change: 1 addition & 0 deletions pint/facets/system/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def from_lines(
for definition in parser.iter_parsed_project(pp):
if isinstance(definition, cls):
return definition
return None

@property
def unit_replacements(self) -> tuple[tuple[str, str | None], ...]:
Expand Down
6 changes: 3 additions & 3 deletions pint/facets/system/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
if TYPE_CHECKING:
from ..._typing import Quantity, Unit

from ..._typing import UnitLike
from ..._typing import QuantityOrUnitLike, UnitLike
from ...util import UnitsContainer as UnitsContainerT
from ...util import (
create_class_with_registry,
Expand Down Expand Up @@ -223,7 +223,7 @@ def _get_base_units(
return base_factor, destination_units

def get_compatible_units(
self, input_units: UnitsContainerT, group_or_system: str | None = None
self, input_units: QuantityOrUnitLike, group_or_system: str | None = None
) -> frozenset[Unit]:
""" """

Expand All @@ -240,7 +240,7 @@ def get_compatible_units(

def _get_compatible_units(
self, input_units: UnitsContainerT, group_or_system: str | None = None
) -> frozenset[Unit]:
) -> frozenset[str]:
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
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,4 @@ exclude = [
"pint/default_en.txt",
"pint/constants_en.txt",
]
disable_error_code = "operator,override"

0 comments on commit f6a7425

Please sign in to comment.