Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created March 10, 2026 02:24
Shared via mypy Playground
from typing import Concatenate, Callable
def foo[**P](fn: Callable[P, None], x: int, *args: P.args, **kwargs: P.kwargs) -> None: ...
def test(a: str) -> None: ...
foo(fn=test, x=1, a="")
@mypy-play
mypy-play / main.py
Created March 9, 2026 19:51
Shared via mypy Playground
from typing import Self, reveal_type
class NodeA:
def __init__(self: Self) -> None:
reveal_type(self) # Self`0
self.myself: Self = self # ✅️
class NodeB:
def __init__(self) -> None:
reveal_type(self) # __main__.NodeB
@mypy-play
mypy-play / main.py
Created March 9, 2026 19:27
Shared via mypy Playground
from typing import Protocol, Self
class FloatScalar[FloatT](Protocol):
def pow(self, arg: FloatT) -> Self: ...
def upcast(arg: FloatScalar[object]) -> FloatScalar[float]:
return arg # ✅️
@mypy-play
mypy-play / main.py
Created March 9, 2026 19:05
Shared via mypy Playground
from typing import Protocol, Self
class FloatScalar[FloatT](Protocol):
def pow(self, arg: FloatT) -> Self: ...
def upcast(arg: FloatScalar[object]) -> FloatScalar[float]:
return arg # ✅️
@mypy-play
mypy-play / main.py
Created March 9, 2026 19:04
Shared via mypy Playground
from typing import Protocol, Self
class FloatScalar[FloatT](Protocol):
def pow(self, arg: FloatT) -> Self | complex: ...
def upcast(arg: FloatScalar[object]) -> FloatScalar[float]:
return arg
@mypy-play
mypy-play / main.py
Created March 9, 2026 17:48
Shared via mypy Playground
from typing import reveal_type
from typing import Generator, AsyncGenerator
def async_returns_generator() -> Generator[int, int, None] | AsyncGenerator[int, int]:
x = yield 1
reveal_type(x)
return None
@mypy-play
mypy-play / main.py
Created March 9, 2026 14:33
Shared via mypy Playground
from typing import Protocol, TypedDict, Unpack, Any
class Factory(Protocol):
def __call__(self, **kwargs: Any) -> Any:
pass
def use_factory(factory: Factory):
pass
@mypy-play
mypy-play / main.py
Created March 9, 2026 14:01
Shared via mypy Playground
from typing import Protocol, TypedDict, Unpack, Any
class Factory(Protocol):
def __call__(self, **kwargs: Any) -> Any:
pass
def use_factory(factory: Factory):
pass
@mypy-play
mypy-play / main.py
Created March 9, 2026 12:25
Shared via mypy Playground
from unittest.mock import Mock
class O: ...
def take_o(o: O) -> None: ...
take_o(Mock())
@mypy-play
mypy-play / main.py
Created March 8, 2026 22:49
Shared via mypy Playground
from typing import Iterable, Protocol
from dataclasses import dataclass
@dataclass
class Square:
sidelength: float
def area(self) -> float:
return self.sidelength * self.sidelength