Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created March 9, 2025 20:40
Shared via mypy Playground
from typing import TypeVar, Generic, ClassVar, Self
T = TypeVar("T", bound="BaseModel")
class BaseModel:
_meta: "ClassVar[Meta[Self]]"
class Meta(Generic[T]):
def __init__(self, model: type[T]) -> None:
self.model = model
@mypy-play
mypy-play / main.py
Created March 9, 2025 20:32
Shared via mypy Playground
from typing import TypeVar, Generic, ClassVar, Self
# Define a generic type variable
T = TypeVar("T", bound="BaseModel")
class BaseModel:
"""A base model with a generic type parameter."""
_meta: "ClassVar[Meta[Self]]"
class Meta(Generic[T]):
@mypy-play
mypy-play / main.py
Created March 9, 2025 20:27
Shared via mypy Playground
from __future__ import annotations
from enum import Enum
from typing import Type
menu_msg : str = \
"""
Please select a valid size (1-3):
1. Small
2. Medium
3. Large
@mypy-play
mypy-play / main.py
Created March 9, 2025 20:22
Shared via mypy Playground
from __future__ import annotations
from enum import Enum
from typing import Type
menu_msg : str = \
"""
Please select a valid size (1-3):
1. Small
2. Medium
3. Large
@mypy-play
mypy-play / main.py
Created March 9, 2025 09:09
Shared via mypy Playground
from typing import TypedDict, Mapping
class FooType(TypedDict):
a: int
b: str
def foo(value: Mapping[str, int | str]):
print(value)
x = {
@mypy-play
mypy-play / main.py
Last active March 9, 2025 09:06
Shared via mypy Playground
from typing import TypedDict, Mapping
class FooType(TypedDict):
a: int
b: str
def foo(value: Mapping[str, int | str]):
print(value)
x = {
@mypy-play
mypy-play / main.py
Created March 8, 2025 18:50
Shared via mypy Playground
from collections.abc import Callable
from enum import Enum
from typing import Generic, Protocol, TypeVar, Type, overload
T = TypeVar("T", covariant=True)
class A(Generic[T]):
pass
@mypy-play
mypy-play / main.py
Created March 8, 2025 15:14
Shared via mypy Playground
import psycopg as pg
@mypy-play
mypy-play / main.py
Created March 8, 2025 05:06
Shared via mypy Playground
# mypy: allow-any-expr=False
class A(tuple[int, int]): ...
print(A()[:])
@mypy-play
mypy-play / main.py
Created March 8, 2025 05:02
Shared via mypy Playground
# mypy: allow-any-expr=False
class A(tuple[int, int]): ...
print(A()[:])