Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created December 29, 2025 03:58
Shared via mypy Playground
from dataclasses import dataclass
@dataclass
class Network:
cidr: str
@dataclass
class Subnet:
network: 'NetworkQ'
cidr: str
@mypy-play
mypy-play / main.py
Created December 28, 2025 23:05
Shared via mypy Playground
from typing import TypeVar, Generator
from types import coroutine
T = TypeVar("T", str, bytes)
async def process(data: T) -> T:
return data
@coroutine
def process2(data: T) -> Generator[T, None, T]:
@mypy-play
mypy-play / main.py
Created December 28, 2025 23:05
Shared via mypy Playground
from typing import TypeVar, Generator
from types import coroutine
T = TypeVar("T", str, bytes)
async def process(data: T) -> T:
return data
@coroutine
def process2(data: T) -> Generator[T, None, T]:
@mypy-play
mypy-play / main.py
Created December 28, 2025 21:51
Shared via mypy Playground
def f(whatever) -> int:
return 42
@f
class Foo: ...
reveal_type(Foo)
@mypy-play
mypy-play / main.py
Created December 27, 2025 20:42
Shared via mypy Playground
from typing import Pattern, Match
import re
reveal_type(re.compile("[abc]*"))
reveal_type(re.match(my_pattern, "abbcab"))
@mypy-play
mypy-play / main.py
Created December 27, 2025 20:42
Shared via mypy Playground
from typing import Pattern, Match
import re
reveal_type(re.compile("[abc]*"))
reveal_type(re.match(my_pattern, "abbcab"))
@mypy-play
mypy-play / main.py
Created December 27, 2025 18:53
Shared via mypy Playground
from pathlib import Path
class MyClass:
def __init__(self, path: Path | None) -> None:
self.path = path
def test(a: MyClass, b: MyClass):
if a.path is None:
@mypy-play
mypy-play / main.py
Created December 27, 2025 07:13
Shared via mypy Playground
from typing import Callable, ParamSpec
P = ParamSpec('P')
DefaultP = ParamSpec('DefaultP', default=(int, str)) # Error
@mypy-play
mypy-play / main.py
Created December 27, 2025 06:33
Shared via mypy Playground
class A:
import math
def __new__(cls) -> "A":
reveal_type(cls.math.pi) # N: Revealed type is "Any"
return cls()
def pos_or_named_self(self) -> None:
reveal_type(self.math.pi) # N: Revealed type is "builtins.float"
@mypy-play
mypy-play / main.py
Created December 27, 2025 06:32
Shared via mypy Playground
class A:
import math
def __new__(cls) -> "A":
reveal_type(cls.math.pi) # N: Revealed type is "types.ModuleType"
return cls()
def pos_or_named_self(self) -> None:
reveal_type(self.math.pi) # N: Revealed type is "types.ModuleType"