Skip to content

Instantly share code, notes, and snippets.

@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)
@mypy-play
mypy-play / main.py
Created January 14, 2026 09:48
Shared via mypy Playground
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
@mypy-play
mypy-play / main.py
Created January 13, 2026 21:57
Shared via mypy Playground
from typing import NamedTuple, TypeVar
T = TypeVar("T")
GenericPoint = NamedTuple("GenericPoint", [("x", T), ("y", T)])
def _(p: GenericPoint[int]):
reveal_type(p[0])
reveal_type(p[1])
reveal_type(p.x)