Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created February 15, 2026 22:52
Shared via mypy Playground
from dataclasses import dataclass
@dataclass
class Example:
a: int
__name__: str
e = Example(1)
@mypy-play
mypy-play / main.py
Created February 15, 2026 15:26
Shared via mypy Playground
import random
def foo() -> None:
a: float | str
a = random.random()
a = input()
@mypy-play
mypy-play / main.py
Created February 15, 2026 13:13
Shared via mypy Playground
import typing
import types
from typing import Any
def _validate_type_hint(parameter_type: type[Any]) -> tuple[str, Any] | None:
generic_args: tuple[type[Any], ...] = typing.get_args(parameter_type)
match generic_args:
case () | (_,):
@mypy-play
mypy-play / main.py
Created February 14, 2026 17:57
Shared via mypy Playground
def show_count(count, word):
if count == 1:
return f'1 {word}'
count_str = str(count) if count else 'no'
return f'{count_str} {word}s'
@mypy-play
mypy-play / main.py
Created February 14, 2026 17:22
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 14, 2026 17:16
Shared via mypy Playground
from typing import Callable
def f(x: str)->None:
if isinstance(x, int):
print("impossible")
@mypy-play
mypy-play / main.py
Created February 13, 2026 22:24
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 13, 2026 16:48
Shared via mypy Playground
class A:
def __new__(cls) -> int:
return 42
reveal_type(A())
@mypy-play
mypy-play / main.py
Created February 13, 2026 00:23
Shared via mypy Playground
class A:
def go(self) -> None:
pass
class B0:
X: type[A] | None = None
class B(B0):
X = A
@mypy-play
mypy-play / main.py
Created February 12, 2026 15:06
Shared via mypy Playground
from typing import Callable, TypeVar
_K = TypeVar("_K")
_V = TypeVar("_V")
class Foo:
pass
class Util: