Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created July 11, 2025 17:40
Shared via mypy Playground
from typing import TypeVar
U = TypeVar('U', bound=int)
class X[U]:
u: U
def do(self, var: U) -> None:
pass
@mypy-play
mypy-play / main.py
Created July 11, 2025 15:51
Shared via mypy Playground
import typing
def apply_on_answer[T](f: typing.Callable[[int], T], /) -> T:
return f(42)
class C:
@apply_on_answer
def x(answer: int) -> int:
return answer
@mypy-play
mypy-play / main.py
Created July 11, 2025 12:57
Shared via mypy Playground
# distutils/extension.py
from dataclasses import dataclass, field, fields
import os
import warnings
from typing import TYPE_CHECKING, Iterable
@dataclass
class _Extension:
name: str
@mypy-play
mypy-play / main.py
Created July 11, 2025 11:27
Shared via mypy Playground
# distutils/extension.py
from __future__ import annotations
import os
from collections.abc import Iterable
from typing import TYPE_CHECKING, TypedDict, Union
if TYPE_CHECKING:
from typing_extensions import TypeAlias, Unpack
@mypy-play
mypy-play / main.py
Created July 11, 2025 11:11
Shared via mypy Playground
from warnings import deprecated
@deprecated("f is deprecated")
def f(): ...
f()
@deprecated("Will be removed in Python 3.14")
def g(): ...
@mypy-play
mypy-play / main.py
Created July 11, 2025 09:10
Shared via mypy Playground
def word(name: str) -> str:
return f"my name is {name}"
print(word("evan"))
@mypy-play
mypy-play / main.py
Created July 11, 2025 09:09
Shared via mypy Playground
import requests
endpoint = "https://gitlab.uk/merge_requests"
headers = {"PRIVATE_TOKEN": "SECRET"}
branch = "banana"
params = {"state": "opened", "scope": "all", "target_branch": branch, "author_id": 87, "view": "simple"}
response = requests.get(endpoint, headers=headers, params=params)
@mypy-play
mypy-play / main.py
Created July 11, 2025 05:17
Shared via mypy Playground
import typing as tp
class Wrapped[**P, T]:
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> T:
raise NotImplementedError
def inner[**P, T](f: tp.Callable[P, T]) -> Wrapped[P, T]:
assert False
@mypy-play
mypy-play / main.py
Created July 10, 2025 22:32
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 July 10, 2025 22:14
Shared via mypy Playground
from typing import overload
class A[T]: ...
class B[T: (int, str)](A[T]):
@overload
def __new__(cls: type[B[int]]) -> B[int]: ...
@overload
def __new__(cls: type[B[str]]) -> B[str]: ...
def __new__(cls): raise NotImplementedError