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.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_28.py | # File models/a.py
from models import b
class A(Model):
def foo(self, b: 'b.B'): ...
# File models/b.py
from models import a
class B(Model):
def bar(self, a: 'a.A'): ...
# File main.py
from models.a import A
from models.b import B
| pydocs | pep-0484.txt:929 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_29.py | from typing import Union
def handle_employees(e: Union[Employee, Sequence[Employee]]) -> None:
if isinstance(e, Employee):
e = [e]
...
| pydocs | pep-0484.txt:951 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_30.py | def handle_employee(e: Union[Employee, None]) -> None: ...
| pydocs | pep-0484.txt:967 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_31.py | from typing import Optional
def handle_employee(e: Optional[Employee]) -> None: ...
| pydocs | pep-0484.txt:972 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_32.py | def handle_employee(e: Employee = None): ...
| pydocs | pep-0484.txt:979 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_33.py | def handle_employee(e: Optional[Employee] = None) -> None: ...
| pydocs | pep-0484.txt:983 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_03.py | _empty = object()
def func(x=_empty):
if x is _empty: # default argument value
return 0
elif x is None: # argument was provided and it's None
return 1
else:
return x * 2
| pydocs | pep-0484.txt:995 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_34.py | from typing import Union
from enum import Enum
class Empty(Enum):
token = 0
_empty = Empty.token
def func(x: Union[int, None, Empty] = _empty) -> int:
boom = x * 42 # This fails type check
if x is _empty:
return 0
elif x is None:
return 1
else: # At this point typechecker knows... | pydocs | pep-0484.txt:1009 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_35.py | class Reason(Enum):
timeout = 1
error = 2
def process(response: Union[str, Reason] = '') -> str:
if response is Reason.timeout:
return 'TIMEOUT'
elif response is Reason.error:
return 'ERROR'
else:
# response can be only str, all other possible values exhausted
return... | pydocs | pep-0484.txt:1033 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_36.py | from typing import Mapping
def use_map(m: Mapping) -> None: # Same as Mapping[Any, Any]
...
| pydocs | pep-0484.txt:1065 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_37.py | from typing import Tuple, List, Callable
def check_args(args: Tuple) -> bool:
...
check_args(()) # OK
check_args((42, 'abc')) # Also OK
check_args(3.14) # Flagged as error by a type checker
# A list of arbitrary callables is accepted by this function
def apply_callbacks(cbs: List[Callable]) ->... | pydocs | pep-0484.txt:1075 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_38.py | from typing import NoReturn
def stop() -> NoReturn:
raise RuntimeError('no way')
| pydocs | pep-0484.txt:1096 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_39.py | # continue from first example
def g(x: int) -> int:
if x > 0:
return x
stop()
return 'whatever works' # Error might be not reported by some checkers
# that ignore errors in unreachable blocks
| pydocs | pep-0484.txt:1115 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_40.py | from typing import List, NoReturn
# All of the following are errors
def bad1(x: NoReturn) -> int:
...
bad2 = None # type: NoReturn
def bad3() -> List[NoReturn]:
...
| pydocs | pep-0484.txt:1126 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_04.py | def new_user(user_class):
user = user_class()
# (Here we could write the user object to a database)
return user
| pydocs | pep-0484.txt:1156 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_41.py | def new_user(user_class: type) -> User:
...
| pydocs | pep-0484.txt:1164 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_42.py | U = TypeVar('U', bound=User)
def new_user(user_class: Type[U]) -> U:
...
| pydocs | pep-0484.txt:1170 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_43.py | def new_non_team_user(user_class: Type[Union[BasicUser, ProUser]]):
user = new_user(user_class)
...
| pydocs | pep-0484.txt:1188 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_44.py | def new_pro_user(pro_user_class: Type[ProUser]):
user = new_user(pro_user_class) # OK
...
| pydocs | pep-0484.txt:1235 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_45.py | T = TypeVar('T', bound='Copyable')
class Copyable:
def copy(self: T) -> T:
...
# return a copy of self
class C(Copyable): ...
c = C()
c2 = c.copy() # type here should be C
| pydocs | pep-0484.txt:1251 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_46.py | T = TypeVar('T', bound='C')
class C:
@classmethod
def factory(cls: Type[T]) -> T:
...
# make a new instance of cls
class D(C): ...
d = D.factory() # type here should be D
| pydocs | pep-0484.txt:1263 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_47.py | import typing
if typing.TYPE_CHECKING:
import expensive_mod
def a_func(arg: 'expensive_mod.SomeClass') -> None:
a_var = arg # type: expensive_mod.SomeClass
...
| pydocs | pep-0484.txt:1308 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_48.py | def foo(*args: str, **kwds: int): ...
| pydocs | pep-0484.txt:1330 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_49.py | def foo(x: AnyStr, y: AnyStr = ...) -> AnyStr: ...
| pydocs | pep-0484.txt:1346 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_50.py | def quux(__x: int, __y__: int = 0) -> None: ...
quux(3, __y__=1) # This call is fine.
quux(__x=3) # This call is an error.
| pydocs | pep-0484.txt:1364 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_51.py | def echo_round() -> Generator[int, float, str]:
res = yield
while res:
res = yield round(res)
return 'OK'
| pydocs | pep-0484.txt:1378 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_52.py | async def spam(ignored: int) -> str:
return 'spam'
async def foo() -> None:
bar = await spam(42) # type: str
| pydocs | pep-0484.txt:1388 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_05.py | from typing import List, Coroutine
c = None # type: Coroutine[List[str], str, int]
...
x = c.send('hi') # type: List[str]
async def bar() -> None:
x = await c # type: int
| pydocs | pep-0484.txt:1400 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_53.py | def op() -> typing.Awaitable[str]:
if cond:
return spam(42)
else:
return asyncio.Future(...)
| pydocs | pep-0484.txt:1411 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_06.py | x = [] # type: List[Employee]
x, y, z = [], [], [] # type: List[int], List[int], List[str]
x, y, z = [], [], [] # type: (List[int], List[int], List[str])
a, b, *c = range(5) # type: float, float, List[float]
x = [1, 2] # type: List[int]
| pydocs | pep-0484.txt:1455 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_07.py | with frobnicate() as foo: # type: int
# Here foo is an int
...
for x, y in points: # type: float, float
# Here x and y are floats
...
| pydocs | pep-0484.txt:1467 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_08.py | from typing import IO
stream = None # type: IO[str]
| pydocs | pep-0484.txt:1487 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_54.py | from typing import List, cast
def find_first_str(a: List[object]) -> str:
index = next(i for i, x in enumerate(a) if isinstance(x, str))
# We only get here if there's at least one string in a
return cast(str, a[index])
| pydocs | pep-0484.txt:1529 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_55.py | class Derived(Base):
def __init__(self, _x: Base) -> None:
...
| pydocs | pep-0484.txt:1569 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_56.py | UserId = NewType('UserId', int)
def name_by_id(user_id: UserId) -> str:
...
UserId('user') # Fails type check
name_by_id(42) # Fails type check
name_by_id(UserId(42)) # OK
num = UserId(5) + 1 # type: int
| pydocs | pep-0484.txt:1578 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_57.py | class PacketId:
def __init__(self, major: int, minor: int) -> None:
self._major = major
self._minor = minor
TcpPacketId = NewType('TcpPacketId', PacketId)
packet = PacketId(100, 100)
tcp_packet = TcpPacketId(packet) # OK
tcp_packet = TcpPacketId(127, 0) # Fails in type checker and at runtime
| pydocs | pep-0484.txt:1597 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_58.py | def __getattr__(name) -> Any: ...
| pydocs | pep-0484.txt:1679 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_59.py | from typing import overload
class bytes:
...
@overload
def __getitem__(self, i: int) -> int: ...
@overload
def __getitem__(self, s: slice) -> bytes: ...
| pydocs | pep-0484.txt:1693 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_60.py | from typing import Union
class bytes:
...
def __getitem__(self, a: Union[int, slice]) -> Union[int, bytes]: ...
| pydocs | pep-0484.txt:1706 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_61.py | from typing import Callable, Iterable, Iterator, Tuple, TypeVar, overload
T1 = TypeVar('T1')
T2 = TypeVar('T2')
S = TypeVar('S')
@overload
def map(func: Callable[[T1], S], iter1: Iterable[T1]) -> Iterator[S]: ...
@overload
def map(func: Callable[[T1, T2], S],
iter1: Iterable[T1], iter2: Iterable[T2]) -> Itera... | pydocs | pep-0484.txt:1716 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_62.py | @overload
def map(func: None, iter1: Iterable[T1]) -> Iterable[T1]: ...
@overload
def map(func: None,
iter1: Iterable[T1],
iter2: Iterable[T2]) -> Iterable[Tuple[T1, T2]]: ...
| pydocs | pep-0484.txt:1731 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_63.py | from typing import TypeVar, Text
AnyStr = TypeVar('AnyStr', Text, bytes)
def concat1(x: AnyStr, y: AnyStr) -> AnyStr: ...
@overload
def concat2(x: str, y: str) -> str: ...
@overload
def concat2(x: bytes, y: bytes) -> bytes: ...
| pydocs | pep-0484.txt:1780 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_09.py | def add(a, b): # type: (int, int) -> int
return a + b
| pydocs | pep-0484.txt:2162 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_10.py | def f():
'''Docstring'''
# type: () -> None # Error!
def g():
'''Docstring'''
# # type: () -> None # This is OK
| pydocs | pep-0484.txt:2168 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_64.py | class Node:
"""Binary tree node."""
def __init__(self, left: Node, right: Node):
self.left = left
self.right = right
| pydocs | pep-0484.txt:2311 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | type-hints | 484 | 3.5 | modern | type-hints/modern_65.py | from __future__ import annotations
class ImSet:
def add(self, a: ImSet) -> List[ImSet]: ...
assert ImSet.add.__annotations__ == {'a': 'ImSet', 'return': 'List[ImSet]'}
| pydocs | pep-0484.txt:2337 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | numeric-underscores | 515 | 3.6 | modern | numeric-underscores/modern_01.py | # grouping decimal numbers by thousands
amount = 10_000_000.0
# grouping hexadecimal addresses by words
addr = 0xCAFE_F00D
# grouping bits into nibbles in a binary literal
flags = 0b_0011_1111_0100_1110
# same, for string conversions
flags = int('0b_1111_0000', 2)
| pydocs | pep-0515.txt:27 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | legacy | variable-annotations/legacy_01.py | # 'primes' is a list of integers
primes = [] # type: List[int]
# 'captain' is a string (Note: initial value is a problem)
captain = ... # type: str
class Starship:
# 'stats' is a class variable
stats = {} # type: Dict[str, int]
| pydocs | pep-0526.txt:43 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_01.py | primes: List[int] = []
captain: str # Note: no initial value!
class Starship:
stats: ClassVar[Dict[str, int]] = {}
| pydocs | pep-0526.txt:57 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | legacy | variable-annotations/legacy_02.py | if some_value:
my_var = function() # type: Logger
else:
my_var = another_function() # Why isn't there a type here?
| pydocs | pep-0526.txt:84 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | legacy | variable-annotations/legacy_03.py | path = None # type: Optional[str] # Path to module source
| pydocs | pep-0526.txt:100 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_02.py | my_var: int
my_var = 5 # Passes type check.
other_var: int = 'a' # Flagged as error by type checker,
# but OK at runtime.
| pydocs | pep-0526.txt:139 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_03.py | var = value # type: annotation
var: annotation; var = value
var: annotation = value
| pydocs | pep-0526.txt:147 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_04.py | some_number: int # variable without initial value
some_list: List[int] = [] # variable with initial value
| pydocs | pep-0526.txt:163 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_05.py | sane_world: bool
if 2+2 == 4:
sane_world = True
else:
sane_world = False
| pydocs | pep-0526.txt:169 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_06.py | # Tuple packing with variable annotation syntax
t: Tuple[int, ...] = (1, 2, 3)
# or
t: Tuple[int, ...] = 1, 2, 3 # This only works in Python 3.8+
# Tuple unpacking with variable annotation syntax
header: str
kind: int
body: Optional[List[str]]
header, kind, body = message
| pydocs | pep-0526.txt:178 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_07.py | a: int
print(a) # raises NameError
| pydocs | pep-0526.txt:191 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_08.py | def f():
a: int
print(a) # raises UnboundLocalError
# Commenting out the a: int makes it a NameError.
| pydocs | pep-0526.txt:197 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_09.py | a: int
a: str # Static type checker may or may not warn about this.
| pydocs | pep-0526.txt:212 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_10.py | class BasicStarship:
captain: str = 'Picard' # instance variable with default
damage: int # instance variable without default
stats: ClassVar[Dict[str, int]] = {} # class variable
| pydocs | pep-0526.txt:225 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_11.py | class Starship:
captain: str = 'Picard'
damage: int
stats: ClassVar[Dict[str, int]] = {}
def __init__(self, damage: int, captain: str = None):
self.damage = damage
if captain:
self.captain = captain # Else keep the default
def hit(self):
Starship.stats['hits'] ... | pydocs | pep-0526.txt:266 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_12.py | from typing import Generic, TypeVar
T = TypeVar('T')
class Box(Generic[T]):
def __init__(self, content):
self.content: T = content
| pydocs | pep-0526.txt:286 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_13.py | class Cls:
pass
c = Cls()
c.x: int = 0 # Annotates c.x with int.
c.y: int # Annotates c.y with int.
d = {}
d['a']: int = 0 # Annotates d['a'] with int.
d['b']: int # Annotates d['b'] with int.
| pydocs | pep-0526.txt:300 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_14.py | (x): int # Annotates x with int, (x) treated as expression by compiler.
(y): int = 0 # Same situation here.
| pydocs | pep-0526.txt:314 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_15.py | a: int
for a in my_iter:
...
f: MyFile
with myfunc() as f:
...
| pydocs | pep-0526.txt:338 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_16.py | # file lib.pyi
ADDRESS: unicode = ...
class Error:
cause: Union[str, unicode]
| pydocs | pep-0526.txt:355 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_17.py | code: int
class Point:
coords: Tuple[int, int]
label: str = '<unknown>'
| pydocs | pep-0526.txt:373 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_18.py | code:int # No space after colon
code : int # Space before colon
class Test:
result: int=0 # No spaces around equality sign
| pydocs | pep-0526.txt:381 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_19.py | def f():
x: NonexistentName # No error.
| pydocs | pep-0526.txt:422 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_20.py | x: NonexistentName # Error!
class X:
var: NonexistentName # Error!
| pydocs | pep-0526.txt:428 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_21.py | from typing import Dict
class Player:
...
players: Dict[str, Player]
__points: int
print(__annotations__)
# prints: {'players': typing.Dict[str, __main__.Player],
# '_Player__points': <class 'int'>}
| pydocs | pep-0526.txt:438 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_22.py | class C:
__annotations__ = 42
x: int = 5 # raises TypeError
| pydocs | pep-0526.txt:455 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_23.py | from typing import Dict, ClassVar, get_type_hints
class Starship:
hitpoints: int = 50
stats: ClassVar[Dict[str, int]] = {}
shield: int = 100
captain: str
def __init__(self, captain: str) -> None:
...
assert get_type_hints(Starship) == {'hitpoints': int,
'... | pydocs | pep-0526.txt:469 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_24.py | alice: 'well done' = 'A+'
bob: 'what a shame' = 'F-'
| pydocs | pep-0526.txt:498 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_25.py | def foo(self):
slef.name: str
| pydocs | pep-0526.txt:658 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | legacy | protocol/legacy_01.py | from typing import Sized, Iterable, Iterator
class Bucket(Sized, Iterable[int]):
...
def __len__(self) -> int: ...
def __iter__(self) -> Iterator[int]: ...
| pydocs | pep-0544.txt:38 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | legacy | protocol/legacy_02.py | from zope.interface import Interface, Attribute, implementer
class IEmployee(Interface):
name = Attribute("Name of employee")
def do(work):
"""Do some work"""
@implementer(IEmployee)
class Employee:
name = 'Anonymous'
def do(self, work):
return work.start()
| pydocs | pep-0544.txt:127 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | legacy | protocol/legacy_03.py | from zope.interface import invariant
def required_contact(obj):
if not (obj.email or obj.phone):
raise Exception("At least one contact info is required")
class IPerson(Interface):
name = Attribute("Name")
email = Attribute("Email Address")
phone = Attribute("Phone Number")
invariant(requ... | pydocs | pep-0544.txt:147 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | legacy | protocol/legacy_04.py | from abc import ABC
class MyTuple(ABC):
pass
MyTuple.register(tuple)
assert issubclass(tuple, MyTuple)
assert isinstance((), MyTuple)
| pydocs | pep-0544.txt:173 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_01.py | from typing import Protocol
class SupportsClose(Protocol):
def close(self) -> None:
...
| pydocs | pep-0544.txt:292 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_02.py | from typing import Protocol
from abc import abstractmethod
class Example(Protocol):
def first(self) -> int: # This is a protocol member
return 42
@abstractmethod
def second(self) -> int: # Method without a default implementation
raise NotImplementedError
| pydocs | pep-0544.txt:337 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_03.py | from typing import Protocol, List
class Template(Protocol):
name: str # This is a protocol member
value: int = 0 # This one too (with default)
def method(self) -> None:
self.temp: List[int] = [] # Error in type checker
class Concrete:
def __init__(self, name: str, value: int) -> None... | pydocs | pep-0544.txt:358 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_04.py | class PColor(Protocol):
@abstractmethod
def draw(self) -> str:
...
def complex_method(self) -> int:
...
# some complex code here
class NiceColor(PColor):
def draw(self) -> str:
return "deep blue"
class BadColor(PColor):
def draw(self) -> str:
return super().... | pydocs | pep-0544.txt:398 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_05.py | class RGB(Protocol):
rgb: Tuple[int, int, int]
@abstractmethod
def intensity(self) -> int:
return 0
class Point(RGB):
def __init__(self, red: int, green: int, blue: str) -> None:
self.rgb = red, green, blue # Error, 'blue' must be 'int'
# Type checker might warn that 'intensity' ... | pydocs | pep-0544.txt:433 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_06.py | from typing import Sized, Protocol
class SizedAndClosable(Sized, Protocol):
def close(self) -> None:
...
| pydocs | pep-0544.txt:470 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_07.py | from typing import Sized
class SupportsClose(Protocol):
def close(self) -> None:
...
class SizedAndClosable(Sized, SupportsClose, Protocol):
pass
| pydocs | pep-0544.txt:483 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_08.py | class Iterable(Protocol[T]):
@abstractmethod
def __iter__(self) -> Iterator[T]:
...
| pydocs | pep-0544.txt:512 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_09.py | T = TypeVar('T')
T_co = TypeVar('T_co', covariant=True)
T_contra = TypeVar('T_contra', contravariant=True)
class Box(Protocol[T_co]):
def content(self) -> T_co:
...
box: Box[float]
second_box: Box[int]
box = second_box # This is OK due to the covariance of 'Box'.
class Sender(Protocol[T_contra]):
de... | pydocs | pep-0544.txt:524 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_10.py | T = TypeVar('T')
class AnotherBox(Protocol[T]): # Error, this protocol is covariant in T,
def content(self) -> T: # not invariant.
...
| pydocs | pep-0544.txt:555 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_11.py | class Traversable(Protocol):
def leaves(self) -> Iterable['Traversable']:
...
| pydocs | pep-0544.txt:570 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_12.py | C = TypeVar('C', bound='Copyable')
class Copyable(Protocol):
def copy(self: C) -> C:
...
class One:
def copy(self) -> 'One':
...
T = TypeVar('T', bound='Other')
class Other:
def copy(self: T) -> T:
...
c: Copyable
c = One() # OK
c = Other() # Also OK
| pydocs | pep-0544.txt:600 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_13.py | from typing import Optional, List, Protocol
class Combiner(Protocol):
def __call__(self, *vals: bytes,
maxlen: Optional[int] = None) -> List[bytes]: ...
def good_cb(*vals: bytes, maxlen: Optional[int] = None) -> List[bytes]:
...
def bad_cb(*vals: bytes, maxitems: Optional[int]) -> List[bytes]... | pydocs | pep-0544.txt:626 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_14.py | # file lib.py
from typing import Sized
T = TypeVar('T', contravariant=True)
class ListLike(Sized, Protocol[T]):
def append(self, x: T) -> None:
pass
def populate(lst: ListLike[int]) -> None:
...
# file main.py
from lib import populate # Note that ListLike is NOT imported
class MockStack:
def __... | pydocs | pep-0544.txt:672 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_15.py | from typing import Union, Optional, Protocol
class Exitable(Protocol):
def exit(self) -> int:
...
class Quittable(Protocol):
def quit(self) -> Optional[int]:
...
def finish(task: Union[Exitable, Quittable]) -> int:
...
class DefaultJob:
...
def quit(self) -> int:
return 0
f... | pydocs | pep-0544.txt:702 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_16.py | from typing import Iterable, Hashable
class HashableFloats(Iterable[float], Hashable, Protocol):
pass
def cached_func(args: HashableFloats) -> float:
...
cached_func((1, 2, 3)) # OK, tuple is both hashable and iterable
| pydocs | pep-0544.txt:722 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_17.py | class Proto(Protocol):
@abstractmethod
def meth(self) -> int:
...
class Concrete:
def meth(self) -> int:
return 42
def fun(cls: Type[Proto]) -> int:
return cls().meth() # OK
fun(Proto) # Error
fun(Concrete) # OK
| pydocs | pep-0544.txt:743 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_18.py | from typing import Any, Protocol
class ProtoA(Protocol):
def meth(self, x: int) -> int: ...
class ProtoB(Protocol):
def meth(self, obj: Any, x: int) -> int: ...
class C:
def meth(self, x: int) -> int: ...
a: ProtoA = C # Type check error, signatures don't match!
b: ProtoB = C # OK
| pydocs | pep-0544.txt:772 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_19.py | from typing import NewType, Protocol, Iterator
class Id(Protocol):
code: int
secrets: Iterator[bytes]
UserId = NewType('UserId', Id) # Error, can't provide distinct type
| pydocs | pep-0544.txt:793 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_20.py | from typing import TypeVar, Reversible, Iterable, Sized
T = TypeVar('T')
class SizedIterable(Iterable[T], Sized, Protocol):
pass
CompatReversible = Union[Reversible[T], SizedIterable[T]]
| pydocs | pep-0544.txt:804 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
train | 3.10 | protocol | 544 | 3.8 | modern | protocol/modern_21.py | # file default_config.py
timeout = 100
one_flag = True
other_flag = False
# file main.py
import default_config
from typing import Protocol
class Options(Protocol):
timeout: int
one_flag: bool
other_flag: bool
def setup(options: Options) -> None:
...
setup(default_config) # OK
| pydocs | pep-0544.txt:819 | 2c89d6ac2ccea9d8691f1b75d95c2d7294cc32ca | CC0-1.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.