Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created September 2, 2025 12:17
Shared via mypy Playground
from typing import reveal_type
def non_optional[T](value: T|None)->T:
assert value is not None
return value
def f(x: int|None):
reveal_type(x)
y = non_optional(x)
reveal_type(y)
@mypy-play
mypy-play / main.py
Created September 2, 2025 11:05
Shared via mypy Playground
options: list[tuple[int, *tuple[str, ...]]] = [(1, 'a'), (2, 'b', 'c')]
for i, *s in options:
print(hex(i), '_'.join(s))
@mypy-play
mypy-play / main.py
Created September 2, 2025 10:44
Shared via mypy Playground
for i, s in [(1, ('a',)), (2, ('b', 'c'))]:
print(hex(i), '_'.join(s))
@mypy-play
mypy-play / main.py
Created September 2, 2025 10:39
Shared via mypy Playground
options: list[tuple[int, *tuple[str, ...]]] = [(1, 'a'), (2, 'b', 'c')]
for i, *s in options:
print(hex(i), '_'.join(s))
@mypy-play
mypy-play / main.py
Created September 2, 2025 10:37
Shared via mypy Playground
for i, *s in [(1, 'a'), (2, 'b', 'c')]:
print(hex(i), '_'.join(s))
@mypy-play
mypy-play / main.py
Created September 2, 2025 10:19
Shared via mypy Playground
from typing import Any, cast
i: int
s: tuple[str]
for i, *s in cast(tuple[Any, ...], [(1, 'a'), (2, 'b', 'c')]):
print(hex(i), '_'.join(s))
@mypy-play
mypy-play / main.py
Created September 2, 2025 10:19
Shared via mypy Playground
from typing import Any, cast
i: int
s: tuple[str]
for i, *s in cast(tuple[Any, ...], [(1, 'a'), (2, 'b', 'c')]):
print(hex(i), '_'.join(s))
@mypy-play
mypy-play / main.py
Created September 2, 2025 02:40
Shared via mypy Playground
import asyncio
from typing import Awaitable, AsyncGenerator
async def foo(*coros: Awaitable[None]) -> bool:
gen = (False for f in coros if not await f)
# Revealed type is "typing.Generator[builtins.bool, None, None]"
# Runtime type is 'async_generator'
reveal_type(gen)
return await anext(gen)
@mypy-play
mypy-play / main.py
Created September 1, 2025 08:44
Shared via mypy Playground
from typing import Any, cast
# A skeleton class based on Django's BaseForm:
# https://github.com/typeddjango/django-stubs/blob/master/django-stubs/forms/forms.pyi
class BaseForm:
cleaned_data: dict[str, Any] = {}
def clean(self) -> dict[str, Any] | None:
return self.cleaned_data
@mypy-play
mypy-play / main.py
Created September 1, 2025 08:32
Shared via mypy Playground
from typing import Any, cast
class BaseForm:
cleaned_data: dict[str, Any] = {}
def clean(self) -> dict[str, Any] | None:
return self.cleaned_data