Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created May 15, 2024 14:37
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 May 15, 2024 14:07
Shared via mypy Playground
import os
from pathlib import Path
def foo() -> list[str| Path]:
return ["a", "b", Path("/tmp")]
def main() -> None:
cmd_line = foo()
file = cmd_line[0]
args = cmd_line[1:]
@mypy-play
mypy-play / main.py
Created May 15, 2024 13:49
Shared via mypy Playground
import os
from pathlib import Path
def foo() -> list[str|os.PathLike[str]]:
return ["a", "b", Path("/tmp")]
def main() -> None:
cmd_line = foo()
file = cmd_line[0]
args = cmd_line[1:]
@mypy-play
mypy-play / main.py
Created May 15, 2024 11:10
Shared via mypy Playground
from typing import TypeVar, Generic
T = TypeVar("T")
class W(Generic[T]):
def managed(self) -> T:
raise NotImplementedError
class MW(Generic[T]):
def __init__(self, w: T) -> None:
@mypy-play
mypy-play / main.py
Created May 15, 2024 09:04
Shared via mypy Playground
from typing import Generic, TypeVar
T = TypeVar("T")
class Foo(Exception, Generic[T]): ...
try:
raise Foo
@mypy-play
mypy-play / main.py
Created May 14, 2024 17:14
Shared via mypy Playground
from enum import Enum
from typing import Self
class E(Enum):
def __new__(cls, arg: int) -> Self: ...
A = "a", "b" # Type error, too many arguments.
@mypy-play
mypy-play / main.py
Created May 14, 2024 17:07
Shared via mypy Playground
def test() -> dict[str, int]:
vals = {'hi': 3, 'ho': 4}
return vals
@mypy-play
mypy-play / main.py
Created May 14, 2024 17:03
Shared via mypy Playground
from collections.abc import Sequence
from enum import Enum
from typing import Self, overload
class MyStr(Sequence["MyStr"]):
@overload
def __new__(cls, object: object = ...) -> Self: ...
@overload
def __new__(cls, object: bytes, encoding: str = ..., errors: str = ...) -> Self: ...
@mypy-play
mypy-play / main.py
Created May 14, 2024 17:01
Shared via mypy Playground
from enum import Enum
class StrEnum(str, Enum):
A = "a" # ok
B = b"b", "utf-8" # ok, see the typeshed definition of `str.__new__`
C = "too", "many", "arguments", "provided" # runtime error: TypeError: str() takes at most 3 arguments (4 given)
@mypy-play
mypy-play / main.py
Created May 14, 2024 16:55
Shared via mypy Playground
from enum import Enum
class MyStrEnum(str, Enum):
A = "a" # ok
B = b"b", "utf-8" # ok, see the typeshed definition of `str.__new__`
C = "too", "many", "arguments", "provided" # runtime error: TypeError: str() takes at most 3 arguments (4 given)