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-types | 586 | 3.8 | modern | literal-types/modern_02.py | # Note: this is a simplification of the true type signature.
_PathType = Union[str, bytes, int]
@overload
def open(path: _PathType,
mode: Literal["r", "w", "a", "x", "r+", "w+", "a+", "x+"],
) -> IO[Text]: ...
@overload
def open(path: _PathType,
mode: Literal["rb", "wb", "ab", "xb", "r+b", "... | pydocs | pep-0586.rst:127 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-types | 586 | 3.8 | modern | literal-types/modern_03.py | Literal[26]
Literal[0x1A] # Exactly equivalent to Literal[26]
Literal[-4]
Literal["hello world"]
Literal[b"hello world"]
Literal[u"hello world"]
Literal[True]
Literal[Color.RED] # Assuming Color is some enum
Literal[None]
| pydocs | pep-0586.rst:167 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-types | 586 | 3.8 | modern | literal-types/modern_04.py | ReadOnlyMode = Literal["r", "r+"]
WriteAndTruncateMode = Literal["w", "w+", "wt", "w+t"]
WriteNoTruncateMode = Literal["r+", "r+t"]
AppendMode = Literal["a", "a+", "at", "a+t"]
AllModes = Literal[ReadOnlyMode, WriteAndTruncateMode,
WriteNoTruncateMode, AppendMode]
| pydocs | pep-0586.rst:184 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-types | 586 | 3.8 | modern | literal-types/modern_05.py | Literal[Literal[Literal[1, 2, 3], "foo"], 5, None]
| pydocs | pep-0586.rst:198 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-types | 586 | 3.8 | modern | literal-types/modern_06.py | Literal[1, 2, 3, "foo", 5, None]
| pydocs | pep-0586.rst:202 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-types | 586 | 3.8 | modern | literal-types/modern_07.py | Optional[Literal[1, 2, 3, "foo", 5]]
| pydocs | pep-0586.rst:206 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-types | 586 | 3.8 | modern | literal-types/modern_08.py | def my_function(x: Literal[1 + 2]) -> int:
return x * 3
x: Literal = 3
y: Literal[my_function] = my_function
| pydocs | pep-0586.rst:273 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-types | 586 | 3.8 | modern | literal-types/modern_09.py | def expects_str(x: str) -> None: ...
var: Literal["foo"] = "foo"
# Legal: Literal["foo"] is a subtype of str
expects_str(var)
| pydocs | pep-0586.rst:362 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-types | 586 | 3.8 | modern | literal-types/modern_10.py | def expects_literal(x: Literal["foo"]) -> None: ...
def runner(my_str: str) -> None:
# ILLEGAL: str is not a subclass of Literal["foo"]
expects_literal(my_str)
| pydocs | pep-0586.rst:371 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-types | 586 | 3.8 | modern | literal-types/modern_11.py | a: Literal[0] = 0
b: Literal[5] = 5
some_tuple: Tuple[int, str, List[bool]] = (3, "abc", [True, False])
reveal_type(some_tuple[a]) # Revealed type is 'int'
some_tuple[b] # Error: 5 is not a valid index into the tuple
| pydocs | pep-0586.rst:395 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-types | 586 | 3.8 | modern | literal-types/modern_12.py | class Test:
def __init__(self, param: int) -> None:
self.myfield = param
def mymethod(self, val: int) -> str: ...
a: Literal["myfield"] = "myfield"
b: Literal["mymethod"] = "mymethod"
c: Literal["blah"] = "blah"
t = Test()
reveal_type(getattr(t, a)) # Revealed type is 'int'
reveal_type(getattr(... | pydocs | pep-0586.rst:404 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-types | 586 | 3.8 | modern | literal-types/modern_13.py | _PathType = Union[str, bytes, int]
@overload
def open(path: _PathType,
mode: Literal["r", "w", "a", "x", "r+", "w+", "a+", "x+"],
) -> IO[Text]: ...
@overload
def open(path: _PathType,
mode: Literal["rb", "wb", "ab", "xb", "r+b", "w+b", "a+b", "x+b"],
) -> IO[bytes]: ...
# Fallback... | pydocs | pep-0586.rst:432 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-types | 586 | 3.8 | modern | literal-types/modern_14.py | A = TypeVar('A', bound=int)
B = TypeVar('B', bound=int)
C = TypeVar('C', bound=int)
# A simplified definition for Matrix[row, column]
class Matrix(Generic[A, B]):
def __add__(self, other: Matrix[A, B]) -> Matrix[A, B]: ...
def __matmul__(self, other: Matrix[B, C]) -> Matrix[A, C]: ...
def transpose(self) -... | pydocs | pep-0586.rst:469 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-types | 586 | 3.8 | modern | literal-types/modern_15.py | T = TypeVar('T', Literal["a"], Literal["b"], Literal["c"])
S = TypeVar('S', bound=Literal["foo"])
| pydocs | pep-0586.rst:488 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-types | 586 | 3.8 | modern | literal-types/modern_16.py | @overload
def is_int_like(x: Union[int, List[int]]) -> Literal[True]: ...
@overload
def is_int_like(x: object) -> bool: ...
def is_int_like(x): ...
vector: List[int] = [1, 2, 3]
if is_int_like(vector):
vector.append(3)
else:
vector.append("bad") # This branch is inferred to be unreachable
scalar: Union[int,... | pydocs | pep-0586.rst:570 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-types | 586 | 3.8 | modern | literal-types/modern_17.py | def expects_three(x: Literal[3]) -> None: ...
expects_three(foo) # Type checks, since 'foo' is Final and equal to 3
| pydocs | pep-0586.rst:602 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | literal-types | 586 | 3.8 | modern | literal-types/modern_18.py | def foobar(arg1: Literal[1], arg2: Literal[True]) -> None:
pass
| pydocs | pep-0586.rst:668 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_01.py | from typing import TypedDict
class Movie(TypedDict):
name: str
year: int
| pydocs | pep-0589.rst:39 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_02.py | class EmptyDict(TypedDict):
pass
| pydocs | pep-0589.rst:161 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_03.py | class Strings(TypedDict):
items: List[str]
print(isinstance({'items': [1]}, Strings)) # Should be False
print(isinstance({'items': ['x']}, Strings)) # Should be True
| pydocs | pep-0589.rst:233 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_04.py | class BookBasedMovie(TypedDict):
name: str
year: int
based_on: str
| pydocs | pep-0589.rst:257 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_05.py | class X(TypedDict):
x: int
class Y(TypedDict):
y: str
class XYZ(X, Y):
z: bool
| pydocs | pep-0589.rst:264 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_06.py | class X(TypedDict):
x: str
class Y(X):
x: int # Type check error: cannot overwrite TypedDict field "x"
| pydocs | pep-0589.rst:284 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_07.py | class X(TypedDict):
x: int
class Y(TypedDict):
x: str
class XYZ(X, Y): # Type check error: cannot overwrite TypedDict field "x" while merging
xyz: bool
| pydocs | pep-0589.rst:298 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_08.py | class Movie(TypedDict, total=False):
name: str
year: int
| pydocs | pep-0589.rst:315 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_09.py | Movie = TypedDict('Movie', {'name': str, 'year': int})
| pydocs | pep-0589.rst:344 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_10.py | Movie = TypedDict('Movie',
{'name': str, 'year': int},
total=False)
| pydocs | pep-0589.rst:348 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_11.py | class A(TypedDict):
x: Optional[int]
class B(TypedDict):
x: int
def f(a: A) -> None:
a['x'] = None
b: B = {'x': 0}
f(b) # Type check error: 'B' not compatible with 'A'
b['x'] + 1 # Runtime error: None + 1
| pydocs | pep-0589.rst:393 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_12.py | class A(TypedDict, total=False):
x: int
class B(TypedDict):
x: int
def f(a: A) -> None:
del a['x']
b: B = {'x': 0}
f(b) # Type check error: 'B' not compatible with 'A'
b['x'] + 1 # Runtime KeyError: 'x'
| pydocs | pep-0589.rst:410 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_13.py | class A(TypedDict, total=False):
x: int
y: int
class B(TypedDict, total=False):
x: int
class C(TypedDict, total=False):
x: int
y: str
def f(a: A) -> None:
a['y'] = 1
def g(b: B) -> None:
f(b) # Type check error: 'B' incompatible with 'A'
c: C = {'x': 0, 'y': 'foo'}
g(c)
c['y'] + 'bar' ... | pydocs | pep-0589.rst:429 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_14.py | class A(TypedDict):
x: int
class B(A):
y: str
def f(d: Dict[str, int]) -> None:
d['y'] = 0
def g(a: A) -> None:
f(a) # Type check error: 'A' incompatible with Dict[str, int]
b: B = {'x': 0, 'y': 'foo'}
g(b)
b['y'] + 'bar' # Runtime error: int + str
| pydocs | pep-0589.rst:455 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | typed-dict | 589 | 3.8 | modern | typed-dict/modern_15.py | class A(TypedDict):
x: int
class B(TypedDict):
x: int
y: str
def sum_values(m: Mapping[str, int]) -> int:
n = 0
for v in m.values():
n += v # Runtime error
return n
def f(a: A) -> None:
sum_values(a) # Error: 'A' incompatible with Mapping[str, int]
b: B = {'x': 0, 'y': 'foo'}
f... | pydocs | pep-0589.rst:477 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_01.py | from typing import final
@final
class Base:
...
class Derived(Base): # Error: Cannot inherit from final class "Base"
...
| pydocs | pep-0591.rst:85 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_02.py | from typing import final
class Base:
@final
def foo(self) -> None:
...
class Derived(Base):
def foo(self) -> None: # Error: Cannot override final attribute "foo"
# (previously declared in base class "Base")
...
| pydocs | pep-0591.rst:96 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_03.py | from typing import Any, overload
class Base:
@overload
def method(self) -> None: ...
@overload
def method(self, arg: int) -> int: ...
@final
def method(self, x=None):
...
| pydocs | pep-0591.rst:112 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_04.py | ID: Final[float] = 1
| pydocs | pep-0591.rst:138 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_05.py | ID: Final = 1
| pydocs | pep-0591.rst:142 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_06.py | from typing import Final
RATE: Final = 3000
class Base:
DEFAULT_ID: Final = 0
RATE = 300 # Error: can't assign to final attribute
Base.DEFAULT_ID = 1 # Error: can't override a final attribute
| pydocs | pep-0591.rst:172 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_07.py | from typing import Final
class Window:
BORDER_WIDTH: Final = 2.5
...
class ListView(Window):
BORDER_WIDTH = 3 # Error: can't override a final attribute
| pydocs | pep-0591.rst:189 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_08.py | class ImmutablePoint:
x: Final[int]
y: Final[int] # Error: final attribute without an initializer
def __init__(self) -> None:
self.x = 1 # Good
| pydocs | pep-0591.rst:201 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_09.py | x: List[Final[int]] = [] # Error!
def fun(x: Final[List[int]]) -> None: # Error!
...
| pydocs | pep-0591.rst:216 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_10.py | x: Final = ['a', 'b']
x.append('c') # OK
y: Final[Sequence[str]] = ['a', 'b']
y.append('x') # Error: "Sequence[str]" has no attribute "append"
z: Final = ('a', 'b') # Also works
| pydocs | pep-0591.rst:226 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | final-qualifier | 591 | 3.8 | modern | final-qualifier/modern_11.py | from typing import NamedTuple, Final
X: Final = "x"
Y: Final = "y"
N = NamedTuple("N", [(X, int), (Y, int)])
| pydocs | pep-0591.rst:238 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | annotated | 593 | 3.9 | modern | annotated/modern_01.py | UnsignedShort = Annotated[int, struct2.ctype('H')]
SignedChar = Annotated[int, struct2.ctype('b')]
class Student(struct2.Packed):
# mypy typechecks 'name' field as 'str'
name: Annotated[str, struct2.ctype("<10s")]
serialnum: UnsignedShort
school: SignedChar
# 'unpack' only uses the metadata within the... | pydocs | pep-0593.rst:71 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | annotated | 593 | 3.9 | modern | annotated/modern_02.py | Currency = Annotated[
TypedDict('Currency', {'dollars': float, 'pounds': float}, total=False),
TaggedUnion,
]
| pydocs | pep-0593.rst:104 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | annotated | 593 | 3.9 | modern | annotated/modern_03.py | Annotated[int, ValueRange(3, 10), ctype("char")]
| pydocs | pep-0593.rst:132 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | annotated | 593 | 3.9 | modern | annotated/modern_04.py | Annotated[int, ValueRange(3, 10), ctype("char")] != Annotated[
int, ctype("char"), ValueRange(3, 10)
]
| pydocs | pep-0593.rst:140 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | annotated | 593 | 3.9 | modern | annotated/modern_05.py | Annotated[Annotated[int, ValueRange(3, 10)], ctype("char")] == Annotated[
int, ValueRange(3, 10), ctype("char")
]
| pydocs | pep-0593.rst:147 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | annotated | 593 | 3.9 | modern | annotated/modern_06.py | Annotated[int, ValueRange(3, 10)] != Annotated[
int, ValueRange(3, 10), ValueRange(3, 10)
]
| pydocs | pep-0593.rst:153 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | annotated | 593 | 3.9 | modern | annotated/modern_07.py | T1 = Annotated[int, ValueRange(-10, 5)]
T2 = Annotated[T1, ValueRange(-20, 3)]
| pydocs | pep-0593.rst:194 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | annotated | 593 | 3.9 | modern | annotated/modern_08.py | T2 = Annotated[int, ValueRange(-10, 5), ValueRange(-20, 3)]
| pydocs | pep-0593.rst:199 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | annotated | 593 | 3.9 | modern | annotated/modern_09.py | @struct2.packed
class Student(NamedTuple):
name: Annotated[str, struct.ctype("<10s")]
get_type_hints(Student) == {'name': str}
get_type_hints(Student, include_extras=False) == {'name': str}
get_type_hints(Student, include_extras=True) == {
'name': Annotated[str, struct.ctype("<10s")]
}
| pydocs | pep-0593.rst:210 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | annotated | 593 | 3.9 | modern | annotated/modern_10.py | T = TypeVar('T')
Const = Annotated[T, my_annotations.CONST]
class C:
def const_method(self: Const[List[int]]) -> int:
...
| pydocs | pep-0593.rst:227 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_01.py | isinstance(5, int | str)
issubclass(bool, int | float)
| pydocs | pep-0604.rst:52 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_02.py | isinstance(None, int | None)
isinstance(42, None | int)
| pydocs | pep-0604.rst:58 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_03.py | # Instead of
# def f(list: List[Union[int, str]], param: Optional[int]) -> Union[float, str]
def f(list: List[int | str], param: int | None) -> float | str:
pass
f([1, "abc"], None)
# Instead of typing.List[typing.Union[str, int]]
typing.List[str | int]
list[str | int]
# Instead of typing.Dict[str, typing.Union[... | pydocs | pep-0604.rst:70 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_04.py | int | str == typing.Union[int, str]
typing.Union[int, int] == int
int | int == int
| pydocs | pep-0604.rst:89 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_05.py | (int | str) == (str | int)
(int | str | float) == typing.Union[str, float, int]
| pydocs | pep-0604.rst:98 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_06.py | None | t == typing.Optional[t]
| pydocs | pep-0604.rst:106 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_07.py | str(int | list[str])
# int | list[str]
str(int | int)
# int
| pydocs | pep-0604.rst:112 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_08.py | # valid
isinstance("", int | str)
# invalid
isinstance(2, list[int]) # TypeError: isinstance() argument 2 cannot be a parameterized generic
isinstance(1, int | list[int])
# valid
issubclass(bool, int | float)
# invalid
issubclass(bool, bool | list[int])
| pydocs | pep-0604.rst:125 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_09.py | class M(type):
def __or__(self, other): return "Hello"
class C(metaclass=M): pass
C | int
int | C
Union[C, int]
| pydocs | pep-0604.rst:147 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | union-syntax | 604 | 3.10 | modern | union-syntax/modern_10.py | from __future__ import annotations
def foo() -> int | str: pass
eval(foo.__annotations__['return'])
| pydocs | pep-0604.rst:194 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | legacy | param-spec/legacy_01.py | from typing import Awaitable, Callable, TypeVar
R = TypeVar("R")
def add_logging(f: Callable[..., R]) -> Callable[..., Awaitable[R]]:
async def inner(*args: object, **kwargs: object) -> R:
await log_to_database()
return f(*args, **kwargs)
return inner
@add_logging
def takes_int_str(x: int, y: str) -> int... | pydocs | pep-0612.rst:37 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | modern | param-spec/modern_01.py | from typing import Awaitable, Callable, ParamSpec, TypeVar
P = ParamSpec("P")
R = TypeVar("R")
def add_logging(f: Callable[P, R]) -> Callable[P, Awaitable[R]]:
async def inner(*args: P.args, **kwargs: P.kwargs) -> R:
await log_to_database()
return f(*args, **kwargs)
return inner
@add_logging
def takes_in... | pydocs | pep-0612.rst:87 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | legacy | param-spec/legacy_02.py | class Request:
...
def with_request(f: Callable[..., R]) -> Callable[..., R]:
def inner(*args: object, **kwargs: object) -> R:
return f(Request(), *args, **kwargs)
return inner
@with_request
def takes_int_str(request: Request, x: int, y: str) -> int:
# use request
return x + 7
takes_int_str(1, "A")
tak... | pydocs | pep-0612.rst:111 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | modern | param-spec/modern_02.py | from typing import Concatenate
def with_request(f: Callable[Concatenate[Request, P], R]) -> Callable[P, R]:
def inner(*args: P.args, **kwargs: P.kwargs) -> R:
return f(Request(), *args, **kwargs)
return inner
@with_request
def takes_int_str(request: Request, x: int, y: str) -> int:
# use request
return x ... | pydocs | pep-0612.rst:133 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | modern | param-spec/modern_03.py | from typing import ParamSpec
P = ParamSpec("P") # Accepted
P = ParamSpec("WrongName") # Rejected because P =/= WrongName
| pydocs | pep-0612.rst:163 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | modern | param-spec/modern_04.py | def foo(x: P) -> P: ... # Rejected
def foo(x: Concatenate[int, P]) -> int: ... # Rejected
def foo(x: typing.List[P]) -> None: ... # Rejected
def foo(x: Callable[[int, str], P]) -> None: ... # Rejected
| pydocs | pep-0612.rst:202 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | modern | param-spec/modern_05.py | T = TypeVar("T")
P_2 = ParamSpec("P_2")
class X(Generic[T, P]):
f: Callable[P, int]
x: T
def f(x: X[int, P_2]) -> str: ... # Accepted
def f(x: X[int, Concatenate[int, P_2]]) -> str: ... # Accepted
def f(x: X[int, [int, bool]]) -> str: ... # Accepted
def f(x: X[int, ...]) -> str: ...... | pydocs | pep-0612.rst:218 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | modern | param-spec/modern_06.py | P = ParamSpec("P")
def foo(x: Callable[P, int], y: Callable[P, int]) -> Callable[P, bool]: ...
def x_y(x: int, y: str) -> int: ...
def y_x(y: int, x: str) -> int: ...
foo(x_y, x_y) # Should return (x: int, y: str) -> bool
foo(x_y, y_x) # Could return (__a: int, __b: str) -> bool
# This works becaus... | pydocs | pep-0612.rst:279 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | modern | param-spec/modern_07.py | def bar(x: int, *args: bool) -> int: ...
def add(x: Callable[P, int]) -> Callable[Concatenate[str, P], bool]: ...
add(bar) # Should return (__a: str, x: int, *args: bool) -> bool
def remove(x: Callable[Concatenate[int, P], int]) -> Callable[P, bool]: ...
remove(bar) # Should return (*args: bool) -> bool
d... | pydocs | pep-0612.rst:324 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | modern | param-spec/modern_08.py | def expects_int_first(x: Callable[Concatenate[int, P], int]) -> None: ...
@expects_int_first # Rejected
def one(x: str) -> int: ...
@expects_int_first # Rejected
def two(*, x: int) -> int: ...
@expects_int_first # Rejected
def three(**kwargs: int) -> int: ...
@expects_int_first # Accepted
def four(*args: int) -> in... | pydocs | pep-0612.rst:347 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | modern | param-spec/modern_09.py | def puts_p_into_scope(f: Callable[P, int]) -> None:
def inner(*args: P.args, **kwargs: P.kwargs) -> None: # Accepted
pass
def mixed_up(*args: P.kwargs, **kwargs: P.args) -> None: # Rejected
pass
def misplaced(x: P.args) -> None: # Rejected
pass
def out_of_scope(*arg... | pydocs | pep-0612.rst:390 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | modern | param-spec/modern_10.py | def puts_p_into_scope(f: Callable[P, int]) -> None:
stored_args: P.args # Rejected
stored_kwargs: P.kwargs # Rejected
def just_args(*args: P.args) -> None: # Rejected
pass
def just_kwargs(**kwargs: P.kwargs) -> None: # Rejected
pass
| pydocs | pep-0612.rst:414 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | modern | param-spec/modern_11.py | def decorator(f: Callable[P, int]) -> Callable[P, None]:
def foo(*args: P.args, **kwargs: P.kwargs) -> None:
f(*args, **kwargs) # Accepted, should resolve to int
f(*kwargs, **args) # Rejected
f(1, *args, **kwargs) # Rejected
return foo # Accepted
| pydocs | pep-0612.rst:454 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | modern | param-spec/modern_12.py | def add(f: Callable[P, int]) -> Callable[Concatenate[str, P], None]:
def foo(s: str, *args: P.args, **kwargs: P.kwargs) -> None: # Accepted
pass
def bar(*args: P.args, s: str, **kwargs: P.kwargs) -> None: # Rejected
pass
return foo # Accepted
def re... | pydocs | pep-0612.rst:479 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | modern | param-spec/modern_13.py | def outer(f: Callable[P, None]) -> Callable[P, None]:
def foo(x: int, *args: P.args, **kwargs: P.kwargs) -> None:
f(*args, **kwargs)
def bar(*args: P.args, **kwargs: P.kwargs) -> None:
foo(1, *args, **kwargs) # Accepted
foo(x=1, *args, **kwargs) # Rejected
return bar
| pydocs | pep-0612.rst:507 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | modern | param-spec/modern_14.py | def twice(f: Callable[P, int], *args: P.args, **kwargs: P.kwargs) -> int:
return f(*args, **kwargs) + f(*args, **kwargs)
| pydocs | pep-0612.rst:545 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | modern | param-spec/modern_15.py | def add_n(f: Callable[P, R]) -> Callable[Concatenate[("n", int), P], R]:
def inner(*args: P.args, n: int, **kwargs: P.kwargs) -> R:
# use n
return f(*args, **kwargs)
return inner
| pydocs | pep-0612.rst:714 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | param-spec | 612 | 3.10 | modern | param-spec/modern_16.py | def innocent_wrapper(f: Callable[P, R]) -> Callable[P, R]:
def inner(*args: P.args, **kwargs: P.kwargs) -> R:
added = add_n(f)
return added(*args, n=1, **kwargs)
return inner
@innocent_wrapper
def problem(n: int) -> None:
pass
| pydocs | pep-0612.rst:728 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-alias | 613 | 3.10 | legacy | type-alias/legacy_01.py | MyType = "ClassName"
def foo() -> MyType: ...
| pydocs | pep-0613.rst:53 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-alias | 613 | 3.10 | modern | type-alias/modern_01.py | MyType: TypeAlias = "ClassName"
def foo() -> MyType: ...
| pydocs | pep-0613.rst:64 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-alias | 613 | 3.10 | legacy | type-alias/legacy_02.py | MyType1 = InvalidType
MyType2 = MyGeneric(int) # i.e., intention was MyGeneric[int]
| pydocs | pep-0613.rst:77 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-alias | 613 | 3.10 | modern | type-alias/modern_02.py | MyType1: TypeAlias = InvalidType
MyType2: TypeAlias = MyGeneric(int)
| pydocs | pep-0613.rst:89 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-alias | 613 | 3.10 | modern | type-alias/modern_03.py | class Foo:
x = ClassName
y: TypeAlias = ClassName
z: Type[ClassName] = ClassName
| pydocs | pep-0613.rst:103 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-alias | 613 | 3.10 | legacy | type-alias/legacy_03.py | x = ClassName
def foo() -> None:
x = ClassName
| pydocs | pep-0613.rst:114 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-alias | 613 | 3.10 | modern | type-alias/modern_04.py | x: TypeAlias = ClassName
def foo() -> None:
x = ClassName
def bar() -> None:
x: TypeAlias = ClassName
| pydocs | pep-0613.rst:127 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-alias | 613 | 3.10 | legacy | type-alias/legacy_04.py | x = 1 # untyped global expression
x: int = 1 # typed global expression
x = int # type alias
x: Type[int] = int # typed global expression
| pydocs | pep-0613.rst:152 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-alias | 613 | 3.10 | modern | type-alias/modern_05.py | x = 1 # untyped global expression
x: int = 1 # typed global expression
x = int # untyped global expression (see note below)
x: Type[int] = int # typed global expression
x: TypeAlias = int # type alias
x: TypeAlias = "MyClass" # type alias
| pydocs | pep-0613.rst:163 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-alias | 613 | 3.10 | modern | type-alias/modern_06.py | MyType: TypeAlias[int]
| pydocs | pep-0613.rst:200 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | type-alias | 613 | 3.10 | modern | type-alias/modern_07.py | MyType = TypeAlias[int]
| pydocs | pep-0613.rst:206 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | variadic-generics | 646 | 3.11 | legacy | variadic-generics/legacy_01.py | Axis1 = TypeVar('Axis1')
Axis2 = TypeVar('Axis2')
class Array1(Generic[Axis1]): ...
class Array2(Generic[Axis1, Axis2]): ...
| pydocs | pep-0646.rst:94 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_01.py | from typing import TypeVar, TypeVarTuple
DType = TypeVar('DType')
Shape = TypeVarTuple('Shape')
class Array(Generic[DType, *Shape]):
def __abs__(self) -> Array[DType, *Shape]: ...
def __add__(self, other: Array[DType, *Shape]) -> Array[DType, *Shape]: ...
| pydocs | pep-0646.rst:116 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_02.py | from typing import TypeVarTuple
Ts = TypeVarTuple('Ts')
| pydocs | pep-0646.rst:180 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_03.py | Shape = TypeVarTuple('Shape')
class Array(Generic[*Shape]): ...
Height = NewType('Height', int)
Width = NewType('Width', int)
x: Array[Height, Width] = Array()
| pydocs | pep-0646.rst:192 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_04.py | class Array(Generic[*Shape]):
def __init__(self, shape: Tuple[*Shape]):
self._shape: Tuple[*Shape] = shape
def get_shape(self) -> Tuple[*Shape]:
return self._shape
shape = (Height(480), Width(640))
x: Array[Height, Width] = Array(shape)
y = abs(x) # Inferred type is Array[Height, Width]
z = ... | pydocs | pep-0646.rst:228 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_05.py | # Unpacking using the star operator in new versions of Python
class Array(Generic[*Shape]): ...
# Unpacking using ``Unpack`` in older versions of Python
class Array(Generic[Unpack[Shape]]): ...
| pydocs | pep-0646.rst:269 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_06.py | def foo(arg1: Tuple[*Ts], arg2: Tuple[*Ts]): ...
a = (0,)
b = ('0',)
foo(a, b) # Can Ts be bound to Tuple[int | str]?
| pydocs | pep-0646.rst:296 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | variadic-generics | 646 | 3.11 | modern | variadic-generics/modern_07.py | def pointwise_multiply(
x: Array[*Shape],
y: Array[*Shape]
) -> Array[*Shape]: ...
x: Array[Height]
y: Array[Width]
z: Array[Height, Width]
pointwise_multiply(x, x) # Valid
pointwise_multiply(x, y) # Error
pointwise_multiply(x, z) # Error
| pydocs | pep-0646.rst:309 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.