Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created May 24, 2024 20:34
Shared via mypy Playground
# mypy: allow-any-generics
from typing import Generator, Any
def f() -> Generator:
x: str = yield
return x
def g() -> Generator[Any, Any, Any]:
x: str = yield
return x
@mypy-play
mypy-play / main.py
Created May 24, 2024 19:43
Shared via mypy Playground
from __future__ import annotations
from typing import TypeVar, Concatenate, ParamSpec, Any
from collections.abc import Callable
F = TypeVar("F", bound = Callable[..., Any])
P = ParamSpec("P")
R = TypeVar("R")
S = TypeVar("S")
@mypy-play
mypy-play / main.py
Created May 24, 2024 18:15
Shared via mypy Playground
from __future__ import annotations
import datetime
import functools
import inspect
from typing import (
TYPE_CHECKING,
Any,
Concatenate,
Generic,
@mypy-play
mypy-play / main.py
Created May 24, 2024 17:37
Shared via mypy Playground
from typing_extensions import TypeIs
from typing import Any, Awaitable, TypeVar, Union
T = TypeVar('T')
def is_awaitable(val: object) -> TypeIs[Awaitable[Any]]: pass
def main(a: Union[Awaitable[T], T]) -> None:
if is_awaitable(a):
reveal_type(a) # N: Revealed type is "Union[typing.Awaitable[T`-1], typing.Awaitable[Any]]"
else:
reveal_type(a) # N: Revealed type is "T`-1"
@mypy-play
mypy-play / main.py
Created May 24, 2024 14:50
Shared via mypy Playground
# https://peps.python.org/pep-0646/
from typing import Generic, Tuple, List, TypeVarTuple, Unpack
class A:
...
@mypy-play
mypy-play / main.py
Created May 24, 2024 14:30
Shared via mypy Playground
import typing
from typing import Any, Never, overload
class NDArray:
shape: tuple
def __setitem__(self, idx: NDArray, v: object) -> None: ...
def random(x: object) -> NDArray: return NDArray()
Image = typing.NewType('Image', NDArray)
@mypy-play
mypy-play / main.py
Created May 24, 2024 11:14
Shared via mypy Playground
from typing import overload, Literal
@overload
def execute_command(command: str,
additional_arguments: str = '',
*,
capture_output: Literal[True],
) -> str:
...
@mypy-play
mypy-play / main.py
Created May 24, 2024 11:11
Shared via mypy Playground
from typing import overload, Literal
@overload
def execute_command(command: str,
additional_arguments: str = '',
*,
capture_output: Literal[True],
) -> str:
...
@mypy-play
mypy-play / main.py
Created May 24, 2024 11:08
Shared via mypy Playground
from typing import overload, Literal
class Foo:
@overload
def some_method(self, bar: str = '', *, flag: Literal[True]) -> int:
...
@overload
def some_method(self, bar: str = '', *, flag: Literal[False]) -> list[int]:
@mypy-play
mypy-play / main.py
Created May 24, 2024 08:30
Shared via mypy Playground
from typing import TypeVar
from abc import ABC
class A(ABC):
def m(self) -> None:
print("A")
class B(A):
def m(self) -> None:
print("B")