Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created April 26, 2024 10:56
Shared via mypy Playground
import typing
class typed_dict(typing.TypedDict, total=False):
user_id: int
class HasTypedDict:
TypedDict = typed_dict
TypedDict_TypeAlias: typing.TypeAlias = typed_dict
@mypy-play
mypy-play / main.py
Created April 26, 2024 09:32
Shared via mypy Playground
def fun(a: int, b: str) -> None:
pass
call_args = {"a": 1, "b": "bbb"}
fun(**call_args)
@mypy-play
mypy-play / main.py
Created April 26, 2024 04:43
Shared via mypy Playground
from typing import Annotated, reveal_type
def hello(code: Annotated[str, "html"]):
reveal_type(code)
hello()
@mypy-play
mypy-play / main.py
Created April 26, 2024 01:02
Shared via mypy Playground
from typing import Dict, Iterator, TypeVar, Union
class A:
name: str = ""
K = TypeVar('K', bound=Union[str, A])
def a(arg: Dict[K, int]) -> None:
d = dict(
(k.name if isinstance(k, A) else k, v)
@mypy-play
mypy-play / main.py
Created April 25, 2024 23:28
Shared via mypy Playground
i: int
s: str
if i == s:
print("hi")
a: int | str
b: str | range
if a == b:
print("hi")
@mypy-play
mypy-play / main.py
Created April 25, 2024 20:44
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 April 25, 2024 14:35
Shared via mypy Playground
from contextlib import ExitStack
filenames = ("test", "test2")
with ExitStack() as stack:
for x in filenames:
stack.enter_context(open(x))
y = 2
print(y)
@mypy-play
mypy-play / main.py
Created April 25, 2024 14:23
Shared via mypy Playground
from typing import Final
# Sentinel to denote unset values
UNSET: Final[object] = object()
def foo(timezone: str | object | None = UNSET) -> None:
pass
if __name__ == "__main__":
foo()
@mypy-play
mypy-play / main.py
Created April 25, 2024 14:17
Shared via mypy Playground
# Sentinel to denote unset values
UNSET = object()
def foo(timezone: str | object | None = UNSET) -> None:
pass
if __name__ == "__main__":
foo()
@mypy-play
mypy-play / main.py
Created April 25, 2024 12:26
Shared via mypy Playground
from typing import TypeVar
from typing_extensions import TypeIs
_T = TypeVar('_T', str, int)
def is_str(val: str | int) -> TypeIs[str]:
return isinstance(val, str)