Created
October 14, 2022 18:52
-
-
Save betafcc/48ee003f2dd39eefc91896258f742e26 to your computer and use it in GitHub Desktop.
Mypy type-level operators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
from typing import ( | |
Any, | |
Awaitable, | |
Callable, | |
Iterable, | |
Literal, | |
Mapping, | |
ParamSpec, | |
Type, | |
TypeAlias, | |
TypeVar, | |
Union, | |
overload, | |
) | |
from typing_extensions import TypeVarTuple, Unpack | |
A = TypeVar("A") | |
B = TypeVar("B") | |
E = TypeVar("E") | |
R = TypeVar("R") | |
S = TypeVar("S") | |
Vars = TypeVarTuple("Vars") | |
_ = TypeVar("_") | |
P = ParamSpec("P") | |
class typeofMeta(type): | |
def __getitem__(cls, value: A) -> Type[A]: | |
raise NotImplementedError | |
class typeof(metaclass=typeofMeta): | |
"""```python | |
def add(a: int, b: int) -> int: return a + b | |
reveal_type(Type[add]) | |
# Revealed type is builtins.object' | |
reveal_type(typeof[add]) | |
# Revealed type is 'def (builtins.int, builtins.int) -> builtins.int' | |
```""" | |
class InstanceTypeMeta(type): | |
def __getitem__(cls, value: Type[A]) -> A: | |
raise NotImplementedError | |
class InstanceType(metaclass=InstanceTypeMeta): | |
"""```python | |
class Box(Generic[A]): | |
value: A | |
reveal_type(InstanceType[Box[int]]) | |
# Revealed type is 'Box[builtins.int]' | |
```""" | |
class ReturnTypeMeta(type): | |
def __getitem__(cls, value: Type[Callable[..., A]]) -> Type[A]: | |
raise NotImplementedError | |
class ReturnType(metaclass=ReturnTypeMeta): | |
"""```python | |
def add(a: int, b: int) -> int: return a + b | |
reveal_type(ReturnType[add]) | |
# Revealed type is 'builtins.int' | |
```""" | |
class ParametersMeta(type): | |
def __getitem__(cls, value: Type[Callable[[Unpack[Vars]], Any]]) -> Type[tuple[Unpack[Vars]]]: | |
raise NotImplementedError | |
class Parameters(metaclass=ParametersMeta): | |
"""```python | |
def add(a: int, b: int) -> int: return a + b | |
reveal_type(Parameters[add]) | |
# Revealed type is 'Tuple[builtins.int, builtins.int]' | |
```""" | |
class AwaitedMeta(type): | |
def __getitem__(cls, value: Type[Awaitable[A]]) -> Type[A]: | |
raise NotImplementedError | |
class Awaited(metaclass=AwaitedMeta): | |
"""```python | |
async def add(a: int, b: int) -> int: return a + b | |
coro = add(1, 2) | |
reveal_type(Awaited[typeof[coro]]) | |
# Revealed type is "Type[builtins.int]" | |
```""" | |
class ItemOfMeta(type): | |
def __getitem__(cls, value: Type[Iterable[A]]) -> Type[A]: | |
raise NotImplementedError | |
class ItemOf(metaclass=ItemOfMeta): | |
""" | |
Get the type of the items of an iterable. | |
```python | |
reveal_type(ItemOf[list[int]]) | |
# Revealed type is 'Type[builtins.int]' | |
``` | |
""" | |
class IdMeta(type): | |
def __getitem__(cls, value: A) -> A: | |
raise NotImplementedError | |
class Id(metaclass=IdMeta): | |
... | |
class IdTypeMeta(type): | |
def __getitem__(cls, value: Type[A]) -> Type[A]: | |
raise NotImplementedError | |
class IdType(metaclass=IdTypeMeta): | |
... | |
class IterableValueTypeMeta(type): | |
def __getitem__(cls, value: Type[Iterable[A]]) -> Type[A]: | |
raise NotImplementedError | |
class IterableValueType(metaclass=IterableValueTypeMeta): | |
... | |
class IfMeta(type): | |
@overload | |
def __getitem__(cls, value: tuple[Literal[True], A, B]) -> A: | |
... | |
@overload | |
def __getitem__(cls, value: tuple[Literal[False], A, B]) -> B: | |
... | |
def __getitem__(cls, value: tuple[bool, A, B]) -> Union[A, B]: | |
... | |
class If(metaclass=IfMeta): | |
"""```python | |
reveal_type(InstanceType[If[True, int, str]]) | |
# Revealed type is 'builtins.int' | |
reveal_type(InstanceType[If[False, int, str]]) | |
# Revealed type is 'builtins.str' | |
```""" | |
class keyofMeta(type): | |
def __getitem__(cls, value: Type[Mapping[A, B]]) -> Type[A]: | |
raise NotImplementedError | |
class keyof(metaclass=keyofMeta): | |
"""` | |
Get key type of Mapping | |
```python | |
reveal_type(keyof[dict[str, int]]) | |
# Revealed type is 'builtins.str' | |
``` | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment