Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created June 10, 2026 20:28
Shared via mypy Playground
from collections.abc import Callable
from typing import Protocol, TypeVar
class TGCStatsInfo(Protocol):
gen: int
iid: int
class TInstantMsg(Protocol):
type: str
name: str
@mypy-play
mypy-play / main.py
Created June 10, 2026 14:08
Shared via mypy Playground
from typing import (
Generic,
Literal,
TypeAlias,
overload,
)
from typing_extensions import (
TypeVar,
)
@mypy-play
mypy-play / main.py
Created June 10, 2026 08:46
Shared via mypy Playground
from enum import StrEnum, auto
from dataclasses import dataclass
class E(StrEnum):
A = auto()
B = auto()
@dataclass()
class D:
e: E
@mypy-play
mypy-play / main.py
Created June 10, 2026 06:50
Shared via mypy Playground
#!/usr/bin/env python3
import sys
# Version check - must be first
if sys.version_info < (3, 14):
logger.info(f"Error: This script requires Python 3.14 or higher. Current version: {sys.version_info.major}.{sys.version_info.minor}")
sys.exit(1)
import asyncio
@mypy-play
mypy-play / main.py
Created June 9, 2026 15:43
Shared via mypy Playground
from typing import TypeVar
from functools import singledispatch
#T = TypeVar("T", bound=str | int) # should give errors
T = TypeVar("T", str, int) # should work
@singledispatch
def times_two(v: T) -> T:
raise NotImplementedError
@mypy-play
mypy-play / main.py
Created June 9, 2026 14:40
Shared via mypy Playground
import signal
1 in (i for i in (signal.SIGINT,))
1 in [i for i in (signal.SIGINT,)]
@mypy-play
mypy-play / main.py
Created June 9, 2026 14:37
Shared via mypy Playground
import signal
1 in (i for i in (signal.SIGINT,))
1 in [i for i in (signal.SIGINT,)]
@mypy-play
mypy-play / main.py
Created June 8, 2026 21:15
Shared via mypy Playground
import dataclasses
from functools import partial
from typing import TYPE_CHECKING, TypeAlias
if TYPE_CHECKING:
from dataclasses import dataclass as frozen_nan_safe
else:
def frozen_nan_safe(**kwargs):
assert kwargs['frozen']
return dataclasses.dataclass(**kwargs)
@mypy-play
mypy-play / main.py
Created June 8, 2026 21:05
Shared via mypy Playground
import dataclasses
from functools import partial
from typing import TYPE_CHECKING
if TYPE_CHECKING:
frozen_nan_safe = dataclasses.dataclass
else:
frozen_nan_safe = partial(dataclasses.dataclass, frozen=True)
def test_frozen_decorator() -> None:
@mypy-play
mypy-play / main.py
Created June 8, 2026 01:06
Shared via mypy Playground
from typing import Callable, Protocol
### Example 1: Works as intended. The method is decorated and is
### callable as usual.
def decorate_method[T](method: Callable[[T], None]) -> Callable[[T], None]:
def _inner(_self: T) -> None:
print('Decorated!')
method(_self)