Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created January 16, 2026 01:45
Shared via mypy Playground
from logging.config import _FormatterConfigurationTypedDict
# This should error - "()" is not a valid key
formatter_config: _FormatterConfigurationTypedDict = {
"()": "logging.Formatter",
"format": "%(message)s",
}
@mypy-play
mypy-play / main.py
Created January 15, 2026 23:51
Shared via mypy Playground
from typing import final, assert_never, reveal_type
class Types:
@final
class A:
pass
@final
class B:
@mypy-play
mypy-play / main.py
Created January 14, 2026 22:00
Shared via mypy Playground
import ast
class ComparisonSwapper(ast.NodeTransformer):
OP_MAP = {
ast.Lt: ast.GtE,
ast.GtE: ast.Lt,
}
def visit_Compare(self, node: ast.AST) -> ast.AST:
new_ops = [self.OP_MAP.get(type(op), type(op))() for op in node.ops]
@mypy-play
mypy-play / main.py
Created January 14, 2026 19:39
Shared via mypy Playground
from functools import cached_property
class Foo:
def awesome(self):
...
class Bar:
@cached_property
def client(self) -> Foo:
return Foo()
@mypy-play
mypy-play / main.py
Created January 14, 2026 16:51
Shared via mypy Playground
class Shape:
def area(self) -> float:
return 1.0
class Circle(Shape):
def radius(self) -> float:
return 2.0
# T is constrained to be Shape or a subclass of Shape
def total_area[T: Shape](shapes: tuple[T, ...]) -> float:
@mypy-play
mypy-play / main.py
Created January 14, 2026 15:36
Shared via mypy Playground
from typing import TypedDict
from typing import ReadOnly
from typing import Required
class GitParameters_A(TypedDict, total=False):
git_projectcollection_url: str
git_commit_hash: str
git_branch: str
git_repository_id: Required[str]
@mypy-play
mypy-play / main.py
Created January 14, 2026 13:00
Shared via mypy Playground
from typing import Callable, Iterable, Protocol, Concatenate, overload
class Transformer[P, B]:
pass
@overload
def partial_transformer[T1, T2, T3, T4, **P, O](func: Callable[Concatenate[T1, T2, T3, T4, P], O]) -> Callable[P, Transformer[tuple[T1, T2, T3, T4], O]]:
pass
@overload
@mypy-play
mypy-play / main.py
Created January 14, 2026 12:16
Shared via mypy Playground
from typing import Iterator
def fib(n: int) -> Iterator[int]:
a, b = 0, 1
while a < n:
yield a
a, b = b, a + b
@mypy-play
mypy-play / main.py
Created January 14, 2026 10:18
Shared via mypy Playground
from typing import reveal_type
class C:
def __new__(cls):
reveal_type(cls)
return super().__new__(cls)
@mypy-play
mypy-play / main.py
Created January 14, 2026 10:17
Shared via mypy Playground
from typing import Self, reveal_type
class C:
def __new__(cls) -> Self:
reveal_type(cls)
return super().__new__(cls)