Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created May 14, 2026 17:59
Shared via mypy Playground
from typing import Iterato
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 May 14, 2026 17:16
Shared via mypy Playground
from typing import reveal_type
text = "some\nstuff"
version, desc, *_ = [*text.split(sep="\n", maxsplit=1), None, None]
reveal_type(version)
reveal_type(desc)
@mypy-play
mypy-play / main.py
Created May 14, 2026 16:26
Shared via mypy Playground
from typing import Callable, TypeVar, reveal_type
from enum import Enum
_T = TypeVar("_T")
def construct_from_string(key: str, enum_type: Callable[[str], _T]) -> _T:
return enum_type(key.lower())
class Color(Enum):
@mypy-play
mypy-play / main.py
Created May 14, 2026 14:05
Shared via mypy Playground
import pathlib
test = pathlib.Path("test/path/foo.py")
test.suffix == "foo.py"
@mypy-play
mypy-play / main.py
Created May 13, 2026 17:51
Shared via mypy Playground
"""Module used to test Ruff."""
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class Sample:
"""Test class."""
@mypy-play
mypy-play / main.py
Created May 13, 2026 12:37
Shared via mypy Playground
def hello(x: str) -> str:
return f"hello: {x}"
def add(a: float, b: float) -> int:
return int(a + b)
hello("james")
add(5.3, 50)
@mypy-play
mypy-play / main.py
Created May 13, 2026 09:44
Shared via mypy Playground
a: tuple[str, ...]
b: tuple[str, *tuple[str, ...]]
b = ("first", *a) # ok
b = (*a, "last") # Incompatible types in assignment (expression has type "tuple[str, ...]", variable has type "tuple[str, *tuple[str, ...]]")
@mypy-play
mypy-play / main.py
Created May 12, 2026 14:23
Shared via mypy Playground
foo = 0
bar = (1, 2)
match foo, *bar:
case _:
pass
@mypy-play
mypy-play / main.py
Created May 12, 2026 13:50
Shared via mypy Playground
import subprocess
from typing import Any
def test(command: list[str], kwargs: dict[str, Any]) -> None:
subprocess.run(command, capture_output=True, **kwargs)
@mypy-play
mypy-play / main.py
Created May 12, 2026 12:12
Shared via mypy Playground
from opnieuw.test_util import retry_immediately
@retry_immediately()
def foo() -> None:
pass