Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created September 2, 2026 20:15
Shared via mypy Playground
from enum import Enum
from typing import reveal_type
class NoValue(Enum):
FIRST = "first"
def command(self) -> str:
reveal_type(self.value)
return "--" + self.value
class WithValue(Enum):
@mypy-play
mypy-play / main.py
Created September 2, 2026 10:04
Shared via mypy Playground
from typing import Any, reveal_type
def get() -> Any:
return 42 # pretend this could be anything
x = get()
reveal_type(x)
y = get()
assert isinstance(y, int)
@mypy-play
mypy-play / main.py
Created September 2, 2026 03:16
Shared via mypy Playground
from typing import Any
def do_something_awesome(thing: list[str | dict[Any, Any]]):
...
only_list_str: list[str] = ["foo", "bar"]
list_mixed: list[str, dict[Any, Any]] = ["foo", {"bar": "baz"}]
do_something_awesome(["foo", "bar"])
@mypy-play
mypy-play / main.py
Created September 1, 2026 00:09
Shared via mypy Playground
from typing import Self, Annotated
class A():
def __init__(self: Annotated[Self]):
pass
@mypy-play
mypy-play / main.py
Created August 30, 2026 13:21
Shared via mypy Playground
from typing import TypeVar, Literal, Generic, overload, reveal_type
from typing import Any
_ST = TypeVar("_ST")
_GT = TypeVar("_GT")
class Field(Generic[_ST, _GT]):
def __init__(self, *, null: bool = False) -> None:
super().__init__()
@mypy-play
mypy-play / main.py
Created August 30, 2026 11:56
Shared via mypy Playground
from __future__ import annotations
from dataclasses import dataclass
from contextvars import ContextVar, copy_context
from typing import Generic, TypeVar, overload, reveal_type
T = TypeVar("T")
class CtxProperty(Generic[T]):
@mypy-play
mypy-play / main.py
Created August 29, 2026 23:32
Shared via mypy Playground
x = []
x.append(2)
x.append("test")
@mypy-play
mypy-play / main.py
Created August 29, 2026 19:59
Shared via mypy Playground
import abc
import collections.abc
import logging
import typing
import uuid
logger = logging.getLogger(__name__)
class GenericRegistry[K, V](abc.ABC, collections.abc.Mapping[K, V]):
@mypy-play
mypy-play / main.py
Created August 29, 2026 13:10
Shared via mypy Playground
from pathlib import Path
def split_paths(paths: str) -> list[Path]:
paths: list[str] = paths.split(":")
return [Path(p) for p in paths]
@mypy-play
mypy-play / main.py
Created August 28, 2026 20:49
Shared via mypy Playground
from dataclasses import dataclass
from typing_extensions import TypeAliasType
@dataclass
class A:
T1 = TypeAliasType("T1", int)
x: A.T1 = 2