Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created December 7, 2025 13:15
Shared via mypy Playground
# Welcome to the Pyrefly Sandbox, this is a page
# where you can write, share and learn about typing in Python.
#
# Here are a couple features of this sandbox:
# - Get real-time type checking feedback
# - Use reveal_type() to inspect inferred types
# - experiment with the IDE features that we support (e.g. autocomplete, go-to-definition, hover, etc)
# - Write and run Python code directly in your browser
from typing import *
@mypy-play
mypy-play / main.py
Created December 7, 2025 10:11
Shared via mypy Playground
from typing import TypeVar, Callable, reveal_type
IntOrStr = TypeVar("IntOrStr", str, int)
def rint() -> int:
return 1
def rstr() -> str:
return "A"
@mypy-play
mypy-play / main.py
Created December 7, 2025 05:51
Shared via mypy Playground
from typing import Callable, overload
@overload
def flow[T](x: T) -> T: ...
@overload
def flow[T, U](x: T, f1: Callable[[T], U]) -> U: ...
@overload
def flow[T, U, V](x: T, f1: Callable[[T], U], f2: Callable[[U], V]) -> V: ...
@overload
@mypy-play
mypy-play / main.py
Created December 6, 2025 19:26
Shared via mypy Playground
from decimal import Decimal
m = min(Decimal(1), 2)
reveal_type(m)
@mypy-play
mypy-play / main.py
Created December 6, 2025 18:54
Shared via mypy Playground
from ib111 import week_11 # noqa
# † V tomto příkladu budeme pracovat s n-árními stromy, které nemají
# v uzlech žádné hodnoty (mají pouze stromovou strukturu).
# Třídu Tree nijak nemodifikujte.
class Tree:
def __init__(self) -> None:
self.children: list[Tree] = []
@mypy-play
mypy-play / main.py
Created December 6, 2025 13:19
Shared via mypy Playground
# from ib111 import week_11 # noqa
# Napište čistou funkci, která na základě daného vzoru vytvoří
# množinu všech odpovídajících řetězců. Vzor je tvořený
# alfanumerickými znaky a navíc může obsahovat hranaté závorky –
# znaky ‹[› a ‹]›. Mezi těmito závorkami může stát libovolný počet
# přípustných znaků (krom samotných hranatých závorek) a na daném
# místě se ve výsledném řetězci může nacházet libovolný z těchto
# znaků. Například vzor ‹a[bc]d› reprezentuje řetězce ‹abd› a ‹acd›.
@mypy-play
mypy-play / main.py
Created December 6, 2025 00:34
Shared via mypy Playground
from typing import Generic, TypeVar, ParamSpec, Protocol
# ─────────────────────────────────────────────────────────────
# Base type parameters
# ─────────────────────────────────────────────────────────────
P = ParamSpec("P") # Params for __call__ and run
O = TypeVar("O") # Raw output type
S = TypeVar("S", bound=dict) # State type
@mypy-play
mypy-play / main.py
Created December 5, 2025 23:59
Shared via mypy Playground
class C:
@property
def type(self) -> int:
return 5
class D(C):
type = 6
@mypy-play
mypy-play / main.py
Created December 5, 2025 22:54
Shared via mypy Playground
import re
import types
def fun(val: str | re.Pattern | None):
match val:
case str():
print('string')
case re.Pattern():
print('pattern')
case types.NoneType():
@mypy-play
mypy-play / main.py
Created December 5, 2025 10:44
Shared via mypy Playground
from typing import Final
def foo(foo: Foo[int | str]) -> None:
pass
def bar(bar: Bar[int | str]) -> None:
pass