Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created May 11, 2024 13:59
Shared via mypy Playground
from typing import Type
from typing import Any
from typing import TypeVar
from typing import cast
T = TypeVar("T")
def assert_type(x: Any, type: Type[T]) -> None:
if not isinstance(x, type):
raise TypeError()
@mypy-play
mypy-play / main.py
Created May 11, 2024 12:33
Shared via mypy Playground
from __future__ import annotations
x: int | float
@mypy-play
mypy-play / main.py
Created May 11, 2024 12:31
Shared via mypy Playground
x: int | float
@mypy-play
mypy-play / main.py
Created May 11, 2024 11:39
Shared via mypy Playground
from typing import Generic, Any, overload
from typing_extensions import TypeVar
class C:
pass
class CustomC(C):
pass
_C = TypeVar('_C', bound=C, default=C)
@mypy-play
mypy-play / main.py
Created May 11, 2024 10:19
Shared via mypy Playground
from collections.abc import Callable
def pprint(obj: object, repr_fn: Callable[..., str] = NotImplemented) -> str:
repr_fn = (
repr_fn # expression has type "function", variable has type "Callable[..., str]"
if repr_fn is not NotImplemented
else repr
)
return repr_fn(obj)
@mypy-play
mypy-play / main.py
Created May 11, 2024 10:18
Shared via mypy Playground
from collections.abc import Callable
def pprint(obj: object, repr_fn: Callable[..., str] = NotImplemented):
repr_fn = (
repr_fn # expression has type "function", variable has type "Callable[..., str]"
if repr_fn is not NotImplemented
else repr
)
return repr_fn(obj)
@mypy-play
mypy-play / main.py
Created May 11, 2024 10:17
Shared via mypy Playground
from collections.abc import Callable
def pprint(obj: object, repr_fn: Callable[..., str] = NotImplemented):
repr_fn = (
repr_fn if repr_fn is not NotImplemented
else repr
)
return repr_fn(obj)
@mypy-play
mypy-play / main.py
Created May 11, 2024 08:03
Shared via mypy Playground
import typing as t
class C:
pass
_C = t.TypeVar('_C', bound=C)
class A(t.Generic[_C]):
def __init__(self, c: type[_C] | None = None) -> None:
self._c = c or C
@mypy-play
mypy-play / main.py
Created May 11, 2024 07:52
Shared via mypy Playground
import typing as t
class C:
pass
_C = t.TypeVar('_C', bound=C)
class A(t.Generic[_C]):
def __init__(self, c: type[_C] | None = None) -> None:
self._c = c or C
@mypy-play
mypy-play / main.py
Created May 10, 2024 19:36
Shared via mypy Playground
from typing import ParamSpec, TypeVar, Callable, Literal, NewType
from dataclasses import dataclass
S3EventType = Literal["S3"]
SQSEventType = Literal["SQS"]
SQSRecord = NewType("SQSRecord", str)
AWSEventTypes = S3EventType | SQSEventType