Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created June 8, 2026 21:15
Shared via mypy Playground
import dataclasses
from functools import partial
from typing import TYPE_CHECKING, TypeAlias
if TYPE_CHECKING:
from dataclasses import dataclass as frozen_nan_safe
else:
def frozen_nan_safe(**kwargs):
assert kwargs['frozen']
return dataclasses.dataclass(**kwargs)
@mypy-play
mypy-play / main.py
Created June 8, 2026 21:05
Shared via mypy Playground
import dataclasses
from functools import partial
from typing import TYPE_CHECKING
if TYPE_CHECKING:
frozen_nan_safe = dataclasses.dataclass
else:
frozen_nan_safe = partial(dataclasses.dataclass, frozen=True)
def test_frozen_decorator() -> None:
@mypy-play
mypy-play / main.py
Created June 8, 2026 01:06
Shared via mypy Playground
from typing import Callable, Protocol
### Example 1: Works as intended. The method is decorated and is
### callable as usual.
def decorate_method[T](method: Callable[[T], None]) -> Callable[[T], None]:
def _inner(_self: T) -> None:
print('Decorated!')
method(_self)
@mypy-play
mypy-play / main.py
Created June 6, 2026 08:12
Shared via mypy Playground
class A:
def __new__(cls) -> "A":
return super().__new__(cls)
class B(A):
pass
reveal_type(B())
@mypy-play
mypy-play / main.py
Created June 5, 2026 17:03
Shared via mypy Playground
from typing import Literal
Address = int
Data = int
Operation = Literal["read", "write"]
class AddressBus:
def __init__(self) -> None:
self._address: Address = 0x0000
@mypy-play
mypy-play / main.py
Created June 5, 2026 17:03
Shared via mypy Playground
from typing import Literal
Address = int
Data = int
Operation = Literal["read", "write"]
class AddressBus:
def __init__(self) -> None:
self._address: Address = 0x0000
@mypy-play
mypy-play / main.py
Created June 5, 2026 17:02
Shared via mypy Playground
from collections.abc import Callable
from functools import wraps
from inspect import signature
from types import FunctionType
def func() -> None:
pass
@mypy-play
mypy-play / main.py
Created June 5, 2026 11:01
Shared via mypy Playground
import asyncio
from typing import overload, Literal
class Redis:
def get(self):
pass
class RedisAsync:
async def get(self):
pass
@mypy-play
mypy-play / main.py
Created June 4, 2026 20:57
Shared via mypy Playground
from typing import Iterator
from warnings import deprecated
@deprecated("...")
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 June 4, 2026 13:03
Shared via mypy Playground
from typing import TypeIs
def check(x: str | int | list[int]) -> TypeIs[int]:
return isinstance(x, int)
def foo(x: str | int | list[int]) -> None:
if not check(x):
reveal_type(x)