Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created May 3, 2024 09:25
Shared via mypy Playground
from typing import (
Generic,
TypeVar,
Callable,
Concatenate,
ParamSpec,
reveal_type,
)
from dataclasses import dataclass
@mypy-play
mypy-play / main.py
Created May 3, 2024 07:56
Shared via mypy Playground
from typing import (
Generic,
TypeVar,
Callable,
Concatenate,
ParamSpec,
reveal_type,
)
from dataclasses import dataclass
@mypy-play
mypy-play / main.py
Created May 3, 2024 03:29
Shared via mypy Playground
from typing import Tuple
child_min_max: Tuple[int, int] = (0, 0)
@mypy-play
mypy-play / main.py
Created May 2, 2024 18:20
Shared via mypy Playground
from typing import TypeVar, Protocol, Generic, Callable, Any
T = TypeVar("T", contravariant=True)
# MyPy: error: Missing return statement [empty-body]
# MyPy: error: Cannot use a contravariant type variable as return type [misc]
# MyPy: error: A function returning TypeVar should receive at least one argument containing the same TypeVar [type-var]
# PyRight: TypeVar "T" appears only once in generic function signature
# Use "object" instead (reportInvalidTypeVarUse)
def f_expecting_Nothing_returning_T() -> T:...
@mypy-play
mypy-play / main.py
Created May 2, 2024 17:57
Shared via mypy Playground
from typing import TypeVar, Protocol, Generic, Callable, Any
T = TypeVar("T", covariant=True)
# MyPy: error: Missing return statement [empty-body]
# MyPy: error: A function returning TypeVar should receive at least one argument containing the same TypeVar [type-var]
# PyRight: TypeVar "T" appears only once in generic function signature
# Use "object" instead (reportInvalidTypeVarUse)
def f_expecting_Nothing_returning_T() -> T:...
# MyPy: error: Cannot use a covariant type variable as a parameter [misc]
@mypy-play
mypy-play / main.py
Created May 2, 2024 17:45
Shared via mypy Playground
from typing import TypeVar, Protocol, Generic, Callable, Any
T = TypeVar("T")
# MyPy: error: Missing return statement [empty-body]
# MyPy: error: A function returning TypeVar should receive at least one argument containing the same TypeVar [type-var]
# PyRight: TypeVar "T" appears only once in generic function signature.
# Use "object" instead (reportInvalidTypeVarUse)
def f_expecting_Nothing_returning_T() -> T:...
# PyRight: TypeVar "T" appears only once in generic function signature.
@mypy-play
mypy-play / main.py
Created May 2, 2024 16:54
Shared via mypy Playground
from functools import lru_cache
from typing import override
class Foo:
def bar(self, x: int) -> None: pass
class Bar(Foo):
@override
@lru_cache
def bar(self, x: int) -> None: pass
@mypy-play
mypy-play / main.py
Created May 2, 2024 15:58
Shared via mypy Playground
from typing import overload, Literal, List, Tuple, Any
class Example:
pass
class Prediction:
pass
class TestMypy:
@mypy-play
mypy-play / main.py
Created May 2, 2024 15:55
Shared via mypy Playground
from typing import Generic, TypeVar
T = TypeVar("T", bound=int | None)
class C(Generic[T]):
def __init__(self, x: T = None):
self.x = x
@mypy-play
mypy-play / main.py
Created May 2, 2024 15:18
Shared via mypy Playground
from typing import (
Protocol,
TypeVar,
Callable,
ParamSpec,
reveal_type,
)
X = TypeVar("X")