Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created March 4, 2026 21:20
Shared via mypy Playground
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Collection
@dataclass
class PreparedTask[I, R, O]:
input: I
reference: R
grader: Grader[I, R, O]
@mypy-play
mypy-play / main.py
Created March 4, 2026 19:42
Shared via mypy Playground
from typing import Self
def foo(x: int, y):
z: str = 0 # at least one param annotation, body is checked
reveal_type(x + y) # y treated as Any
def bar(x: int, y: int):
z: str = 0 # all params annotated, body is checked
reveal_type(x + y)
@mypy-play
mypy-play / main.py
Created March 4, 2026 14:18
Shared via mypy Playground
from collections.abc import Callable
from typing import Protocol
def x() -> Callable[[int], str]:
return lambda a: str(reveal_type(a))
class A(Protocol):
def __call__(self, a: int) -> str: ...
@mypy-play
mypy-play / main.py
Created March 4, 2026 11:20
Shared via mypy Playground
# mypy: enable-incomplete-feature=TypeForm
from typing import Annotated, Any, TypeVar, overload
from typing_extensions import assert_type, TypeForm
T = TypeVar("T")
@overload
def convert(value: Any) -> Any:
...
@mypy-play
mypy-play / main.py
Created March 4, 2026 10:51
Shared via mypy Playground
from typing import reveal_type
def foo() -> list[str]:
result = []
for i in range(0, 10):
result.append(str(i))
reveal_type(result)
return result
@mypy-play
mypy-play / main.py
Created March 4, 2026 07:58
Shared via mypy Playground
from typing import assert_type
import random
cond = random.randint(0, 1)
if cond:
def zip(*args) -> list[int]:
return []
def f(a: list[int], b: list[int]):
assert_type(zip([1], [2]), list[int])
@mypy-play
mypy-play / main.py
Created March 2, 2026 17:29
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 February 28, 2026 07:01
Shared via mypy Playground
class PmbArgs:
hook: int = 123
def install_hook(hook: str) -> None:
print(f"Installing {hook}")
args = PmbArgs()
install_hook(getattr(args, "hook"))
@mypy-play
mypy-play / main.py
Created February 27, 2026 21:27
Shared via mypy Playground
from typing import Sequence
vals: Sequence[int] = (1,2,3)
reveal_type(vals)
@mypy-play
mypy-play / main.py
Created February 27, 2026 15:45
Shared via mypy Playground
def foo(o: object) -> None:
assert isinstance(o, list)
def bar(o: object) -> None:
foo(o) # Ensures that o is a list
print(o[0])