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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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 |
train | 3.12 | param-spec | 612 | 3.10 | modern | param-spec/modern_07.py | def bar(x: int, *args: bool) -> int: ...
def add(x: Callable[P, int]) -> Callable[Concatenate[str, P], bool]: ...
add(bar) # Should return (__a: str, x: int, *args: bool) -> bool
def remove(x: Callable[Concatenate[int, P], int]) -> Callable[P, bool]: ...
remove(bar) # Should return (*args: bool) -> bool
d... | pydocs | pep-0612.rst:324 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | param-spec | 612 | 3.10 | modern | param-spec/modern_08.py | def expects_int_first(x: Callable[Concatenate[int, P], int]) -> None: ...
@expects_int_first # Rejected
def one(x: str) -> int: ...
@expects_int_first # Rejected
def two(*, x: int) -> int: ...
@expects_int_first # Rejected
def three(**kwargs: int) -> int: ...
@expects_int_first # Accepted
def four(*args: int) -> in... | pydocs | pep-0612.rst:347 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | param-spec | 612 | 3.10 | modern | param-spec/modern_09.py | def puts_p_into_scope(f: Callable[P, int]) -> None:
def inner(*args: P.args, **kwargs: P.kwargs) -> None: # Accepted
pass
def mixed_up(*args: P.kwargs, **kwargs: P.args) -> None: # Rejected
pass
def misplaced(x: P.args) -> None: # Rejected
pass
def out_of_scope(*arg... | pydocs | pep-0612.rst:390 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | param-spec | 612 | 3.10 | modern | param-spec/modern_10.py | def puts_p_into_scope(f: Callable[P, int]) -> None:
stored_args: P.args # Rejected
stored_kwargs: P.kwargs # Rejected
def just_args(*args: P.args) -> None: # Rejected
pass
def just_kwargs(**kwargs: P.kwargs) -> None: # Rejected
pass
| pydocs | pep-0612.rst:414 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | param-spec | 612 | 3.10 | modern | param-spec/modern_11.py | def decorator(f: Callable[P, int]) -> Callable[P, None]:
def foo(*args: P.args, **kwargs: P.kwargs) -> None:
f(*args, **kwargs) # Accepted, should resolve to int
f(*kwargs, **args) # Rejected
f(1, *args, **kwargs) # Rejected
return foo # Accepted
| pydocs | pep-0612.rst:454 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | param-spec | 612 | 3.10 | modern | param-spec/modern_12.py | def add(f: Callable[P, int]) -> Callable[Concatenate[str, P], None]:
def foo(s: str, *args: P.args, **kwargs: P.kwargs) -> None: # Accepted
pass
def bar(*args: P.args, s: str, **kwargs: P.kwargs) -> None: # Rejected
pass
return foo # Accepted
def re... | pydocs | pep-0612.rst:479 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | param-spec | 612 | 3.10 | modern | param-spec/modern_13.py | def outer(f: Callable[P, None]) -> Callable[P, None]:
def foo(x: int, *args: P.args, **kwargs: P.kwargs) -> None:
f(*args, **kwargs)
def bar(*args: P.args, **kwargs: P.kwargs) -> None:
foo(1, *args, **kwargs) # Accepted
foo(x=1, *args, **kwargs) # Rejected
return bar
| pydocs | pep-0612.rst:507 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | param-spec | 612 | 3.10 | modern | param-spec/modern_14.py | def twice(f: Callable[P, int], *args: P.args, **kwargs: P.kwargs) -> int:
return f(*args, **kwargs) + f(*args, **kwargs)
| pydocs | pep-0612.rst:545 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | param-spec | 612 | 3.10 | modern | param-spec/modern_15.py | def add_n(f: Callable[P, R]) -> Callable[Concatenate[("n", int), P], R]:
def inner(*args: P.args, n: int, **kwargs: P.kwargs) -> R:
# use n
return f(*args, **kwargs)
return inner
| pydocs | pep-0612.rst:714 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | param-spec | 612 | 3.10 | modern | param-spec/modern_16.py | def innocent_wrapper(f: Callable[P, R]) -> Callable[P, R]:
def inner(*args: P.args, **kwargs: P.kwargs) -> R:
added = add_n(f)
return added(*args, n=1, **kwargs)
return inner
@innocent_wrapper
def problem(n: int) -> None:
pass
| pydocs | pep-0612.rst:728 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-alias | 613 | 3.10 | legacy | type-alias/legacy_01.py | MyType = "ClassName"
def foo() -> MyType: ...
| pydocs | pep-0613.rst:53 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-alias | 613 | 3.10 | modern | type-alias/modern_01.py | MyType: TypeAlias = "ClassName"
def foo() -> MyType: ...
| pydocs | pep-0613.rst:64 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-alias | 613 | 3.10 | legacy | type-alias/legacy_02.py | MyType1 = InvalidType
MyType2 = MyGeneric(int) # i.e., intention was MyGeneric[int]
| pydocs | pep-0613.rst:77 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-alias | 613 | 3.10 | modern | type-alias/modern_02.py | MyType1: TypeAlias = InvalidType
MyType2: TypeAlias = MyGeneric(int)
| pydocs | pep-0613.rst:89 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-alias | 613 | 3.10 | modern | type-alias/modern_03.py | class Foo:
x = ClassName
y: TypeAlias = ClassName
z: Type[ClassName] = ClassName
| pydocs | pep-0613.rst:103 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-alias | 613 | 3.10 | legacy | type-alias/legacy_03.py | x = ClassName
def foo() -> None:
x = ClassName
| pydocs | pep-0613.rst:114 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-alias | 613 | 3.10 | modern | type-alias/modern_04.py | x: TypeAlias = ClassName
def foo() -> None:
x = ClassName
def bar() -> None:
x: TypeAlias = ClassName
| pydocs | pep-0613.rst:127 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-alias | 613 | 3.10 | legacy | type-alias/legacy_04.py | x = 1 # untyped global expression
x: int = 1 # typed global expression
x = int # type alias
x: Type[int] = int # typed global expression
| pydocs | pep-0613.rst:152 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-alias | 613 | 3.10 | modern | type-alias/modern_05.py | x = 1 # untyped global expression
x: int = 1 # typed global expression
x = int # untyped global expression (see note below)
x: Type[int] = int # typed global expression
x: TypeAlias = int # type alias
x: TypeAlias = "MyClass" # type alias
| pydocs | pep-0613.rst:163 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-alias | 613 | 3.10 | modern | type-alias/modern_06.py | MyType: TypeAlias[int]
| pydocs | pep-0613.rst:200 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-alias | 613 | 3.10 | modern | type-alias/modern_07.py | MyType = TypeAlias[int]
| pydocs | pep-0613.rst:206 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | legacy | variadic-generics/legacy_01.py | Axis1 = TypeVar('Axis1')
Axis2 = TypeVar('Axis2')
class Array1(Generic[Axis1]): ...
class Array2(Generic[Axis1, Axis2]): ...
| pydocs | pep-0646.rst:94 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_01.py | from typing import TypeVar, TypeVarTuple
DType = TypeVar('DType')
Shape = TypeVarTuple('Shape')
class Array(Generic[DType, *Shape]):
def __abs__(self) -> Array[DType, *Shape]: ...
def __add__(self, other: Array[DType, *Shape]) -> Array[DType, *Shape]: ...
| pydocs | pep-0646.rst:116 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_02.py | from typing import TypeVarTuple
Ts = TypeVarTuple('Ts')
| pydocs | pep-0646.rst:180 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_03.py | Shape = TypeVarTuple('Shape')
class Array(Generic[*Shape]): ...
Height = NewType('Height', int)
Width = NewType('Width', int)
x: Array[Height, Width] = Array()
| pydocs | pep-0646.rst:192 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_04.py | class Array(Generic[*Shape]):
def __init__(self, shape: Tuple[*Shape]):
self._shape: Tuple[*Shape] = shape
def get_shape(self) -> Tuple[*Shape]:
return self._shape
shape = (Height(480), Width(640))
x: Array[Height, Width] = Array(shape)
y = abs(x) # Inferred type is Array[Height, Width]
z = ... | pydocs | pep-0646.rst:228 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_05.py | # Unpacking using the star operator in new versions of Python
class Array(Generic[*Shape]): ...
# Unpacking using ``Unpack`` in older versions of Python
class Array(Generic[Unpack[Shape]]): ...
| pydocs | pep-0646.rst:269 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_06.py | def foo(arg1: Tuple[*Ts], arg2: Tuple[*Ts]): ...
a = (0,)
b = ('0',)
foo(a, b) # Can Ts be bound to Tuple[int | str]?
| pydocs | pep-0646.rst:296 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_07.py | def pointwise_multiply(
x: Array[*Shape],
y: Array[*Shape]
) -> Array[*Shape]: ...
x: Array[Height]
y: Array[Width]
z: Array[Height, Width]
pointwise_multiply(x, x) # Valid
pointwise_multiply(x, y) # Error
pointwise_multiply(x, z) # Error
| pydocs | pep-0646.rst:309 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_08.py | class Array(Generic[*Ts1, *Ts2]): ... # Error
| pydocs | pep-0646.rst:328 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_09.py | Shape = TypeVarTuple('Shape')
Batch = NewType('Batch', int)
Channels = NewType('Channels', int)
def add_batch_axis(x: Array[*Shape]) -> Array[Batch, *Shape]: ...
def del_batch_axis(x: Array[Batch, *Shape]) -> Array[*Shape]: ...
def add_batch_channels(
x: Array[*Shape]
) -> Array[Batch, *Shape, Channels]: ...
a: Arr... | pydocs | pep-0646.rst:343 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_10.py | T = TypeVar('T')
Ts = TypeVarTuple('Ts')
def prefix_tuple(
x: T,
y: Tuple[*Ts]
) -> Tuple[T, *Ts]: ...
z = prefix_tuple(x=0, y=(True, 'a'))
# Inferred type of z is Tuple[int, bool, str]
| pydocs | pep-0646.rst:363 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_11.py | def process_batch_channels(
x: Array[Batch, *Tuple[Any, ...], Channels]
) -> None:
...
x: Array[Batch, Height, Width, Channels]
process_batch_channels(x) # OK
y: Array[Batch, Channels]
process_batch_channels(y) # OK
z: Array[Batch]
process_batch_channels(z) # Error: Expected Channels.
| pydocs | pep-0646.rst:408 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_12.py | y: Array[*Tuple[Any, ...]] = read_from_file()
def expect_variadic_array(
x: Array[Batch, *Shape]
) -> None: ...
expect_variadic_array(y) # OK
def expect_precise_array(
x: Array[Batch, Height, Width, Channels]
) -> None: ...
expect_precise_array(y) # OK
| pydocs | pep-0646.rst:430 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_13.py | x: Tuple[int, *Ts, str, *Ts2] # Error
y: Tuple[int, *Tuple[int, ...], str, *Tuple[str, ...]] # Error
| pydocs | pep-0646.rst:466 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_14.py | Ts = TypeVarTuple('Ts')
def args_to_tuple(*args: *Ts) -> Tuple[*Ts]: ...
args_to_tuple(1, 'a') # Inferred type is Tuple[int, str]
| pydocs | pep-0646.rst:483 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_15.py | # os.execle takes arguments 'path, arg0, arg1, ..., env'
def execle(path: str, *args: *Tuple[*Ts, Env]) -> None: ...
| pydocs | pep-0646.rst:499 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_16.py | def execle(path: str, *args: *Ts, env: Env) -> None: ...
| pydocs | pep-0646.rst:506 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_17.py | def foo(*args: *Tuple[int, ...]) -> None: ...
# equivalent to:
def foo(*args: int) -> None: ...
| pydocs | pep-0646.rst:517 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_18.py | def foo(*args: *Tuple[int, *Tuple[str, ...], str]) -> None: ...
| pydocs | pep-0646.rst:528 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_19.py | def foo(*args: *Tuple[int, str]) -> None: ...
foo(1, "hello") # OK
| pydocs | pep-0646.rst:535 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_20.py | def foo(*args: Tuple[*Ts]): ...
foo((0,), (1,)) # Valid
foo((0,), (1, 2)) # Error
foo((0,), ('1',)) # Error
| pydocs | pep-0646.rst:554 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_21.py | class Process:
def __init__(
self,
target: Callable[[*Ts], None],
args: Tuple[*Ts],
) -> None: ...
def func(arg1: int, arg2: str) -> None: ...
Process(target=func, args=(0, 'foo')) # Valid
Process(target=func, args=('foo', 0)) # Error
| pydocs | pep-0646.rst:577 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_22.py | T = TypeVar('T')
def foo(f: Callable[[int, *Ts, T], Tuple[T, *Ts]]): ...
| pydocs | pep-0646.rst:594 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_23.py | def foo(*args: *Ts) -> None: ...
| pydocs | pep-0646.rst:605 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_24.py | def foo(*args: *Tuple[int, *Ts, T]) -> Tuple[T, *Ts]: ...
| pydocs | pep-0646.rst:612 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_25.py | def takes_any_array(arr: Array): ...
# equivalent to:
def takes_any_array(arr: Array[*Tuple[Any, ...]]): ...
x: Array[Height, Width]
takes_any_array(x) # Valid
y: Array[Time, Height, Width]
takes_any_array(y) # Also valid
| pydocs | pep-0646.rst:623 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_26.py | IntTuple = Tuple[int, *Ts]
NamedArray = Tuple[str, Array[*Ts]]
IntTuple[float, bool] # Equivalent to Tuple[int, float, bool]
NamedArray[Height] # Equivalent to Tuple[str, Array[Height]]
| pydocs | pep-0646.rst:663 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_27.py | Shape = TypeVarTuple('Shape')
DType = TypeVar('DType')
class Array(Generic[DType, *Shape]):
...
# E.g. Float32Array[Height, Width, Channels]
Float32Array = Array[np.float32, *Shape]
# E.g. Array1D[np.uint8]
Array1D = Array[DType, Any]
| pydocs | pep-0646.rst:678 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_28.py | T = TypeVar('T')
Foo = Tuple[T, *Ts]
# T bound to str, Ts to Tuple[int]
Foo[str, int]
# T bound to float, Ts to Tuple[()]
Foo[float]
# T bound to Any, Ts to an Tuple[Any, ...]
Foo
| pydocs | pep-0646.rst:716 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_29.py | Ts1 = TypeVar('Ts1')
Ts2 = TypeVar('Ts2')
IntTuple = Tuple[int, *Ts1]
IntFloatTuple = IntTuple[float, *Ts2] # Valid
| pydocs | pep-0646.rst:743 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_30.py | IntFloatsTuple = IntTuple[*Tuple[float, ...]] # Valid
| pydocs | pep-0646.rst:758 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_31.py | T = TypeVar('T')
IntTuple = Tuple[int, T]
IntTuple[str] # Valid
IntTuple[*Ts] # NOT valid
IntTuple[*Tuple[float, ...]] # NOT valid
| pydocs | pep-0646.rst:773 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_32.py | T = TypeVar('T')
Foo = Tuple[T, *Ts]
Foo[str, int] # T bound to str, Ts to Tuple[int]
Foo[str, int, float] # T bound to str, Ts to Tuple[int, float]
| pydocs | pep-0646.rst:794 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_33.py | T1 = TypeVar('T1')
T2 = TypeVar('T2')
T3 = TypeVar('T3')
Tuple[*Ts, T1, T2] # Valid
Tuple[T1, T2, *Ts] # Valid
Tuple[T1, *Ts, T2, T3] # Valid
| pydocs | pep-0646.rst:807 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_34.py | Shrubbery = Tuple[*Ts, T1, T2]
Shrubbery[str, bool] # T2=bool, T1=str, Ts=Tuple[()]
Shrubbery[str, bool, float] # T2=float, T1=bool, Ts=Tuple[str]
Shrubbery[str, bool, float, int] # T2=int, T1=float, Ts=Tuple[str, bool]
Ptang = Tuple[T1, *Ts, T2, T3]
Ptang[str, bool, float] # T1=str, ... | pydocs | pep-0646.rst:822 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_35.py | Elderberries = Tuple[*Ts, T1]
Hamster = Elderberries[*Tuple[int, ...]] # valid
| pydocs | pep-0646.rst:850 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_36.py | Elderberries[*Tuple[int, ...], str]
| pydocs | pep-0646.rst:869 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_37.py | Elderberries[str, *Tuple[int, ...]]
| pydocs | pep-0646.rst:879 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_38.py | Ts1 = TypeVarTuple('Ts1')
Ts2 = TypeVarTuple('Ts2')
Camelot = Tuple[T, *Ts1]
Camelot[*Ts2] # NOT valid
| pydocs | pep-0646.rst:896 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_39.py | Shape = TypeVarTuple('Shape')
Axis1 = TypeVar('Axis1')
Axis2 = TypeVar('Axis2')
Axis3 = TypeVar('Axis3')
class Array(Generic[*Shape]):
@overload
def transpose(
self: Array[Axis1, Axis2]
) -> Array[Axis2, Axis1]: ...
@overload
def transpose(
self: Array[Axis1, Axis2, Axis3]
) -> Array[Axis3, Axis2... | pydocs | pep-0646.rst:915 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | legacy | variadic-generics/legacy_02.py | class Array1(Generic[Axis1]): ...
class Array2(Generic[Axis1, Axis2]): ...
| pydocs | pep-0646.rst:959 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_40.py | DType = TypeVar('DType')
Shape = TypeVarTuple('Shape')
class Array(Generic[DType, *Shape]):
...
| pydocs | pep-0646.rst:1077 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_41.py | idxs = (1, 2)
array_slice = array[0, *idxs, -1] # Equivalent to [0, 1, 2, -1]
array[0, *idxs, -1] = array_slice # Also allowed
| pydocs | pep-0646.rst:1146 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_42.py | array[*idxs_to_select, *idxs_to_select] # Equivalent to array[1, 2, 1, 2]
| pydocs | pep-0646.rst:1154 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_43.py | array[3:5, *idxs_to_select] # Equivalent to array[3:5, 1, 2]
| pydocs | pep-0646.rst:1164 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_44.py | Ts = TypeVarTuple('Ts')
def foo(*args: *Ts): pass
foo.__annotations__
| pydocs | pep-0646.rst:1228 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_45.py | foo = [1]
def bar(*args: *foo): pass
bar.__annotations__
foo = [1, 2]
def bar(*args: *foo): pass
| pydocs | pep-0646.rst:1281 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-guard | 647 | 3.10 | modern | type-guard/modern_01.py | from typing import TypeGuard
def is_str_list(val: List[object]) -> TypeGuard[List[str]]:
"""Determines whether all objects in the list are strings"""
return all(isinstance(x, str) for x in val)
| pydocs | pep-0647.rst:103 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-guard | 647 | 3.10 | modern | type-guard/modern_02.py | class Person(TypedDict):
name: str
age: int
def is_person(val: dict) -> "TypeGuard[Person]":
try:
return isinstance(val["name"], str) and isinstance(val["age"], int)
except KeyError:
return False
def print_age(val: dict):
if is_person(val):
print(f"Age: {val['age']}")
e... | pydocs | pep-0647.rst:115 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-guard | 647 | 3.10 | modern | type-guard/modern_03.py | _T = TypeVar("_T")
def is_two_element_tuple(val: Tuple[_T, ...]) -> TypeGuard[Tuple[_T, _T]]:
return len(val) == 2
def func(names: Tuple[str, ...]):
if is_two_element_tuple(names):
reveal_type(names) # Tuple[str, str]
else:
reveal_type(names) # Tuple[str, ...]
| pydocs | pep-0647.rst:159 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-guard | 647 | 3.10 | modern | type-guard/modern_04.py | def is_str_list(val: List[object], allow_empty: bool) -> TypeGuard[List[str]]:
if len(val) == 0:
return allow_empty
return all(isinstance(x, str) for x in val)
_T = TypeVar("_T")
def is_set_of(val: Set[Any], type: Type[_T]) -> TypeGuard[Set[_T]]:
return all(isinstance(x, type) for x in val)
| pydocs | pep-0647.rst:185 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-guard | 647 | 3.10 | modern | type-guard/modern_05.py | def f(value: int) -> TypeGuard[str]:
return True
| pydocs | pep-0647.rst:300 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_01.py | class Movie(TypedDict):
title: str
year: NotRequired[int]
| pydocs | pep-0655.rst:63 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_02.py | Actor = TypedDict('Actor', {
'name': str,
# "in" is a keyword, so the functional syntax is necessary
'in': NotRequired[List[str]],
})
| pydocs | pep-0655.rst:75 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_03.py | class Movie(TypedDict, total=False):
title: Required[str]
year: int
| pydocs | pep-0655.rst:129 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_04.py | class Movie(TypedDict): # implicitly total=True
title: str
year: NotRequired[int]
| pydocs | pep-0655.rst:139 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_05.py | class Movie(TypedDict):
title: Required[str] # redundant
year: NotRequired[int]
| pydocs | pep-0655.rst:152 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_06.py | class Movie(TypedDict):
title: str
year: NotRequired[Required[int]] # ERROR
| pydocs | pep-0655.rst:161 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_07.py | Movie = TypedDict('Movie', {'name': str, 'year': NotRequired[int]})
| pydocs | pep-0655.rst:175 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_08.py | class _MovieBase(TypedDict):
title: str
class Movie(_MovieBase):
year: NotRequired[int]
| pydocs | pep-0655.rst:200 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_09.py | class Movie(TypedDict):
title: str
year: NotRequired[Annotated[int, ValueRange(-9999, 9999)]] # ok
| pydocs | pep-0655.rst:215 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_10.py | class Movie(TypedDict):
title: str
year: Annotated[NotRequired[int], ValueRange(-9999, 9999)] # ok
| pydocs | pep-0655.rst:221 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_11.py | class Movie(TypedDict):
title: str
year: NotRequired[int]
assert get_type_hints(Movie) == \
{'title': str, 'year': int}
assert get_type_hints(Movie, include_extras=True) == \
{'title': str, 'year': NotRequired[int]}
| pydocs | pep-0655.rst:250 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_12.py | assert get_origin(Required[int]) is Required
assert get_args(Required[int]) == (int,)
assert get_origin(NotRequired[int]) is NotRequired
assert get_args(NotRequired[int]) == (int,)
| pydocs | pep-0655.rst:268 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_13.py | from __future__ import annotations # for Python 3.7-3.9
class Dog(TypedDict):
name: str
owner: NotRequired[str|None]
| pydocs | pep-0655.rst:317 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_14.py | class Dog(TypedDict):
name: str
owner: 'NotRequired[str|None]'
| pydocs | pep-0655.rst:328 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_15.py | class Dog(TypedDict):
name: str
# ick; avoid using both Optional and NotRequired
owner: NotRequired[Optional[str]]
| pydocs | pep-0655.rst:337 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.