Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created May 3, 2025 16:16
Shared via mypy Playground
from collections.abc import Callable
import typing_extensions as t
P = t.ParamSpec("P")
T_co = t.TypeVar("T_co", covariant=True)
class ExpectCallable(t.Generic[P, T_co]): ...
class ExpectType(ExpectCallable[P, T_co]): ...
@mypy-play
mypy-play / main.py
Created May 3, 2025 11:34
Shared via mypy Playground
from typing import Protocol
class Descriptor:
def __get__(self, instance, owner) -> int:
return 42
class Foo(Protocol):
x: Descriptor = Descriptor()
class Bar(Protocol):
@mypy-play
mypy-play / main.py
Created May 3, 2025 11:31
Shared via mypy Playground
from typing import Generic, TypeVar, cast, reveal_type
T = TypeVar("T")
class Foo(Generic[T]):
def __init__(self, t: T):
self.t = t
def print(self, t: T):
print(t, self.t)
@mypy-play
mypy-play / main.py
Created May 3, 2025 11:26
Shared via mypy Playground
from typing import Generic, TypeVar, cast, reveal_type
T = TypeVar("T")
class Foo(Generic[T]):
def __init__(self, t: T):
self.t = t
def print(self, t: T):
print(t, self.t)
@mypy-play
mypy-play / main.py
Created May 3, 2025 10:58
Shared via mypy Playground
from typing import Generic, TypeVar
T = TypeVar("T")
class Foo(Generic[T]):
def __init__(self, t: T):
self.t = t
def print(self, t: T):
print(t, self.t)
@mypy-play
mypy-play / main.py
Created May 3, 2025 10:56
Shared via mypy Playground
from typing import Generic, TypeVar
T = TypeVar("T")
class Foo(Generic[T]):
def __init__(self, t: T):
self.t = t
def print(self, t: T):
print(t, self.t)
@mypy-play
mypy-play / main.py
Created May 3, 2025 10:32
Shared via mypy Playground
from typing import Generic, TypeVar
T = TypeVar("T")
class Foo(Generic[T]):
def __init__(self, t: T):
self.t = t
def print(self, t: T):
print(t, self.t)
@mypy-play
mypy-play / main.py
Created May 3, 2025 09:28
Shared via mypy Playground
def foo(*args: str) -> tuple[str, ...]:
return args
print(foo(args="a"))
@mypy-play
mypy-play / main.py
Created May 3, 2025 05:28
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 May 3, 2025 00:48
Shared via mypy Playground
from __future__ import annotations
from enum import IntEnum
from typing import Protocol, Self, cast, reveal_type
class SupportsAdd(Protocol):
def __add__(self, other: Self, /) -> Self: ...
def add_one[T: SupportsAdd](value: T) -> T:
match value: