cut stringclasses 1
value | version stringclasses 10
values | idiom_id stringclasses 53
values | pep stringclasses 53
values | introduced_in stringclasses 11
values | form stringclasses 2
values | file stringclasses 887
values | code stringclasses 887
values | source stringclasses 1
value | origin stringclasses 887
values | origin_revision stringclasses 1
value | license stringclasses 1
value |
|---|---|---|---|---|---|---|---|---|---|---|---|
train | 3.13 | protocol | 544 | 3.8 | modern | protocol/modern_17.py | class Proto(Protocol):
@abstractmethod
def meth(self) -> int:
...
class Concrete:
def meth(self) -> int:
return 42
def fun(cls: Type[Proto]) -> int:
return cls().meth() # OK
fun(Proto) # Error
fun(Concrete) # OK
| pydocs | pep-0544.rst:748 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | protocol | 544 | 3.8 | modern | protocol/modern_18.py | from typing import Any, Protocol
class ProtoA(Protocol):
def meth(self, x: int) -> int: ...
class ProtoB(Protocol):
def meth(self, obj: Any, x: int) -> int: ...
class C:
def meth(self, x: int) -> int: ...
a: ProtoA = C # Type check error, signatures don't match!
b: ProtoB = C # OK
| pydocs | pep-0544.rst:777 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | protocol | 544 | 3.8 | modern | protocol/modern_19.py | from typing import NewType, Protocol, Iterator
class Id(Protocol):
code: int
secrets: Iterator[bytes]
UserId = NewType('UserId', Id) # Error, can't provide distinct type
| pydocs | pep-0544.rst:798 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | protocol | 544 | 3.8 | modern | protocol/modern_20.py | from typing import TypeVar, Reversible, Iterable, Sized
T = TypeVar('T')
class SizedIterable(Iterable[T], Sized, Protocol):
pass
CompatReversible = Union[Reversible[T], SizedIterable[T]]
| pydocs | pep-0544.rst:809 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | protocol | 544 | 3.8 | modern | protocol/modern_21.py | # file default_config.py
timeout = 100
one_flag = True
other_flag = False
# file main.py
import default_config
from typing import Protocol
class Options(Protocol):
timeout: int
one_flag: bool
other_flag: bool
def setup(options: Options) -> None:
...
setup(default_config) # OK
| pydocs | pep-0544.rst:824 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | protocol | 544 | 3.8 | modern | protocol/modern_22.py | # callbacks.py
def on_error(x: int) -> None:
...
def on_success() -> None:
...
# main.py
import callbacks
from typing import Protocol
class Reporter(Protocol):
def on_error(self, x: int) -> None:
...
def on_success(self) -> None:
...
rp: Reporter = callbacks # Passes type check
| pydocs | pep-0544.rst:846 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | protocol | 544 | 3.8 | modern | protocol/modern_23.py | from typing import runtime_checkable, Protocol
@runtime_checkable
class SupportsClose(Protocol):
def close(self):
...
assert isinstance(open('some/file'), SupportsClose)
| pydocs | pep-0544.rst:884 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | protocol | 544 | 3.8 | modern | protocol/modern_24.py | class Foo(Protocol):
@property
def c(self) -> int:
return 42 # Default value can be provided for property...
@abstractproperty
def d(self) -> int: # ... or it can be abstract
return 0
| pydocs | pep-0544.rst:943 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | protocol | 544 | 3.8 | modern | protocol/modern_25.py | from typing import Protocol
class Base:
attr: str
class Proto(Base, Protocol):
def meth(self) -> int:
...
class C:
attr: str
def meth(self) -> int:
return 0
| pydocs | pep-0544.rst:1080 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | protocol | 544 | 3.8 | modern | protocol/modern_26.py | class Proto(Protocol):
@abstractmethod
def first(self) -> int:
raise NotImplementedError
def second(self) -> int:
return self.first() + 1
def fun(arg: Proto) -> None:
arg.second()
| pydocs | pep-0544.rst:1152 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | protocol | 544 | 3.8 | modern | protocol/modern_27.py | class P(Protocol):
x: int
class C:
def initialize(self) -> None:
self.x = 0
c = C()
isinstance(c, P) # False
c.initialize()
isinstance(c, P) # True
def f(x: Union[P, int]) -> None:
if isinstance(x, P):
# Static type of x is P here.
...
else:
# Static type of x is int... | pydocs | pep-0544.rst:1239 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | protocol | 544 | 3.8 | modern | protocol/modern_28.py | class Proto(Proto1, Proto2, ..., Protocol):
pass
| pydocs | pep-0544.rst:1276 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | protocol | 544 | 3.8 | modern | protocol/modern_29.py | class P(Protocol):
x: float
def f(arg: P) -> None:
arg.x = 0.42
class C:
x: int
c = C()
f(c) # Would typecheck if covariant subtyping
# of mutable attributes were allowed.
c.x >> 1 # But this fails at runtime
| pydocs | pep-0544.rst:1319 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | protocol | 544 | 3.8 | modern | protocol/modern_30.py | T = TypeVar('T')
class P(Protocol[T]): # Protocol is declared as invariant.
def meth(self) -> T:
...
class C:
def meth(self) -> float:
...
class D(C):
def meth(self) -> int:
...
| pydocs | pep-0544.rst:1347 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | protocol | 544 | 3.8 | modern | protocol/modern_31.py | class OldIterable(Sized, Protocol[T]):
def __getitem__(self, item: int) -> T: ...
CompatIterable = Union[Iterable[T], OldIterable[T]]
class A:
def __iter__(self) -> Iterator[str]: ...
class B:
def __len__(self) -> int: ...
def __getitem__(self, item: int) -> str: ...
def iterate(it: CompatIterable[st... | pydocs | pep-0544.rst:1394 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | positional-only-params | 570 | 3.8 | modern | positional-only-params/modern_01.py | def name(positional_only_parameters, /, positional_or_keyword_parameters,
*, keyword_only_parameters):
...
| pydocs | pep-0570.rst:440 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | positional-only-params | 570 | 3.8 | modern | positional-only-params/modern_02.py | def name(p1, p2, /, p_or_kw, *, kw):
...
def name(p1, p2=None, /, p_or_kw=None, *, kw):
...
def name(p1, p2=None, /, *, kw):
...
def name(p1, p2=None, /):
...
def name(p1, p2, /, p_or_kw):
...
def name(p1, p2, /):
...
| pydocs | pep-0570.rst:458 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | positional-only-params | 570 | 3.8 | modern | positional-only-params/modern_03.py | def foo(name, /, **kwds):
return 'name' in kwds
| pydocs | pep-0570.rst:536 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | positional-only-params | 570 | 3.8 | modern | positional-only-params/modern_04.py | def standard_arg(arg):
print(arg)
def pos_only_arg(arg, /):
print(arg)
def kwd_only_arg(*, arg):
print(arg)
def combined_example(pos_only, /, standard, *, kwd_only):
print(pos_only, standard, kwd_only)
| pydocs | pep-0570.rst:632 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | positional-only-params | 570 | 3.8 | modern | positional-only-params/modern_05.py | def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):
...
| pydocs | pep-0570.rst:702 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | builtin-generics | 585 | 3.9 | modern | builtin-generics/modern_01.py | from __future__ import annotations
def find(haystack: dict[str, list[int]]) -> int:
...
| pydocs | pep-0585.rst:67 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | builtin-generics | 585 | 3.9 | modern | builtin-generics/modern_02.py | list[str]
tuple[int, ...]
ChainMap[str, list[str]]
| pydocs | pep-0585.rst:143 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | builtin-generics | 585 | 3.9 | modern | builtin-generics/modern_03.py | l = list[str]()
list is list[str]
list == list[str]
list[str] == list[str]
list[str] == list[int]
isinstance([1, 2, 3], list[str])
issubclass(list, list[str])
isinstance(list[str], types.GenericAlias)
| pydocs | pep-0585.rst:168 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | builtin-generics | 585 | 3.9 | modern | builtin-generics/modern_04.py | l: list[str] = []
| pydocs | pep-0585.rst:194 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | builtin-generics | 585 | 3.9 | modern | builtin-generics/modern_05.py | l = list[str]()
| pydocs | pep-0585.rst:198 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | builtin-generics | 585 | 3.9 | modern | builtin-generics/modern_06.py | list[str]
tuple[int, ...]
collections.ChainMap[str, list[str]]
| pydocs | pep-0585.rst:244 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | builtin-generics | 585 | 3.9 | legacy | builtin-generics/legacy_01.py | from typing import List, Tuple, ChainMap
List[str]
Tuple[int, ...]
ChainMap[str, List[str]]
| pydocs | pep-0585.rst:255 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | builtin-generics | 585 | 3.9 | modern | builtin-generics/modern_07.py | isinstance([1, 2, 3], list[str])
| pydocs | pep-0585.rst:323 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_01.py | from typing import Literal
def accepts_only_four(x: Literal[4]) -> None:
pass
accepts_only_four(4) # OK
accepts_only_four(19) # Rejected
| pydocs | pep-0586.rst:25 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_02.py | # Note: this is a simplification of the true type signature.
_PathType = Union[str, bytes, int]
@overload
def open(path: _PathType,
mode: Literal["r", "w", "a", "x", "r+", "w+", "a+", "x+"],
) -> IO[Text]: ...
@overload
def open(path: _PathType,
mode: Literal["rb", "wb", "ab", "xb", "r+b", "... | pydocs | pep-0586.rst:127 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_03.py | Literal[26]
Literal[0x1A] # Exactly equivalent to Literal[26]
Literal[-4]
Literal["hello world"]
Literal[b"hello world"]
Literal[u"hello world"]
Literal[True]
Literal[Color.RED] # Assuming Color is some enum
Literal[None]
| pydocs | pep-0586.rst:167 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_04.py | ReadOnlyMode = Literal["r", "r+"]
WriteAndTruncateMode = Literal["w", "w+", "wt", "w+t"]
WriteNoTruncateMode = Literal["r+", "r+t"]
AppendMode = Literal["a", "a+", "at", "a+t"]
AllModes = Literal[ReadOnlyMode, WriteAndTruncateMode,
WriteNoTruncateMode, AppendMode]
| pydocs | pep-0586.rst:184 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_05.py | Literal[Literal[Literal[1, 2, 3], "foo"], 5, None]
| pydocs | pep-0586.rst:198 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_06.py | Literal[1, 2, 3, "foo", 5, None]
| pydocs | pep-0586.rst:202 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_07.py | Optional[Literal[1, 2, 3, "foo", 5]]
| pydocs | pep-0586.rst:206 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_08.py | def my_function(x: Literal[1 + 2]) -> int:
return x * 3
x: Literal = 3
y: Literal[my_function] = my_function
| pydocs | pep-0586.rst:273 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_09.py | def expects_str(x: str) -> None: ...
var: Literal["foo"] = "foo"
# Legal: Literal["foo"] is a subtype of str
expects_str(var)
| pydocs | pep-0586.rst:362 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_10.py | def expects_literal(x: Literal["foo"]) -> None: ...
def runner(my_str: str) -> None:
# ILLEGAL: str is not a subclass of Literal["foo"]
expects_literal(my_str)
| pydocs | pep-0586.rst:371 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_11.py | a: Literal[0] = 0
b: Literal[5] = 5
some_tuple: Tuple[int, str, List[bool]] = (3, "abc", [True, False])
reveal_type(some_tuple[a]) # Revealed type is 'int'
some_tuple[b] # Error: 5 is not a valid index into the tuple
| pydocs | pep-0586.rst:395 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_12.py | class Test:
def __init__(self, param: int) -> None:
self.myfield = param
def mymethod(self, val: int) -> str: ...
a: Literal["myfield"] = "myfield"
b: Literal["mymethod"] = "mymethod"
c: Literal["blah"] = "blah"
t = Test()
reveal_type(getattr(t, a)) # Revealed type is 'int'
reveal_type(getattr(... | pydocs | pep-0586.rst:404 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_13.py | _PathType = Union[str, bytes, int]
@overload
def open(path: _PathType,
mode: Literal["r", "w", "a", "x", "r+", "w+", "a+", "x+"],
) -> IO[Text]: ...
@overload
def open(path: _PathType,
mode: Literal["rb", "wb", "ab", "xb", "r+b", "w+b", "a+b", "x+b"],
) -> IO[bytes]: ...
# Fallback... | pydocs | pep-0586.rst:432 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_14.py | A = TypeVar('A', bound=int)
B = TypeVar('B', bound=int)
C = TypeVar('C', bound=int)
# A simplified definition for Matrix[row, column]
class Matrix(Generic[A, B]):
def __add__(self, other: Matrix[A, B]) -> Matrix[A, B]: ...
def __matmul__(self, other: Matrix[B, C]) -> Matrix[A, C]: ...
def transpose(self) -... | pydocs | pep-0586.rst:469 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_15.py | T = TypeVar('T', Literal["a"], Literal["b"], Literal["c"])
S = TypeVar('S', bound=Literal["foo"])
| pydocs | pep-0586.rst:488 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_16.py | @overload
def is_int_like(x: Union[int, List[int]]) -> Literal[True]: ...
@overload
def is_int_like(x: object) -> bool: ...
def is_int_like(x): ...
vector: List[int] = [1, 2, 3]
if is_int_like(vector):
vector.append(3)
else:
vector.append("bad") # This branch is inferred to be unreachable
scalar: Union[int,... | pydocs | pep-0586.rst:570 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_17.py | def expects_three(x: Literal[3]) -> None: ...
expects_three(foo) # Type checks, since 'foo' is Final and equal to 3
| pydocs | pep-0586.rst:602 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | literal-types | 586 | 3.8 | modern | literal-types/modern_18.py | def foobar(arg1: Literal[1], arg2: Literal[True]) -> None:
pass
| pydocs | pep-0586.rst:668 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_01.py | from typing import TypedDict
class Movie(TypedDict):
name: str
year: int
| pydocs | pep-0589.rst:39 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_02.py | class EmptyDict(TypedDict):
pass
| pydocs | pep-0589.rst:161 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_03.py | class Strings(TypedDict):
items: List[str]
print(isinstance({'items': [1]}, Strings)) # Should be False
print(isinstance({'items': ['x']}, Strings)) # Should be True
| pydocs | pep-0589.rst:233 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_04.py | class BookBasedMovie(TypedDict):
name: str
year: int
based_on: str
| pydocs | pep-0589.rst:257 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_05.py | class X(TypedDict):
x: int
class Y(TypedDict):
y: str
class XYZ(X, Y):
z: bool
| pydocs | pep-0589.rst:264 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_06.py | class X(TypedDict):
x: str
class Y(X):
x: int # Type check error: cannot overwrite TypedDict field "x"
| pydocs | pep-0589.rst:284 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_07.py | class X(TypedDict):
x: int
class Y(TypedDict):
x: str
class XYZ(X, Y): # Type check error: cannot overwrite TypedDict field "x" while merging
xyz: bool
| pydocs | pep-0589.rst:298 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_08.py | class Movie(TypedDict, total=False):
name: str
year: int
| pydocs | pep-0589.rst:315 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_09.py | Movie = TypedDict('Movie', {'name': str, 'year': int})
| pydocs | pep-0589.rst:344 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_10.py | Movie = TypedDict('Movie',
{'name': str, 'year': int},
total=False)
| pydocs | pep-0589.rst:348 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_11.py | class A(TypedDict):
x: Optional[int]
class B(TypedDict):
x: int
def f(a: A) -> None:
a['x'] = None
b: B = {'x': 0}
f(b) # Type check error: 'B' not compatible with 'A'
b['x'] + 1 # Runtime error: None + 1
| pydocs | pep-0589.rst:393 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_12.py | class A(TypedDict, total=False):
x: int
class B(TypedDict):
x: int
def f(a: A) -> None:
del a['x']
b: B = {'x': 0}
f(b) # Type check error: 'B' not compatible with 'A'
b['x'] + 1 # Runtime KeyError: 'x'
| pydocs | pep-0589.rst:410 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_13.py | class A(TypedDict, total=False):
x: int
y: int
class B(TypedDict, total=False):
x: int
class C(TypedDict, total=False):
x: int
y: str
def f(a: A) -> None:
a['y'] = 1
def g(b: B) -> None:
f(b) # Type check error: 'B' incompatible with 'A'
c: C = {'x': 0, 'y': 'foo'}
g(c)
c['y'] + 'bar' ... | pydocs | pep-0589.rst:429 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_14.py | class A(TypedDict):
x: int
class B(A):
y: str
def f(d: Dict[str, int]) -> None:
d['y'] = 0
def g(a: A) -> None:
f(a) # Type check error: 'A' incompatible with Dict[str, int]
b: B = {'x': 0, 'y': 'foo'}
g(b)
b['y'] + 'bar' # Runtime error: int + str
| pydocs | pep-0589.rst:455 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_15.py | class A(TypedDict):
x: int
class B(TypedDict):
x: int
y: str
def sum_values(m: Mapping[str, int]) -> int:
n = 0
for v in m.values():
n += v # Runtime error
return n
def f(a: A) -> None:
sum_values(a) # Error: 'A' incompatible with Mapping[str, int]
b: B = {'x': 0, 'y': 'foo'}
f... | pydocs | pep-0589.rst:477 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_01.py | from typing import final
@final
class Base:
...
class Derived(Base): # Error: Cannot inherit from final class "Base"
...
| pydocs | pep-0591.rst:85 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_02.py | from typing import final
class Base:
@final
def foo(self) -> None:
...
class Derived(Base):
def foo(self) -> None: # Error: Cannot override final attribute "foo"
# (previously declared in base class "Base")
...
| pydocs | pep-0591.rst:96 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_03.py | from typing import Any, overload
class Base:
@overload
def method(self) -> None: ...
@overload
def method(self, arg: int) -> int: ...
@final
def method(self, x=None):
...
| pydocs | pep-0591.rst:112 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_04.py | ID: Final[float] = 1
| pydocs | pep-0591.rst:138 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_05.py | ID: Final = 1
| pydocs | pep-0591.rst:142 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_06.py | from typing import Final
RATE: Final = 3000
class Base:
DEFAULT_ID: Final = 0
RATE = 300 # Error: can't assign to final attribute
Base.DEFAULT_ID = 1 # Error: can't override a final attribute
| pydocs | pep-0591.rst:172 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_07.py | from typing import Final
class Window:
BORDER_WIDTH: Final = 2.5
...
class ListView(Window):
BORDER_WIDTH = 3 # Error: can't override a final attribute
| pydocs | pep-0591.rst:189 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_08.py | class ImmutablePoint:
x: Final[int]
y: Final[int] # Error: final attribute without an initializer
def __init__(self) -> None:
self.x = 1 # Good
| pydocs | pep-0591.rst:201 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_09.py | x: List[Final[int]] = [] # Error!
def fun(x: Final[List[int]]) -> None: # Error!
...
| pydocs | pep-0591.rst:216 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_10.py | x: Final = ['a', 'b']
x.append('c') # OK
y: Final[Sequence[str]] = ['a', 'b']
y.append('x') # Error: "Sequence[str]" has no attribute "append"
z: Final = ('a', 'b') # Also works
| pydocs | pep-0591.rst:226 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_11.py | from typing import NamedTuple, Final
X: Final = "x"
Y: Final = "y"
N = NamedTuple("N", [(X, int), (Y, int)])
| pydocs | pep-0591.rst:238 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | annotated | 593 | 3.9 | modern | annotated/modern_01.py | UnsignedShort = Annotated[int, struct2.ctype('H')]
SignedChar = Annotated[int, struct2.ctype('b')]
class Student(struct2.Packed):
# mypy typechecks 'name' field as 'str'
name: Annotated[str, struct2.ctype("<10s")]
serialnum: UnsignedShort
school: SignedChar
# 'unpack' only uses the metadata within the... | pydocs | pep-0593.rst:71 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | annotated | 593 | 3.9 | modern | annotated/modern_02.py | Currency = Annotated[
TypedDict('Currency', {'dollars': float, 'pounds': float}, total=False),
TaggedUnion,
]
| pydocs | pep-0593.rst:104 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | annotated | 593 | 3.9 | modern | annotated/modern_03.py | Annotated[int, ValueRange(3, 10), ctype("char")]
| pydocs | pep-0593.rst:132 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | annotated | 593 | 3.9 | modern | annotated/modern_04.py | Annotated[int, ValueRange(3, 10), ctype("char")] != Annotated[
int, ctype("char"), ValueRange(3, 10)
]
| pydocs | pep-0593.rst:140 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | annotated | 593 | 3.9 | modern | annotated/modern_05.py | Annotated[Annotated[int, ValueRange(3, 10)], ctype("char")] == Annotated[
int, ValueRange(3, 10), ctype("char")
]
| pydocs | pep-0593.rst:147 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | annotated | 593 | 3.9 | modern | annotated/modern_06.py | Annotated[int, ValueRange(3, 10)] != Annotated[
int, ValueRange(3, 10), ValueRange(3, 10)
]
| pydocs | pep-0593.rst:153 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | annotated | 593 | 3.9 | modern | annotated/modern_07.py | T1 = Annotated[int, ValueRange(-10, 5)]
T2 = Annotated[T1, ValueRange(-20, 3)]
| pydocs | pep-0593.rst:194 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | annotated | 593 | 3.9 | modern | annotated/modern_08.py | T2 = Annotated[int, ValueRange(-10, 5), ValueRange(-20, 3)]
| pydocs | pep-0593.rst:199 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | annotated | 593 | 3.9 | modern | annotated/modern_09.py | @struct2.packed
class Student(NamedTuple):
name: Annotated[str, struct.ctype("<10s")]
get_type_hints(Student) == {'name': str}
get_type_hints(Student, include_extras=False) == {'name': str}
get_type_hints(Student, include_extras=True) == {
'name': Annotated[str, struct.ctype("<10s")]
}
| pydocs | pep-0593.rst:210 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | annotated | 593 | 3.9 | modern | annotated/modern_10.py | T = TypeVar('T')
Const = Annotated[T, my_annotations.CONST]
class C:
def const_method(self: Const[List[int]]) -> int:
...
| pydocs | pep-0593.rst:227 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_01.py | isinstance(5, int | str)
issubclass(bool, int | float)
| pydocs | pep-0604.rst:52 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_02.py | isinstance(None, int | None)
isinstance(42, None | int)
| pydocs | pep-0604.rst:58 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_03.py | # Instead of
# def f(list: List[Union[int, str]], param: Optional[int]) -> Union[float, str]
def f(list: List[int | str], param: int | None) -> float | str:
pass
f([1, "abc"], None)
# Instead of typing.List[typing.Union[str, int]]
typing.List[str | int]
list[str | int]
# Instead of typing.Dict[str, typing.Union[... | pydocs | pep-0604.rst:70 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_04.py | int | str == typing.Union[int, str]
typing.Union[int, int] == int
int | int == int
| pydocs | pep-0604.rst:89 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_05.py | (int | str) == (str | int)
(int | str | float) == typing.Union[str, float, int]
| pydocs | pep-0604.rst:98 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_06.py | None | t == typing.Optional[t]
| pydocs | pep-0604.rst:106 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_07.py | str(int | list[str])
# int | list[str]
str(int | int)
# int
| pydocs | pep-0604.rst:112 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_08.py | # valid
isinstance("", int | str)
# invalid
isinstance(2, list[int]) # TypeError: isinstance() argument 2 cannot be a parameterized generic
isinstance(1, int | list[int])
# valid
issubclass(bool, int | float)
# invalid
issubclass(bool, bool | list[int])
| pydocs | pep-0604.rst:125 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_09.py | class M(type):
def __or__(self, other): return "Hello"
class C(metaclass=M): pass
C | int
int | C
Union[C, int]
| pydocs | pep-0604.rst:147 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_10.py | from __future__ import annotations
def foo() -> int | str: pass
eval(foo.__annotations__['return'])
| pydocs | pep-0604.rst:194 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | param-spec | 612 | 3.10 | legacy | param-spec/legacy_01.py | from typing import Awaitable, Callable, TypeVar
R = TypeVar("R")
def add_logging(f: Callable[..., R]) -> Callable[..., Awaitable[R]]:
async def inner(*args: object, **kwargs: object) -> R:
await log_to_database()
return f(*args, **kwargs)
return inner
@add_logging
def takes_int_str(x: int, y: str) -> int... | pydocs | pep-0612.rst:37 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | param-spec | 612 | 3.10 | modern | param-spec/modern_01.py | from typing import Awaitable, Callable, ParamSpec, TypeVar
P = ParamSpec("P")
R = TypeVar("R")
def add_logging(f: Callable[P, R]) -> Callable[P, Awaitable[R]]:
async def inner(*args: P.args, **kwargs: P.kwargs) -> R:
await log_to_database()
return f(*args, **kwargs)
return inner
@add_logging
def takes_in... | pydocs | pep-0612.rst:87 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | param-spec | 612 | 3.10 | legacy | param-spec/legacy_02.py | class Request:
...
def with_request(f: Callable[..., R]) -> Callable[..., R]:
def inner(*args: object, **kwargs: object) -> R:
return f(Request(), *args, **kwargs)
return inner
@with_request
def takes_int_str(request: Request, x: int, y: str) -> int:
# use request
return x + 7
takes_int_str(1, "A")
tak... | pydocs | pep-0612.rst:111 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | param-spec | 612 | 3.10 | modern | param-spec/modern_02.py | from typing import Concatenate
def with_request(f: Callable[Concatenate[Request, P], R]) -> Callable[P, R]:
def inner(*args: P.args, **kwargs: P.kwargs) -> R:
return f(Request(), *args, **kwargs)
return inner
@with_request
def takes_int_str(request: Request, x: int, y: str) -> int:
# use request
return x ... | pydocs | pep-0612.rst:133 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | param-spec | 612 | 3.10 | modern | param-spec/modern_03.py | from typing import ParamSpec
P = ParamSpec("P") # Accepted
P = ParamSpec("WrongName") # Rejected because P =/= WrongName
| pydocs | pep-0612.rst:163 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | param-spec | 612 | 3.10 | modern | param-spec/modern_04.py | def foo(x: P) -> P: ... # Rejected
def foo(x: Concatenate[int, P]) -> int: ... # Rejected
def foo(x: typing.List[P]) -> None: ... # Rejected
def foo(x: Callable[[int, str], P]) -> None: ... # Rejected
| pydocs | pep-0612.rst:202 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | param-spec | 612 | 3.10 | modern | param-spec/modern_05.py | T = TypeVar("T")
P_2 = ParamSpec("P_2")
class X(Generic[T, P]):
f: Callable[P, int]
x: T
def f(x: X[int, P_2]) -> str: ... # Accepted
def f(x: X[int, Concatenate[int, P_2]]) -> str: ... # Accepted
def f(x: X[int, [int, bool]]) -> str: ... # Accepted
def f(x: X[int, ...]) -> str: ...... | pydocs | pep-0612.rst:218 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.13 | param-spec | 612 | 3.10 | modern | param-spec/modern_06.py | P = ParamSpec("P")
def foo(x: Callable[P, int], y: Callable[P, int]) -> Callable[P, bool]: ...
def x_y(x: int, y: str) -> int: ...
def y_x(y: int, x: str) -> int: ...
foo(x_y, x_y) # Should return (x: int, y: str) -> bool
foo(x_y, y_x) # Could return (__a: int, __b: str) -> bool
# This works becaus... | pydocs | pep-0612.rst:279 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.