Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created November 12, 2025 21:19
Shared via mypy Playground
from typing import TypeVar, Literal
A = TypeVar("A")
B = TypeVar("B")
MutExResult = tuple[Literal["neither"], None, None] | tuple[Literal["both"], A, B] | tuple[Literal["left"], A, None] | tuple[Literal["right"], None, B]
def check_mutually_exclusive(a: A | None, b: B | None) -> MutExResult[A, B]:
if a is None and b is None:
@mypy-play
mypy-play / main.py
Created November 12, 2025 11:17
Shared via mypy Playground
import dataclasses
@dataclasses.dataclass
class Base:
def fn(self, a: int) -> int:
return a
@mypy-play
mypy-play / main.py
Created November 12, 2025 11:06
Shared via mypy Playground
from dataclasses import dataclass
from typing import TypeVar, Generic, Callable, Dict, Any
import sys
T_Input = TypeVar("T_Input")
class _RegisteredType(Generic[T_Input]):
def __init__(self, name: str):
self.name = name
@mypy-play
mypy-play / main.py
Created November 12, 2025 09:41
Shared via mypy Playground
from typing import Generic, TypeVar, Literal
from dataclasses import dataclass
T = TypeVar('T')
class RequestTypeToken(Generic[T]):
"""Token that proves a request type has been registered."""
def __init__(self, type_name: T):
self.name = type_name
print(type(type_name))
@mypy-play
mypy-play / main.py
Created November 11, 2025 22:59
Shared via mypy Playground
from typing import Any, Literal, NotRequired, TypedDict, final
# @final
class InputPromptJSON(TypedDict):
"""JSON format representing the input text to an LLM completion.
This may evolve over time as we add support for tools and other features.
"""
# New rows should always use an array of blocks, even if the application code just gave a string.
@mypy-play
mypy-play / main.py
Created November 11, 2025 15:34
Shared via mypy Playground
from dataclasses import dataclass
@dataclass
class A:
a: int
@dataclass(slots=True)
class ASlot:
a: int
@mypy-play
mypy-play / main.py
Created November 11, 2025 13:49
Shared via mypy Playground
from typing import Iterable
def fun[T](values: Iterable[T], value: list[T]) -> tuple[T | None, T | None]:
...
return None, None
values: Iterable[int]
my_list: list[int]
@mypy-play
mypy-play / main.py
Created November 10, 2025 22:46
Shared via mypy Playground
from __future__ import annotations
from typing import TypeVar
T = TypeVar("T", bound=str | int)
VALUES: dict[str, str] = {"SIZE": "100", "ADDR": "0x100", "NAME": "potato"}
def get_parameter(parameter: str, _rtype: type[T]) -> T:
"""Get the parameter"""
if parameter.startswith("N"):
@mypy-play
mypy-play / main.py
Created November 10, 2025 22:27
Shared via mypy Playground
from __future__ import annotations
from typing import TypeVar
T = TypeVar("T", bound=str | int)
def get_parameter(parameter: str, _rtype: type[T]) -> T:
"""Get the parameter"""
if parameter == "SIZE":
return 5
@mypy-play
mypy-play / main.py
Created November 10, 2025 22:22
Shared via mypy Playground
from __future__ import annotations
from typing import TypeVar
VALUES: dict[str, str] = {"SIZE": "100", "ADDR": "0x100", "NAME": "potato"}
T = TypeVar("T", bound=str | int)
def get_parameter(parameter: str, _rtype: type[T]) -> T:
"""Get the parameter"""