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.15 | literal-string | 675 | 3.11 | modern | literal-string/modern_15.py | def run(command: LiteralString, *args: str, shell: bool=...): ...
| pydocs | pep-0675.rst:868 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-string | 675 | 3.11 | modern | literal-string/modern_16.py | def mark_safe(s: LiteralString) -> str: ...
| pydocs | pep-0675.rst:899 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-string | 675 | 3.11 | modern | literal-string/modern_17.py | class Template:
def __init__(self, source: LiteralString): ...
| pydocs | pep-0675.rst:933 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-string | 675 | 3.11 | modern | literal-string/modern_18.py | def info(msg: LiteralString, *args: object) -> None:
...
| pydocs | pep-0675.rst:969 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-string | 675 | 3.11 | modern | literal-string/modern_19.py | # before
def capitalize(self) -> str: ...
# after
@overload
def capitalize(self: LiteralString) -> LiteralString: ...
@overload
def capitalize(self) -> str: ...
| pydocs | pep-0675.rst:1047 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-string | 675 | 3.11 | modern | literal-string/modern_20.py | @overload
def capitalize(self: LiteralString) -> LiteralString: ...
@overload
def capitalize(self) -> str: ...
@overload
def casefold(self: LiteralString) -> LiteralString: ...
@overload
def casefold(self) -> str: ...
@overload
def center(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = ...) -... | pydocs | pep-0675.rst:1068 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-string | 675 | 3.11 | modern | literal-string/modern_21.py | # OK
@overload
def my_transform(x: LiteralString, y: Literal["a", "b"]) -> LiteralString: ...
@overload
def my_transform(x: str, y: str) -> str: ...
# Not OK
@overload
def my_transform(x: LiteralString, y: str) -> LiteralString: ...
@overload
def my_transform(x: str, y: str) -> str: ...
| pydocs | pep-0675.rst:1252 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclass-transform | 681 | 3.11 | modern | dataclass-transform/modern_01.py | _T = TypeVar("_T")
# The ``create_model`` decorator is defined by a library.
# This could be in a type stub or inline.
@typing.dataclass_transform()
def create_model(cls: Type[_T]) -> Type[_T]:
cls.__init__ = ...
cls.__eq__ = ...
cls.__ne__ = ...
return cls
# The ``create_model`` decorator can now be ... | pydocs | pep-0681.rst:151 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclass-transform | 681 | 3.11 | modern | dataclass-transform/modern_02.py | # The ``ModelBase`` class is defined by a library. This could be in
# a type stub or inline.
@typing.dataclass_transform()
class ModelBase: ...
# The ``ModelBase`` class can now be used to create new model
# subclasses, like this:
class CustomerModel(ModelBase):
id: int
name: str
| pydocs | pep-0681.rst:174 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclass-transform | 681 | 3.11 | modern | dataclass-transform/modern_03.py | # The ``ModelMeta`` metaclass and ``ModelBase`` class are defined by
# a library. This could be in a type stub or inline.
@typing.dataclass_transform()
class ModelMeta(type): ...
class ModelBase(metaclass=ModelMeta): ...
# The ``ModelBase`` class can now be used to create new model
# subclasses, like this:
class Cust... | pydocs | pep-0681.rst:190 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclass-transform | 681 | 3.11 | modern | dataclass-transform/modern_04.py | # Indicate that the ``create_model`` function assumes keyword-only
# parameters for the synthesized ``__init__`` method unless it is
# invoked with ``kw_only=False``. It always synthesizes order-related
# methods and provides no way to override this behavior.
@typing.dataclass_transform(kw_only_default=True, order_defa... | pydocs | pep-0681.rst:279 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclass-transform | 681 | 3.11 | modern | dataclass-transform/modern_05.py | # Indicate that classes that derive from this class default to
# synthesizing comparison methods.
@typing.dataclass_transform(eq_default=True, order_default=True)
class ModelBase:
def __init_subclass__(
cls,
*,
init: bool = True,
frozen: bool = False,
eq: bool = True,
... | pydocs | pep-0681.rst:302 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclass-transform | 681 | 3.11 | modern | dataclass-transform/modern_06.py | # Indicate that classes that use this metaclass default to
# synthesizing comparison methods.
@typing.dataclass_transform(eq_default=True, order_default=True)
class ModelMeta(type):
def __new__(
cls,
name,
bases,
namespace,
*,
init: bool = True,
frozen: bool =... | pydocs | pep-0681.rst:333 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclass-transform | 681 | 3.11 | modern | dataclass-transform/modern_07.py | # Library code (within type stub or inline)
# In this library, passing a resolver means that init must be False,
# and the overload with Literal[False] enforces that.
@overload
def model_field(
*,
default: Optional[Any] = ...,
resolver: Callable[[], Any],
init: Literal[False] = False,
... | pydocs | pep-0681.rst:447 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclass-transform | 681 | 3.11 | modern | dataclass-transform/modern_08.py | # ModelBase is not considered either "frozen" or "non-frozen"
# because it is decorated with ``dataclass_transform``
@typing.dataclass_transform()
class ModelBase(): ...
# Vehicle is considered non-frozen because it does not specify
# "frozen=True".
class Vehicle(ModelBase):
name: str
# Car is a frozen class that... | pydocs | pep-0681.rst:525 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclass-transform | 681 | 3.11 | modern | dataclass-transform/modern_09.py | @typing.dataclass_transform()
class ModelMeta(type): ...
# ModelBase is not considered either "frozen" or "non-frozen"
# because it directly specifies ModelMeta as its metaclass.
class ModelBase(metaclass=ModelMeta): ...
# Vehicle is considered non-frozen because it does not specify
# "frozen=True".
class Vehicle(Mod... | pydocs | pep-0681.rst:544 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | buffer-protocol | 688 | 3.12 | modern | buffer-protocol/modern_01.py | def need_buffer(b: Buffer) -> memoryview:
return memoryview(b)
need_buffer(b"xy") # ok
need_buffer("xy") # rejected by static type checkers
| pydocs | pep-0688.rst:184 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | buffer-protocol | 688 | 3.12 | modern | buffer-protocol/modern_02.py | from collections.abc import Buffer
isinstance(b"xy", Buffer)
issubclass(bytes, Buffer)
issubclass(memoryview, Buffer)
isinstance("xy", Buffer)
issubclass(str, Buffer)
| pydocs | pep-0688.rst:195 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | buffer-protocol | 688 | 3.12 | modern | buffer-protocol/modern_03.py | import contextlib
import inspect
class MyBuffer:
def __init__(self, data: bytes):
self.data = bytearray(data)
self.view = None
def __buffer__(self, flags: int) -> memoryview:
if flags != inspect.BufferFlags.FULL_RO:
raise TypeError("Only BufferFlags.FULL_RO supported")
... | pydocs | pep-0688.rst:219 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-kwargs | 692 | 3.12 | modern | typed-kwargs/modern_01.py | def foo(**kwargs: Unpack[Movie]) -> None: ...
| pydocs | pep-0692.rst:116 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-kwargs | 692 | 3.12 | modern | typed-kwargs/modern_02.py | def foo(**kwargs: Unpack[Movie]) -> None:
assert_type(kwargs, Movie) # OK!
| pydocs | pep-0692.rst:130 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-kwargs | 692 | 3.12 | modern | typed-kwargs/modern_03.py | def foo(**kwargs: Unpack[Movie]) -> None: ...
movie: dict[str, object] = {"name": "Life of Brian", "year": 1979}
foo(**movie) # WRONG! Movie is of type dict[str, object]
typed_movie: Movie = {"name": "The Meaning of Life", "year": 1983}
foo(**typed_movie) # OK!
another_movie = {"name": "Life of Brian", "year": 197... | pydocs | pep-0692.rst:146 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-kwargs | 692 | 3.12 | modern | typed-kwargs/modern_04.py | def foo(name, **kwargs: Unpack[Movie]) -> None: ... # WRONG! "name" will
# always bind to the
# first parameter.
def foo(name, /, **kwargs: Unpack[Movie]) -> None: ... # OK! "name" is a
... | pydocs | pep-0692.rst:166 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-kwargs | 692 | 3.12 | modern | typed-kwargs/modern_05.py | class Animal(TypedDict):
name: str
class Dog(Animal):
breed: str
def accept_animal(**kwargs: Unpack[Animal]): ...
def accept_dog(**kwargs: Unpack[Dog]): ...
accept_dog = accept_animal # OK! Expression of type Dog can be
# assigned to a variable of type Animal.
accept_animal = ac... | pydocs | pep-0692.rst:208 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-kwargs | 692 | 3.12 | modern | typed-kwargs/modern_06.py | class Example(TypedDict):
animal: Animal
string: str
number: NotRequired[int]
def src(**kwargs: Unpack[Example]): ...
def dest(*, animal: Dog, string: str, number: int = ...): ...
dest = src # OK!
| pydocs | pep-0692.rst:236 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-kwargs | 692 | 3.12 | modern | typed-kwargs/modern_07.py | def dest(**kwargs: Unpack[Animal]): ...
def src(name: str): ...
dog: Dog = {"name": "Daisy", "breed": "Labrador"}
animal: Animal = dog
dest = src # WRONG!
dest(**animal) # Fails at runtime.
| pydocs | pep-0692.rst:268 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-kwargs | 692 | 3.12 | modern | typed-kwargs/modern_08.py | def src(**kwargs): ...
def dest(**kwargs: Unpack[Movie]): ...
dest = src # OK!
| pydocs | pep-0692.rst:286 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-kwargs | 692 | 3.12 | modern | typed-kwargs/modern_09.py | class Vehicle:
...
class Car(Vehicle):
...
class Motorcycle(Vehicle):
...
class Vehicles(TypedDict):
car: Car
moto: Motorcycle
def dest(**kwargs: Unpack[Vehicles]): ...
def src(**kwargs: Vehicle): ...
dest = src # OK!
| pydocs | pep-0692.rst:299 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-kwargs | 692 | 3.12 | modern | typed-kwargs/modern_10.py | class Animal(TypedDict):
name: str
class Dog(Animal):
breed: str
def takes_name(name: str): ...
dog: Dog = {"name": "Daisy", "breed": "Labrador"}
animal: Animal = dog
def foo(**kwargs: Unpack[Animal]):
print(kwargs["name"].capitalize())
def bar(**kwargs: Unpack[Animal]):
takes_name(**kwargs)
def b... | pydocs | pep-0692.rst:333 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-kwargs | 692 | 3.12 | modern | typed-kwargs/modern_11.py | def bar(**kwargs: Unpack[Animal]):
name = kwargs["name"]
takes_name(name)
| pydocs | pep-0692.rst:382 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-kwargs | 692 | 3.12 | modern | typed-kwargs/modern_12.py | def foo(name: str, year: int): ... # Preferred way.
def foo(**kwargs: Unpack[Movie]): ...
| pydocs | pep-0692.rst:425 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-kwargs | 692 | 3.12 | modern | typed-kwargs/modern_13.py | class Book(TypedDict):
genre: str
pages: int
TypedDictUnion = Movie | Book
def foo(**kwargs: Unpack[TypedDictUnion]) -> None: ... # WRONG! Unsupported use
# of a union of
# TypedDicts to type
... | pydocs | pep-0692.rst:486 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-kwargs | 692 | 3.12 | modern | typed-kwargs/modern_14.py | @overload
def foo(**kwargs: Unpack[Movie]): ...
@overload
def foo(**kwargs: Unpack[Book]): ...
| pydocs | pep-0692.rst:500 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-kwargs | 692 | 3.12 | modern | typed-kwargs/modern_15.py | def foo(**kwargs: Unpack[Movie]): ...
| pydocs | pep-0692.rst:532 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | legacy | type-param-syntax/legacy_01.py | from typing import Generic, TypeVar
_T_co = TypeVar("_T_co", covariant=True, bound=str)
class ClassA(Generic[_T_co]):
def method1(self) -> _T_co:
...
| pydocs | pep-0695.rst:120 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_01.py | class ClassA[T: str]:
def method1(self) -> T:
...
| pydocs | pep-0695.rst:133 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | legacy | type-param-syntax/legacy_02.py | from typing import TypeVar
_T = TypeVar("_T")
def func(a: _T, b: _T) -> _T:
...
| pydocs | pep-0695.rst:142 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_02.py | def func[T](a: T, b: T) -> T:
...
| pydocs | pep-0695.rst:153 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | legacy | type-param-syntax/legacy_03.py | from typing import TypeAlias
_T = TypeVar("_T")
ListOrSet: TypeAlias = list[_T] | set[_T]
| pydocs | pep-0695.rst:161 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_03.py | type ListOrSet[T] = list[T] | set[T]
| pydocs | pep-0695.rst:172 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_04.py | # This generic class is parameterized by a TypeVar T, a
# TypeVarTuple Ts, and a ParamSpec P.
class ChildClass[T, *Ts, **P]: ...
| pydocs | pep-0695.rst:192 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_05.py | class ClassA[T](Generic[T]): ... # Runtime error
| pydocs | pep-0695.rst:204 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_06.py | class ClassA[S, T](Protocol): ... # OK
class ClassB[S, T](Protocol[S, T]): ... # Recommended type checker error
| pydocs | pep-0695.rst:215 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_07.py | class ClassA[T, *T]: ... # Syntax Error
def func1[T, **T](): ... # Syntax Error
| pydocs | pep-0695.rst:227 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_08.py | class ClassA[T: str]: ...
| pydocs | pep-0695.rst:247 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_09.py | class ClassA[T: dict[str, int]]: ... # OK
class ClassB[T: "ForwardReference"]: ... # OK
class ClassC[V]:
class ClassD[T: dict[str, V]]: ... # Type checker error: generic type
class ClassE[T: [str, int]]: ... # Type checker error: illegal expression form
| pydocs | pep-0695.rst:259 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_10.py | class ClassA[AnyStr: (str, bytes)]: ... # OK
class ClassB[T: ("ForwardReference", bytes)]: ... # OK
class ClassC[T: ()]: ... # Type checker error: two or more types required
class ClassD[T: (str, )]: ... # Type checker error: two or more types required
t1 = (bytes, str)
class ClassE[T: t1]: ... # Type checker ... | pydocs | pep-0695.rst:279 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_11.py | class ClassF[T: (3, bytes)]: ... # Type checker error: invalid expression form
| pydocs | pep-0695.rst:297 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_12.py | class ClassG[T: (list[S], str)]: ... # Type checker error: generic type
| pydocs | pep-0695.rst:306 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_13.py | # A non-generic type alias
type IntOrStr = int | str
# A generic type alias
type ListOrSet[T] = list[T] | set[T]
| pydocs | pep-0695.rst:327 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_14.py | # A type alias that includes a forward reference
type AnimalOrVegetable = Animal | "Vegetable"
# A generic self-referential type alias
type RecursiveList[T] = T | list[RecursiveList[T]]
| pydocs | pep-0695.rst:338 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_15.py | T = TypeVar("T")
type MyList = list[T] # Type checker error: traditional type variable usage
| pydocs | pep-0695.rst:364 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_16.py | # The following generates no compiler error, but a type checker
# should generate an error because an upper bound type must be concrete,
# and ``Sequence[S]`` is generic. Future extensions to the type system may
# eliminate this limitation.
class ClassA[S, T: Sequence[S]]: ...
# The following generates no compiler err... | pydocs | pep-0695.rst:413 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_17.py | class ClassA[T](BaseClass[T], param = Foo[T]): ... # OK
print(T) # Runtime error: 'T' is not defined
@dec(Foo[T]) # Runtime error: 'T' is not defined
class ClassA[T]: ...
| pydocs | pep-0695.rst:433 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_18.py | def func1[T](a: T) -> T: ... # OK
print(T) # Runtime error: 'T' is not defined
def func2[T](a = list[T]): ... # Runtime error: 'T' is not defined
@dec(list[T]) # Runtime error: 'T' is not defined
def func3[T](): ...
| pydocs | pep-0695.rst:450 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_19.py | type Alias1[K, V] = Mapping[K, V] | Sequence[K]
| pydocs | pep-0695.rst:464 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_20.py | S = 0
def outer1[S]():
S = 1
T = 1
def outer2[T]():
def inner1():
nonlocal S # OK because it binds variable S from outer1
nonlocal T # Syntax error: nonlocal binding not allowed for type parameter
def inner2():
global S # OK because it binds variabl... | pydocs | pep-0695.rst:472 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_21.py | class Outer:
class Private:
pass
# If the type parameter scope was like a traditional scope,
# the base class 'Private' would not be accessible here.
class Inner[T](Private, Sequence[T]):
pass
# Likewise, 'Inner' would not be available in these type annotations.
def method1[T](... | pydocs | pep-0695.rst:500 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_22.py | T = 0
@decorator(T) # Argument expression `T` evaluates to 0
class ClassA[T](Sequence[T]):
T = 1
# All methods below should result in a type checker error
# "type parameter 'T' already in use" because they are using the
# type parameter 'T', which is already in use by the outer scope
# 'ClassA'.
... | pydocs | pep-0695.rst:523 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_23.py | T = 0
# T refers to the global variable
print(T) # Prints 0
class Outer[T]:
T = 1
# T refers to the local variable scoped to class 'Outer'
print(T) # Prints 1
class Inner1:
T = 2
# T refers to the local type variable within 'Inner1'
print(T) # Prints 2
def inner_... | pydocs | pep-0695.rst:548 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_24.py | class ClassA[T]((x := Sequence[T])): ... # Syntax error: assignment expression not allowed
def func1[T](val: (x := int)): ... # Syntax error: assignment expression not allowed
def func2[T]() -> (x := Sequence[T]): ... # Syntax error: assignment expression not allowed
type Alias1[T] = (x := list[T]) # Syntax erro... | pydocs | pep-0695.rst:597 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_25.py | class ClassA[T1, T2, T3](list[T1]):
def method1(self, a: T2) -> None:
...
def method2(self) -> T3:
...
| pydocs | pep-0695.rst:664 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | legacy | type-param-syntax/legacy_04.py | T1 = TypeVar("T1", infer_variance=True) # Inferred variance
T2 = TypeVar("T2") # Invariant
T3 = TypeVar("T3", covariant=True) # Covariant
# A type checker should infer the variance for T1 but use the
# specified variance for T2 and T3.
class ClassA(Generic[T1, T2, T3]): ...
| pydocs | pep-0695.rst:719 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_26.py | K = TypeVar("K")
class ClassA[V](dict[K, V]): ... # Type checker error
class ClassB[K, V](dict[K, V]): ... # OK
class ClassC[V]:
# The use of K and V for "method1" is OK because it uses the
# "traditional" generic function mechanism where type parameters
# are implicit. In this case V comes from an out... | pydocs | pep-0695.rst:744 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_27.py | X = int
class Foo[T: X, U: X]:
t, u = T, U
print(Foo.t.__bound__) # prints "int"
X = str
print(Foo.u.__bound__) # prints "str"
| pydocs | pep-0695.rst:866 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_28.py | type Alias = int
| pydocs | pep-0695.rst:946 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_29.py | type Alias[T: int] = list[T]
| pydocs | pep-0695.rst:957 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_30.py | def f[T](x: T) -> T:
return x
| pydocs | pep-0695.rst:974 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_31.py | class C[T](Base):
def __init__(self, x: T):
self.x = x
| pydocs | pep-0695.rst:1028 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_32.py | class C:
class Nested: ...
def generic_method[T](self, x: T, y: Nested) -> T: ...
| pydocs | pep-0695.rst:1048 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_01.py | T = TypeVar("T", default=int) # This means that if no type is specified T = int
@dataclass
class Box(Generic[T]):
value: T | None = None
reveal_type(Box()) # type is Box[int]
reveal_type(Box(value="Hello World!")) # type is Box[str]
| pydocs | pep-0696.rst:37 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_02.py | YieldT = TypeVar("YieldT")
SendT = TypeVar("SendT", default=None)
ReturnT = TypeVar("ReturnT", default=None)
class Generator(Generic[YieldT, SendT, ReturnT]): ...
Generator[int] == Generator[int, None] == Generator[int, None, None]
| pydocs | pep-0696.rst:50 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_03.py | class Bot: ...
BotT = TypeVar("BotT", bound=Bot, default=Bot)
class Context(Generic[BotT]):
bot: BotT
class MyBot(Bot): ...
reveal_type(Context().bot) # type is Bot # notice this is not Any which is what it would be currently
reveal_type(Context[MyBot]().bot) # type is MyBot
| pydocs | pep-0696.rst:62 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_04.py | DefaultStrT = TypeVar("DefaultStrT", default=str)
DefaultIntT = TypeVar("DefaultIntT", default=int)
DefaultBoolT = TypeVar("DefaultBoolT", default=bool)
T = TypeVar("T")
T2 = TypeVar("T2")
class NonDefaultFollowsDefault(Generic[DefaultStrT, T]): ... # Invalid: non-default TypeVars cannot follow ones with defaults
c... | pydocs | pep-0696.rst:104 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_05.py | type Alias[DefaultT = int, T] = tuple[DefaultT, T] # SyntaxError: non-default TypeVars cannot follow ones with defaults
def generic_func[DefaultT = int, T](x: DefaultT, y: T) -> None: ... # SyntaxError: non-default TypeVars cannot follow ones with defaults
class GenericClass[DefaultT = int, T]: ... # SyntaxError: ... | pydocs | pep-0696.rst:144 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_06.py | DefaultP = ParamSpec("DefaultP", default=[str, int])
class Foo(Generic[DefaultP]): ...
reveal_type(Foo) # type is type[Foo[DefaultP = [str, int]]]
reveal_type(Foo()) # type is Foo[[str, int]]
reveal_type(Foo[[bool, bool]]()) # type is Foo[[bool, bool]]
| pydocs | pep-0696.rst:159 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_07.py | DefaultTs = TypeVarTuple("DefaultTs", default=Unpack[tuple[str, int]])
class Foo(Generic[*DefaultTs]): ...
reveal_type(Foo) # type is type[Foo[DefaultTs = *tuple[str, int]]]
reveal_type(Foo()) # type is Foo[str, int]
reveal_type(Foo[int, bool]()) # type is Foo[int, bool]
| pydocs | pep-0696.rst:176 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_08.py | StartT = TypeVar("StartT", default=int)
StopT = TypeVar("StopT", default=StartT)
StepT = TypeVar("StepT", default=int | None)
class slice(Generic[StartT, StopT, StepT]): ...
reveal_type(slice) # type is type[slice[StartT = int, StopT = StartT, StepT = int | None]]
reveal_type(slice()) # type i... | pydocs | pep-0696.rst:200 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_09.py | T2 = TypeVar("T2", default=T1)
class Foo(Generic[T1, T2]): ... # Valid
class Foo(Generic[T1]):
class Bar(Generic[T2]): ... # Valid
StartT = TypeVar("StartT", default="StopT") # Swapped defaults around from previous example
StopT = TypeVar("StopT", default=int)
class slice(Generic[StartT, StopT, StepT]): ...
... | pydocs | pep-0696.rst:230 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_10.py | T1 = TypeVar("T1", bound=int)
TypeVar("Ok", default=T1, bound=float) # Valid
TypeVar("AlsoOk", default=T1, bound=int) # Valid
TypeVar("Invalid", default=T1, bound=str) # Invalid: int is not a subtype of str
| pydocs | pep-0696.rst:250 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_11.py | T1 = TypeVar("T1", bound=int)
TypeVar("Invalid", float, str, default=T1) # Invalid: upper bound int is incompatible with constraints float or str
T1 = TypeVar("T1", int, str)
TypeVar("AlsoOk", int, str, bool, default=T1) # Valid
TypeVar("AlsoInvalid", bool, complex, default=T1) # Invalid: {bool, complex}... | pydocs | pep-0696.rst:262 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_12.py | T = TypeVar("T")
ListDefaultT = TypeVar("ListDefaultT", default=list[T])
class Bar(Generic[T, ListDefaultT]):
def __init__(self, x: T, y: ListDefaultT): ...
reveal_type(Bar) # type is type[Bar[T, ListDefaultT = list[T]]]
reveal_type(Bar[int]) # type is type[Bar[int, list[int]]]
re... | pydocs | pep-0696.rst:279 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_13.py | TypeVar("Ok", bound=float, default=int) # Valid
TypeVar("Invalid", bound=str, default=int) # Invalid: the bound and default are incompatible
| pydocs | pep-0696.rst:357 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_14.py | TypeVar("Ok", float, str, default=float) # Valid
TypeVar("Invalid", float, str, default=int) # Invalid: expected one of float or str got int
| pydocs | pep-0696.rst:369 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_15.py | T = TypeVar('T', default=int)
def func(x: int | set[T]) -> T: ...
reveal_type(func(0)) # a type checker may reveal T's default of int here
| pydocs | pep-0696.rst:386 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_16.py | Ts = TypeVarTuple("Ts")
T = TypeVar("T", default=bool)
class Foo(Generic[Ts, T]): ... # Type checker error
# Could be reasonably interpreted as either Ts = (int, str, float), T = bool
# or Ts = (int, str), T = float
Foo[int, str, float]
| pydocs | pep-0696.rst:399 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_17.py | Ts = TypeVarTuple("Ts")
P = ParamSpec("P", default=[float, bool])
class Foo(Generic[Ts, P]): ... # Valid
Foo[int, str] # Ts = (int, str), P = [float, bool]
Foo[int, str, [bytes]] # Ts = (int, str), P = [bytes]
| pydocs | pep-0696.rst:416 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_18.py | class Foo[T = int]:
def meth(self) -> Self:
return self
reveal_type(Foo.meth) # type is (self: Foo[int]) -> Foo[int]
| pydocs | pep-0696.rst:453 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_19.py | YieldT = TypeVar("YieldT", default=Any)
SendT = TypeVar("SendT", default=Any)
ReturnT = TypeVar("ReturnT")
class Coroutine(Generic[YieldT, SendT, ReturnT]): ...
Coroutine[int] == Coroutine[Any, Any, int]
| pydocs | pep-0696.rst:570 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-param-defaults | 696 | 3.13 | modern | type-param-defaults/modern_20.py | T = TypeVar("T", bound=int) # default is implicitly int
U = TypeVar("U")
class Foo(Generic[T, U]):
...
# would expand to
T = TypeVar("T", bound=int, default=int)
U = TypeVar("U")
class Foo(Generic[T, U]):
...
| pydocs | pep-0696.rst:601 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | override-decorator | 698 | 3.12 | modern | override-decorator/modern_01.py | from typing import override
class Parent:
def foo(self) -> int:
return 1
def bar(self, x: str) -> str:
return x
class Child(Parent):
@override
def foo(self) -> int:
return 2
@override
def baz(self) -> int: # Type check error: no matching signature in ancestor
... | pydocs | pep-0698.rst:186 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | fstring-grammar | 701 | 3.12 | modern | fstring-grammar/modern_01.py | f'Magic wand: { bag['wand'] }'
| pydocs | pep-0701.rst:44 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | fstring-grammar | 701 | 3.12 | modern | fstring-grammar/modern_02.py | f'''A complex trick: {
bag['bag'] # recursive bags!
}'''
| pydocs | pep-0701.rst:56 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | fstring-grammar | 701 | 3.12 | modern | fstring-grammar/modern_03.py | f"These are the things: {", ".join(things)}"
f"{source.removesuffix(".py")}.c: $(srcdir)/{source}"
f"{f"{f"infinite"}"}" + " " + f"{f"nesting!!!"}"
| pydocs | pep-0701.rst:144 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | fstring-grammar | 701 | 3.12 | modern | fstring-grammar/modern_04.py | a = ["hello", "world"]
f"{'\n'.join(a)}"
| pydocs | pep-0701.rst:157 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | fstring-grammar | 701 | 3.12 | modern | fstring-grammar/modern_05.py | f"{f"{f"{f"{f"{f"{1+1}"}"}"}"}"}"
| pydocs | pep-0701.rst:192 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | fstring-grammar | 701 | 3.12 | modern | fstring-grammar/modern_06.py | x = 1
f"___{
x
}___"
f"___{(
x
)}___"
| pydocs | pep-0701.rst:422 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | fstring-grammar | 701 | 3.12 | modern | fstring-grammar/modern_07.py | def py2c(source):
return f"{source.removesuffix(".py")}.c"
| pydocs | pep-0701.rst:491 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | deprecated-decorator | 702 | 3.13 | modern | deprecated-decorator/modern_01.py | from warnings import deprecated
@deprecated("Use Spam instead")
class Ham: ...
@deprecated("It is pining for the fiords")
def norwegian_blue(x: int) -> int: ...
@overload
@deprecated("Only str will be allowed")
def foo(x: int) -> str: ...
@overload
def foo(x: str) -> str: ...
class Spam:
@deprecated("There is e... | pydocs | pep-0702.rst:162 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | readonly-items | 705 | 3.13 | modern | readonly-items/modern_01.py | from typing import NotRequired, ReadOnly, TypedDict
class Movie(TypedDict):
name: ReadOnly[str]
year: ReadOnly[NotRequired[int | None]]
def movie_string(movie: Movie) -> str:
if movie.get("year") is None:
return movie["name"]
else:
return f'{movie["name"]} ({movie["year"]})'
| pydocs | pep-0705.rst:135 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.