Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created April 21, 2026 22:05
Shared via mypy Playground
from typing import Generic
from typing import NamedTuple
from typing import TypeVar
T = TypeVar("T")
class Missing:
def __repr__(self):
return "MISSING"
@mypy-play
mypy-play / main.py
Created April 21, 2026 11:48
Shared via mypy Playground
from typing import TypeVar, Literal, Generic, overload, Any, reveal_type, Self, Never
import uuid
_ST = TypeVar("_ST", contravariant=True)
_GT = TypeVar("_GT", covariant=True)
_NT = TypeVar("_NT", Literal[True], Literal[False], default=Literal[False])
class Model:
pass
@mypy-play
mypy-play / main.py
Created April 21, 2026 11:06
Shared via mypy Playground
def foo() -> list[bool, int]:
return [False, 0]
def bar() -> list[bool | int]:
return [False, 0]
def baz() -> tuple[bool, int]:
return (False, 0)
@mypy-play
mypy-play / main.py
Created April 21, 2026 08:13
Shared via mypy Playground
from typing import TypeVar
T = TypeVar("T")
def create_store(initial: T, fallback: T) -> T:
return initial if initial is not None else fallback
# mypy/pyright NIE zgłosi błędu — zachowuje się podobnie do TS bez NoInfer
store = create_store("hello", 42) # T = str | int, brak błędu
@mypy-play
mypy-play / main.py
Created April 21, 2026 08:07
Shared via mypy Playground
def checking_tyoe(a):
return a
print(checking_tyoe(12))
@mypy-play
mypy-play / main.py
Created April 20, 2026 16:48
Shared via mypy Playground
def what_now(x=1.23):
reveal_type(x)
@mypy-play
mypy-play / main.py
Created April 20, 2026 16:13
Shared via mypy Playground
from collections.abc import Callable
from typing import NamedTuple
class MyBaseClass: ...
class NameBox[T: MyBaseClass]: ...
class InfoHolder[T: MyBaseClass, U: NameBox](NamedTuple):
box_cls: type[U] # This should be `type[U[T]]`
human_name: str
@mypy-play
mypy-play / main.py
Created April 20, 2026 13:52
Shared via mypy Playground
from typing import Iterable
def foo(args: Iterable[str | int] | str | int) -> Iterable[str]:
if isinstance(args, (str, int)):
args = (args,)
args = (
arg if isinstance(arg, str) else str(arg)
for arg in args
)
return args
@mypy-play
mypy-play / main.py
Created April 19, 2026 17:07
Shared via mypy Playground
from collections.abc import Callable
from enum import Enum
from fractions import Fraction
from math import ceil, floor
RoundingCallType = Callable[[Fraction], int]
def floor_method(number: Fraction) -> int:
return floor(number)
@mypy-play
mypy-play / main.py
Created April 19, 2026 13:27
Shared via mypy Playground
from typing import overload, Unpack
@overload
def f(x: tuple[int]) -> int: ...
@overload
def f(x: tuple[int, int, Unpack[tuple[int, ...]]]) -> list[int]: ...
def f(x: tuple[int, ...]) -> int | list[int]:
if len(x) == 1:
return x[0]
return list(x)