Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created April 25, 2024 14:17
Shared via mypy Playground
# Sentinel to denote unset values
UNSET = object()
def foo(timezone: str | object | None = UNSET) -> None:
pass
if __name__ == "__main__":
foo()
@mypy-play
mypy-play / main.py
Created April 25, 2024 12:26
Shared via mypy Playground
from typing import TypeVar
from typing_extensions import TypeIs
_T = TypeVar('_T', str, int)
def is_str(val: str | int) -> TypeIs[str]:
return isinstance(val, str)
@mypy-play
mypy-play / main.py
Created April 25, 2024 10:55
Shared via mypy Playground
from typing import TypeVar, TypedDict, Generic
VarT = TypeVar("VarT", bound=dict)
class MyCls(Generic[VarT]):
def sneed(self, t: VarT):
print(t)
class MyDict(TypedDict):
a: int
@mypy-play
mypy-play / main.py
Created April 25, 2024 08:34
Shared via mypy Playground
import typing
SingleItem = typing.Tuple[int]
MultipleItems = typing.Tuple[int, ...]
def test(a: SingleItem, b: MultipleItems) -> None:
return
@mypy-play
mypy-play / main.py
Created April 25, 2024 08:21
Shared via mypy Playground
from typing import TypeVar, Generic, Any, cast, overload
from abc import abstractmethod, ABC
_TP = TypeVar("_TP", bound=tuple[Any, ...])
class Select(Generic[_TP]):
pass
@mypy-play
mypy-play / main.py
Created April 25, 2024 08:18
Shared via mypy Playground
from typing import TypeVar, Generic, Any, cast
from abc import abstractmethod, ABC
_TP = TypeVar("_TP", bound=tuple[Any, ...])
_TM = TypeVar("_TM")
class Select(Generic[_TP]):
pass
@mypy-play
mypy-play / main.py
Created April 25, 2024 08:18
Shared via mypy Playground
from typing import TypeVar, Generic, Any, cast
from abc import abstractmethod, ABC
_TP = TypeVar("_TP", bound=tuple[Any, ...])
_TM = TypeVar("_TM")
class Select(Generic[_TP]):
pass
@mypy-play
mypy-play / main.py
Created April 24, 2024 23:21
Shared via mypy Playground
import typing
def f() -> typing.Annotated[int, "this is da' sh..."]:
return 0
x: int = f()
reveal_type(f())
@mypy-play
mypy-play / main.py
Created April 24, 2024 23:20
Shared via mypy Playground
import typing
def f() -> typing.Annotated[int, "this is da' sh..."]:
return 0
x: int = f()
@mypy-play
mypy-play / main.py
Created April 24, 2024 18:00
Shared via mypy Playground
from typing import TypeVar
from typing_extensions import TypeIs
_T = TypeVar('_T', str, int)
def is_str(val: str | int) -> TypeIs[str]:
return isinstance(val, str)