This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Iterator | |
def fib(n: int) -> Iterator[int]: | |
a, b = 0, 1 | |
while a < n: | |
yield a | |
a, b = b, a + b | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Generic, TypeVar | |
T = TypeVar("T") | |
class Foo(Exception, Generic[T]): ... | |
try: | |
raise Foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test() -> dict[str, int]: | |
vals = {'hi': 3, 'ho': 4} | |
return vals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |