Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Last active August 6, 2026 02:15
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 August 6, 2026 02:15
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 August 5, 2026 20:34
Shared via mypy Playground
from collections.abc import Sequence, Hashable
class DataFrame:
def __setitem__(
self, key: tuple[slice, Hashable], value: Sequence[int]
) -> None: ...
# mypy and ty error, pyrefly and pyright pass
DataFrame().__setitem__((slice(None, None, None), iter([0])), [1])
DataFrame()[:, iter([0])] = [1]
@mypy-play
mypy-play / main.py
Created August 5, 2026 13:46
Shared via mypy Playground
from typing import Literal
def signed(sign: Literal[-1,1], num: float) -> float:
return sign * num
assert signed(1, 3) == 3
assert signed(-1, 3) == -3
@mypy-play
mypy-play / main.py
Created August 5, 2026 12:03
Shared via mypy Playground
from typing import Iterator
def fib(n: float) -> Iterator[int]:
a, b = 0, 1
while a < n:
yield a
a, b = b, a + b
for zahl in fib(10):
print(zahl)
@mypy-play
mypy-play / main.py
Created August 5, 2026 11:44
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 August 4, 2026 19:49
Shared via mypy Playground
def _combine_tuple[*T](*args: *T) -> tuple[*T]:
return args
def foo():
bla = _combine_tuple(1, "foo", 6)
@mypy-play
mypy-play / main.py
Last active August 4, 2026 07:15
Shared via mypy Playground
from typing import Callable, reveal_type
def asdf[T](fn: Callable[[T], T]) -> Callable[[T], T]: ...
@asdf
def foo[T](value: T) -> T: ...
reveal_type(foo)
@mypy-play
mypy-play / main.py
Created August 3, 2026 11:48
Shared via mypy Playground
from typing import assert_never, TypeAlias, TypeGuard
A: TypeAlias = int | str | None
B: TypeAlias = A | complex
def _is_a(t: object) -> TypeGuard[A]:
return isinstance(t, (str, int)) or t is None
def f(t: B) -> object:
@mypy-play
mypy-play / main.py
Created August 3, 2026 10:12
Shared via mypy Playground
from typing import overload
class Tag: ...
Attrs = dict[str, str]
class Soup:
# e.g. find() / find(attr="value")
@overload
def find(self, name: None = None, attrs: None = None) -> Tag | None: ...