Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created December 6, 2025 13:19
Shared via mypy Playground
# from ib111 import week_11 # noqa
# Napište čistou funkci, která na základě daného vzoru vytvoří
# množinu všech odpovídajících řetězců. Vzor je tvořený
# alfanumerickými znaky a navíc může obsahovat hranaté závorky –
# znaky ‹[› a ‹]›. Mezi těmito závorkami může stát libovolný počet
# přípustných znaků (krom samotných hranatých závorek) a na daném
# místě se ve výsledném řetězci může nacházet libovolný z těchto
# znaků. Například vzor ‹a[bc]d› reprezentuje řetězce ‹abd› a ‹acd›.
@mypy-play
mypy-play / main.py
Created December 6, 2025 00:34
Shared via mypy Playground
from typing import Generic, TypeVar, ParamSpec, Protocol
# ─────────────────────────────────────────────────────────────
# Base type parameters
# ─────────────────────────────────────────────────────────────
P = ParamSpec("P") # Params for __call__ and run
O = TypeVar("O") # Raw output type
S = TypeVar("S", bound=dict) # State type
@mypy-play
mypy-play / main.py
Created December 5, 2025 23:59
Shared via mypy Playground
class C:
@property
def type(self) -> int:
return 5
class D(C):
type = 6
@mypy-play
mypy-play / main.py
Created December 5, 2025 22:54
Shared via mypy Playground
import re
import types
def fun(val: str | re.Pattern | None):
match val:
case str():
print('string')
case re.Pattern():
print('pattern')
case types.NoneType():
@mypy-play
mypy-play / main.py
Created December 5, 2025 10:44
Shared via mypy Playground
from typing import Final
def foo(foo: Foo[int | str]) -> None:
pass
def bar(bar: Bar[int | str]) -> None:
pass
@mypy-play
mypy-play / main.py
Created December 5, 2025 05:26
Shared via mypy Playground
from typing import Callable
class Foo[**P]:
def __init__(self, fn: Callable[P, int], *args: P.args, **kwargs: P.kwargs) -> None:
self.fn = fn
self.args = args
self.kwargs = kwargs
def fn(x: int, y: str) -> int:
return 1
@mypy-play
mypy-play / main.py
Created December 5, 2025 04:52
Shared via mypy Playground
from typing import Any
class Foo[**P, T]: ...
reveal_type(Foo[Any, int])
@mypy-play
mypy-play / main.py
Created December 4, 2025 18:47
Shared via mypy Playground
def foo(variants: dict[str, dict[str, str]] = None) -> str:
return "hello"
@mypy-play
mypy-play / main.py
Created December 4, 2025 18:40
Shared via mypy Playground
from typing import Self, Protocol
class Comparable(Protocol):
def __lt__(self, other: Self, /) -> bool:
...
def funny(c: bool) -> Comparable:
if c:
return ('', '')
return ('', None)
@mypy-play
mypy-play / main.py
Created December 4, 2025 17:38
Shared via mypy Playground
from datetime import date, datetime, timedelta
from typing_extensions import Any, Generic, TypeVar
_AnyDateT = TypeVar("_AnyDateT", date, datetime)
_DateT_co = TypeVar("_DateT_co", bound=date, covariant=True)
class datetime64(Generic[_DateT_co]):
def __sub__(self: datetime64[_AnyDateT], other: _AnyDateT, /) -> timedelta: ... # type: ignore[empty-body]
def repro(d: datetime64[Any]):