cut stringclasses 1
value | version stringclasses 10
values | idiom_id stringclasses 53
values | pep stringclasses 53
values | introduced_in stringclasses 11
values | form stringclasses 2
values | file stringclasses 887
values | code stringclasses 887
values | source stringclasses 1
value | origin stringclasses 887
values | origin_revision stringclasses 1
value | license stringclasses 1
value |
|---|---|---|---|---|---|---|---|---|---|---|---|
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | literal-string | 675 | 3.11 | modern | literal-string/modern_12.py | xs: List[LiteralString] = ["foo", "bar", "baz"]
| pydocs | pep-0675.rst:528 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | literal-string | 675 | 3.11 | modern | literal-string/modern_14.py | x: LiteralString = "hello"
expect_literal_string(x)
| pydocs | pep-0675.rst:584 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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.12 | 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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.