summaryrefslogtreecommitdiff
path: root/pint/_typing.py
blob: 7a67efc455f3c932199538bb8c04703cbb877623 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Callable, TypeVar, Union, Protocol
from decimal import Decimal
from fractions import Fraction

from .compat import TypeAlias, Never

if TYPE_CHECKING:
    from .facets.plain import PlainQuantity as Quantity
    from .facets.plain import PlainUnit as Unit
    from .util import UnitsContainer


HAS_NUMPY = False
if TYPE_CHECKING:
    from .compat import HAS_NUMPY

if HAS_NUMPY:
    from .compat import np

    Scalar: TypeAlias = Union[float, int, Decimal, Fraction, np.number[Any]]
    Array = np.ndarray[Any, Any]
else:
    Scalar: TypeAlias = Union[float, int, Decimal, Fraction]
    Array: TypeAlias = Never

# TODO: Change when Python 3.10 becomes minimal version.
Magnitude = Union[Scalar, Array]

UnitLike = Union[str, dict[str, Scalar], "UnitsContainer", "Unit"]

QuantityOrUnitLike = Union["Quantity", UnitLike]

Shape = tuple[int, ...]

S = TypeVar("S")

FuncType = Callable[..., Any]
F = TypeVar("F", bound=FuncType)


# TODO: Improve or delete types
QuantityArgument = Any

T = TypeVar("T")


class Handler(Protocol):
    def __getitem__(self, item: type[T]) -> Callable[[T], None]:
        ...