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 916
values | source stringclasses 1
value | origin stringlengths 15 17 | origin_revision stringclasses 10
values | license stringclasses 1
value |
|---|---|---|---|---|---|---|---|---|---|---|---|
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | type-alias | 613 | 3.10 | legacy | type-alias/legacy_01.py | MyType = "ClassName"
def foo() -> MyType: ...
| pydocs | pep-0613.rst:53 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | type-alias | 613 | 3.10 | modern | type-alias/modern_01.py | MyType: TypeAlias = "ClassName"
def foo() -> MyType: ...
| pydocs | pep-0613.rst:64 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | type-alias | 613 | 3.10 | modern | type-alias/modern_02.py | MyType1: TypeAlias = InvalidType
MyType2: TypeAlias = MyGeneric(int)
| pydocs | pep-0613.rst:89 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | type-alias | 613 | 3.10 | legacy | type-alias/legacy_03.py | x = ClassName
def foo() -> None:
x = ClassName
| pydocs | pep-0613.rst:114 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | type-alias | 613 | 3.10 | modern | type-alias/modern_06.py | MyType: TypeAlias[int]
| pydocs | pep-0613.rst:200 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | type-alias | 613 | 3.10 | modern | type-alias/modern_07.py | MyType = TypeAlias[int]
| pydocs | pep-0613.rst:206 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_02.py | from typing import TypeVarTuple
Ts = TypeVarTuple('Ts')
| pydocs | pep-0646.rst:180 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_08.py | class Array(Generic[*Ts1, *Ts2]): ... # Error
| pydocs | pep-0646.rst:328 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_23.py | def foo(*args: *Ts) -> None: ...
| pydocs | pep-0646.rst:605 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_30.py | IntFloatsTuple = IntTuple[*Tuple[float, ...]] # Valid
| pydocs | pep-0646.rst:758 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_36.py | Elderberries[*Tuple[int, ...], str]
| pydocs | pep-0646.rst:869 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_37.py | Elderberries[str, *Tuple[int, ...]]
| pydocs | pep-0646.rst:879 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | type-guard | 647 | 3.10 | modern | type-guard/modern_05.py | def f(value: int) -> TypeGuard[str]:
return True
| pydocs | pep-0647.rst:300 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_01.py | class Movie(TypedDict):
title: str
year: NotRequired[int]
| pydocs | pep-0655.rst:63 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_07.py | Movie = TypedDict('Movie', {'name': str, 'year': NotRequired[int]})
| pydocs | pep-0655.rst:175 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_16.py | from typing import NotRequired, TypedDict
class Dog(TypedDict):
name: str
owner: NotRequired[str|None]
| pydocs | pep-0655.rst:356 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_17.py | from __future__ import annotations # for Python 3.7-3.9
from typing_extensions import NotRequired, TypedDict # for Python <3.11 with (Not)Required
class Dog(TypedDict):
name: str
owner: NotRequired[str|None]
| pydocs | pep-0655.rst:367 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | required-notrequired | 655 | 3.11 | modern | required-notrequired/modern_18.py | from typing import TypedDict # oops: should import from typing_extensions instead
from typing_extensions import NotRequired
class Movie(TypedDict):
title: str
year: NotRequired[int]
assert Movie.__required_keys__ == frozenset({'title', 'year'}) # yikes
assert Movie.__optional_keys__ == frozenset() # yikes
| pydocs | pep-0655.rst:380 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | legacy | self-type/legacy_01.py | from typing import TypeVar
TShape = TypeVar("TShape", bound="Shape")
class Shape:
def set_scale(self: TShape, scale: float) -> TShape:
self.scale = scale
return self
class Circle(Shape):
def set_radius(self, radius: float) -> Circle:
self.radius = radius
return self
Circle()... | pydocs | pep-0673.rst:80 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | modern | self-type/modern_01.py | from typing import Self
class Shape:
def set_scale(self, scale: float) -> Self:
self.scale = scale
return self
class Circle(Shape):
def set_radius(self, radius: float) -> Self:
self.radius = radius
return self
| pydocs | pep-0673.rst:113 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | modern | self-type/modern_02.py | from typing import Self
class Shape:
def set_scale(self, scale: float) -> Self:
self.scale = scale
return self
| pydocs | pep-0673.rst:163 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | legacy | self-type/legacy_02.py | from typing import TypeVar
SelfShape = TypeVar("SelfShape", bound="Shape")
class Shape:
def set_scale(self: SelfShape, scale: float) -> SelfShape:
self.scale = scale
return self
| pydocs | pep-0673.rst:174 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | modern | self-type/modern_03.py | class Circle(Shape):
def set_radius(self, radius: float) -> Self:
self.radius = radius
return self
| pydocs | pep-0673.rst:187 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | legacy | self-type/legacy_03.py | SelfCircle = TypeVar("SelfCircle", bound="Circle")
class Circle(Shape):
def set_radius(self: SelfCircle, radius: float) -> SelfCircle:
self.radius = radius
return self
| pydocs | pep-0673.rst:196 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | legacy | self-type/legacy_04.py | Self = TypeVar("Self", bound="Shape")
class Shape:
@classmethod
def from_config(
cls: type[Self], config: dict[str, float]
) -> Self:
return cls(config["scale"])
| pydocs | pep-0673.rst:248 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | modern | self-type/modern_04.py | from typing import Self
class Shape:
@classmethod
def from_config(cls, config: dict[str, float]) -> Self:
return cls(config["scale"])
| pydocs | pep-0673.rst:261 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | legacy | self-type/legacy_05.py | Self = TypeVar("Self", bound="Shape")
class Shape:
def difference(self: Self, other: Self) -> float: ...
def apply(self: Self, f: Callable[[Self], None]) -> None: ...
| pydocs | pep-0673.rst:280 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | modern | self-type/modern_05.py | from typing import Self
class Shape:
def difference(self, other: Self) -> float: ...
def apply(self, f: Callable[[Self], None]) -> None: ...
| pydocs | pep-0673.rst:291 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | modern | self-type/modern_06.py | class Shape:
def difference(self: Self, other: Self) -> float: ...
| pydocs | pep-0673.rst:303 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | modern | self-type/modern_07.py | from typing import Self
@dataclass
class LinkedList(Generic[T]):
value: T
next: Self | None = None
@dataclass
class OrdinalLinkedList(LinkedList[int]):
def ordinal_value(self) -> str:
return as_ordinal(self.value)
xs = OrdinalLinkedList(value=1, next=LinkedList[int](value=2))
# Type error: Expect... | pydocs | pep-0673.rst:352 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | legacy | self-type/legacy_06.py | from dataclasses import dataclass
from typing import Any, Generic, TypeVar
T = TypeVar("T")
Self = TypeVar("Self", bound="LinkedList")
class LinkedList(Generic[T]):
value: T
@property
def next(self: Self) -> Self | None:
return self._next
@next.setter
def next(self: Self, next: Self | N... | pydocs | pep-0673.rst:378 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | modern | self-type/modern_08.py | class Container(Generic[T]):
value: T
def set_value(self, value: T) -> Self: ...
| pydocs | pep-0673.rst:407 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | legacy | self-type/legacy_07.py | Self = TypeVar("Self", bound="Container[Any]")
class Container(Generic[T]):
value: T
def set_value(self: Self, value: T) -> Self: ...
| pydocs | pep-0673.rst:416 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | modern | self-type/modern_09.py | class Container(Generic[T]):
def foo(
self, other: Self[int], other2: Self,
) -> Self[str]: # Rejected
...
| pydocs | pep-0673.rst:457 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | modern | self-type/modern_10.py | from typing import Protocol, Self
class ShapeProtocol(Protocol):
scale: float
def set_scale(self, scale: float) -> Self:
self.scale = scale
return self
| pydocs | pep-0673.rst:482 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | legacy | self-type/legacy_08.py | from typing import TypeVar
SelfShape = TypeVar("SelfShape", bound="ShapeProtocol")
class ShapeProtocol(Protocol):
scale: float
def set_scale(self: SelfShape, scale: float) -> SelfShape:
self.scale = scale
return self
| pydocs | pep-0673.rst:495 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.