Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created October 4, 2026 20:32
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 October 4, 2026 17:09
Shared via mypy Playground
from typing import reveal_type
def bug_wrong(my_list: list[tuple[int, str, str]]) -> bool:
for _, *my_tuple in my_list:
reveal_type(my_tuple)
if my_tuple == ('foo', 'bar'):
return True
return False
@mypy-play
mypy-play / main.py
Created October 4, 2026 17:08
Shared via mypy Playground
from typing import Optional
from typing import reveal_type
def bug_wrong(my_list: list[tuple[int, str, str]]) -> bool:
for _, *my_tuple in my_list:
reveal_type(my_tuple)
if my_tuple == ('foo', 'bar'):
return True
return False
@mypy-play
mypy-play / main.py
Created October 4, 2026 17:01
Shared via mypy Playground
from typing import Optional
from typing import reveal_type
def bug_wrong(my_list: list[tuple[int, str, str]]) -> bool:
for _, *my_tuple in my_list:
reveal_type(my_tuple)
if my_tuple == ('foo', 'bar'):
return True
return False
@mypy-play
mypy-play / main.py
Created October 3, 2026 10:42
Shared via mypy Playground
from typing import Generic, Protocol, TypeVar, reveal_type
T = TypeVar("T")
T_contra = TypeVar("T_contra", contravariant=True)
S2 = TypeVar("S2")
class ElementOpsMixin(Generic[S2]):
def _sub(self: ElementOpsMixin[int], other: int) -> ElementOpsMixin[int]: ... # invariant
class Supports_Sub(Protocol[T_contra, S2]):
@mypy-play
mypy-play / main.py
Created October 2, 2026 16:15
Shared via mypy Playground
import importlib.machinery
import importlib.util
class PatchedLoader(importlib.machinery.SourceFileLoader):
pass
spec = importlib.util.spec_from_file_location(
"", "", loader=PatchedLoader("", "")
@mypy-play
mypy-play / main.py
Created October 2, 2026 16:15
Shared via mypy Playground
import importlib.machinery
import importlib.util
class PatchedLoader(importlib.machinery.SourceFileLoader):
pass
spec = importlib.util.spec_from_file_location(
"", "", loader=PatchedLoader("", "")
)
@mypy-play
mypy-play / main.py
Created October 2, 2026 13:36
Shared via mypy Playground
foo: int = "bar" # type: ignore[assignment, ty:invalid-assignment]
bar: int = "foo" # type: ignore[assignment] # ty: ignore[invalid-assignment]
@mypy-play
mypy-play / main.py
Created October 2, 2026 09:07
Shared via mypy Playground
x = 100
x = "Now I am a string"
print(x.upper())
@mypy-play
mypy-play / main.py
Created September 30, 2026 18:30
Shared via mypy Playground
from typing import TypeVar, Generic
from abc import ABC, abstractmethod
T = TypeVar("T")
class SomeParent(ABC, Generic[T]):
class Inner(ABC):
@abstractmethod
def get_t(self) -> T: