Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created November 8, 2026 09:36
Shared via mypy Playground
def f(items: list[str]) -> None:
for item in items:
if item == "a":
x = 1
else:
print(x)
@mypy-play
mypy-play / main.py
Created November 8, 2026 09:27
Shared via mypy Playground
import os
def f(u: str | os.PathLike[str]) -> str:
if u and (u := str(u)):
return u
return ""
@mypy-play
mypy-play / main.py
Created November 7, 2026 23:42
Shared via mypy Playground
from dataclasses import dataclass
from typing import Final
@dataclass
class Wrapper[T]:
obj: Final[T]
@mypy-play
mypy-play / main.py
Created November 7, 2026 23:36
Shared via mypy Playground
from typing import Generic, Literal, overload
from typing_extensions import TypeVar
_S = TypeVar("_S", contravariant=True)
_G = TypeVar("_G", covariant=True)
class Base(Generic[_S, _G]):
@overload
def __init__(self, *, null: Literal[False] = False) -> None: ...
@mypy-play
mypy-play / main.py
Created November 6, 2026 16:43
Shared via mypy Playground
from typing import Optional
class A:
pass
def fib() -> Optional[A]:
return
@mypy-play
mypy-play / main.py
Created November 6, 2026 15:37
Shared via mypy Playground
from typing import assert_type
class Base:
def __lt__(self, other: "Base") -> str:
return "lt"
class Child(Base):
def __gt__(self, other: "Child") -> int:
@mypy-play
mypy-play / main.py
Created November 6, 2026 15:15
Shared via mypy Playground
from dataclasses import InitVar, dataclass
from typing import overload
@dataclass
class Record:
value: InitVar[int]
@overload
def __post_init__(self, value: int) -> None: ...
@mypy-play
mypy-play / main.py
Created November 5, 2026 21:48
Shared via mypy Playground
from typing import assert_type
class LeftMeta(type):
def __mul__(cls, other: object) -> int:
return 1
class RightMeta(LeftMeta):
def __rmul__(cls, other: object) -> str:
@mypy-play
mypy-play / main.py
Created November 5, 2026 18:41
Shared via mypy Playground
from collections.abc import Callable
from typing import assert_type
class Foo[*Ts, **P = [float, bool]]:
def args(self) -> tuple[*Ts]:
raise NotImplementedError
def fn(self) -> Callable[P, None]:
raise NotImplementedError
@mypy-play
mypy-play / main.py
Created November 4, 2026 21:59
Shared via mypy Playground
from types import EllipsisType
from typing import overload
@overload
def func(): ...
@overload
def func(arg: int): ...
def func(arg: int | EllipsisType = ...):
if arg is ...: