Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created March 17, 2026 21:03
Shared via mypy Playground
import typing
def iterator() -> typing.Iterator[int]:
x = yield 10
# The `Iterator` return type here is a supertype of `Generator`.
# It has no "send" type, nor a send method, so this should always be `None`
reveal_type(x) # revealed: None
@mypy-play
mypy-play / main.py
Created March 17, 2026 13:42
Shared via mypy Playground
def foo() -> int:
raise Exception()
def optional_import_func():
try:
x = foo()
except ImportError:
pass
print(x)
@mypy-play
mypy-play / main.py
Created March 17, 2026 01:54
Shared via mypy Playground
from typing import TypedDict
@abc
@efg.hij
@klm[nop]
@qrs.tuv[wxy]
class A(TypedDict):
pass
@mypy-play
mypy-play / main.py
Created March 16, 2026 23:05
Shared via mypy Playground
from typing import overload, Callable
@overload
def decorator[T](cls: type[T], /, *, extra: int = 0) -> type[T]:
...
@overload
def decorator[T](cls: None = None, /, *, extra: int = 0) -> Callable[[type[T]], type[T]]:
...
@mypy-play
mypy-play / main.py
Created March 16, 2026 18:44
Shared via mypy Playground
from typing import Never, Protocol, reveal_type
class ReturnsT[T](Protocol):
def get(self) -> T: ...
class ReturnsNever:
def get(self) -> Never:
raise RuntimeError
def through_protocol[T](x: ReturnsT[T]) -> T:
@mypy-play
mypy-play / main.py
Created March 16, 2026 10:23
Shared via mypy Playground
from typing import Iterator
def fib(n) -> Iterator[int]:
a, b = 0, 1
while a < n:
yield a
a, b = b, a + b
@mypy-play
mypy-play / main.py
Created March 15, 2026 20:56
Shared via mypy Playground
print()
@mypy-play
mypy-play / main.py
Created March 15, 2026 20:14
Shared via mypy Playground
class Hard:
pass
class Soft:
pass
lst: list[Hard] | list[Soft]
lst = [val for val in lst]
@mypy-play
mypy-play / main.py
Created March 15, 2026 15:12
Shared via mypy Playground
class Hard:
pass
class Soft:
pass
lst: list[Hard] | list[Soft]
lst = [val for val in lst]
@mypy-play
mypy-play / main.py
Created March 15, 2026 12:58
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