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 | self-type | 673 | 3.11 | modern | self-type/modern_11.py | from typing import Protocol
class ShapeProtocol(Protocol):
def set_scale(self, scale: float) -> Self: ...
class ReturnSelf:
scale: float = 1.0
def set_scale(self, scale: float) -> Self:
self.scale = scale
return self
class ReturnConcreteShape:
scale: float = 1.0
def set_scale(se... | pydocs | pep-0673.rst:519 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | modern | self-type/modern_12.py | class ReturnsSelf:
def foo(self) -> Self: ... # Accepted
@classmethod
def bar(cls) -> Self: # Accepted
return cls()
def __new__(cls, value: int) -> Self: ... # Accepted
def explicitly_use_self(self: Self) -> Self: ... # Accepted
# Accepted (Self can be nested within other types)
... | pydocs | pep-0673.rst:579 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | modern | self-type/modern_13.py | def foo(bar: Self) -> Self: ... # Rejected (not within a class)
bar: Self # Rejected (not within a class)
class Foo:
# Rejected (Self is treated as unknown).
def has_existing_self_annotation(self: T) -> Self: ...
class Foo:
def return_concrete_type(self) -> Self:
return Foo() # Rejected (see F... | pydocs | pep-0673.rst:638 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | modern | self-type/modern_14.py | TupleSelf = Tuple[Self, Self] # Rejected
class Alias:
def return_tuple(self) -> TupleSelf: # Rejected
return (self, self)
| pydocs | pep-0673.rst:673 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | modern | self-type/modern_15.py | class Base:
@staticmethod
def make() -> Self: # Rejected
...
@staticmethod
def return_parameter(foo: Self) -> Self: # Rejected
...
| pydocs | pep-0673.rst:686 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | self-type | 673 | 3.11 | modern | self-type/modern_16.py | class MyMetaclass(type):
def __new__(cls, *args: Any) -> Self: # Rejected
return super().__new__(cls, *args)
def __mul__(cls, count: int) -> list[Self]: # Rejected
return [cls()] * count
class Foo(metaclass=MyMetaclass): ...
| pydocs | pep-0673.rst:705 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_01.py | from typing import LiteralString
def execute(self, sql: LiteralString, parameters: Iterable[str] = ...) -> Cursor: ...
| pydocs | pep-0675.rst:131 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_02.py | variable_annotation: LiteralString
def my_function(literal_string: LiteralString) -> LiteralString: ...
class Foo:
my_attribute: LiteralString
type_argument: List[LiteralString]
T = TypeVar("T", bound=LiteralString)
| pydocs | pep-0675.rst:296 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_03.py | bad_union: Literal["hello", LiteralString] # Not OK
bad_nesting: Literal[LiteralString] # Not OK
| pydocs | pep-0675.rst:311 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_04.py | literal_string: LiteralString
s: str = literal_string # OK
literal_string: LiteralString = s # Error: Expected LiteralString, got str.
literal_string: LiteralString = "hello" # OK
| pydocs | pep-0675.rst:382 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_05.py | def expect_literal_string(s: LiteralString) -> None: ...
expect_literal_string("foo" + "bar") # OK
expect_literal_string(literal_string + "bar") # OK
literal_string2: LiteralString
expect_literal_string(literal_string + literal_string2) # OK
plain_string: str
expect_literal_string(literal_string + plain_string) ... | pydocs | pep-0675.rst:392 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_06.py | expect_literal_string(",".join(["foo", "bar"])) # OK
expect_literal_string(literal_string.join(["foo", "bar"])) # OK
expect_literal_string(literal_string.join([literal_string, literal_string2])) # OK
xs: List[LiteralString]
expect_literal_string(literal_string.join(xs)) # OK
expect_literal_string(plain_string.join(... | pydocs | pep-0675.rst:407 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_07.py | literal_name: LiteralString
expect_literal_string(f"hello {literal_name}")
# OK because it is composed from literal strings.
expect_literal_string("hello {}".format(literal_name)) # OK
expect_literal_string(f"hello") # OK
username: str
expect_literal_string(f"hello {username}")
# NOT OK. The format-string is const... | pydocs | pep-0675.rst:428 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_08.py | def add_limit(query: LiteralString) -> LiteralString:
return query + " LIMIT = 1"
def my_query(query: LiteralString, user_id: str) -> None:
sql_connection().execute(add_limit(query), (user_id,)) # OK
| pydocs | pep-0675.rst:458 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_09.py | def return_literal_string() -> LiteralString:
return "foo" if condition1() else "bar" # OK
def return_literal_str2(literal_string: LiteralString) -> LiteralString:
return "foo" if condition1() else literal_string # OK
def return_literal_str3() -> LiteralString:
if condition1():
result: Literal["... | pydocs | pep-0675.rst:468 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_10.py | from typing import Literal, LiteralString, TypeVar
TLiteral = TypeVar("TLiteral", bound=LiteralString)
def literal_identity(s: TLiteral) -> TLiteral:
return s
hello: Literal["hello"] = "hello"
y = literal_identity(hello)
reveal_type(y) # => Literal["hello"]
s: LiteralString
y2 = literal_identity(s)
reveal_type... | pydocs | pep-0675.rst:490 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_11.py | class Container(Generic[T]):
def __init__(self, value: T) -> None:
self.value = value
literal_string: LiteralString = "hello"
x: Container[LiteralString] = Container(literal_string) # OK
s: str
x_error: Container[LiteralString] = Container(s) # Not OK
| pydocs | pep-0675.rst:514 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_12.py | xs: List[LiteralString] = ["foo", "bar", "baz"]
| pydocs | pep-0675.rst:528 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_13.py | @overload
def foo(x: Literal["foo"]) -> int: ...
@overload
def foo(x: LiteralString) -> bool: ...
@overload
def foo(x: str) -> str: ...
x1: int = foo("foo") # First overload.
x2: bool = foo("bar") # Second overload.
s: str
x3: str = foo(s) # Third overload.
| pydocs | pep-0675.rst:541 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_14.py | x: LiteralString = "hello"
expect_literal_string(x)
| pydocs | pep-0675.rst:584 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_15.py | def run(command: LiteralString, *args: str, shell: bool=...): ...
| pydocs | pep-0675.rst:868 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_16.py | def mark_safe(s: LiteralString) -> str: ...
| pydocs | pep-0675.rst:899 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_17.py | class Template:
def __init__(self, source: LiteralString): ...
| pydocs | pep-0675.rst:933 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | literal-string | 675 | 3.11 | modern | literal-string/modern_18.py | def info(msg: LiteralString, *args: object) -> None:
...
| pydocs | pep-0675.rst:969 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | typed-kwargs | 692 | 3.12 | modern | typed-kwargs/modern_01.py | def foo(**kwargs: Unpack[Movie]) -> None: ...
| pydocs | pep-0692.rst:116 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | typed-kwargs | 692 | 3.12 | modern | typed-kwargs/modern_15.py | def foo(**kwargs: Unpack[Movie]): ...
| pydocs | pep-0692.rst:532 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_08.py | class ClassA[T: str]: ...
| pydocs | pep-0695.rst:247 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_28.py | type Alias = int
| pydocs | pep-0695.rst:946 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | type-param-syntax | 695 | 3.12 | modern | type-param-syntax/modern_29.py | type Alias[T: int] = list[T]
| pydocs | pep-0695.rst:957 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
train | 3.14 | 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 | cec9cb6ae57ddb740dc344666866839f5e8eec8c | CC0-1.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.