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 | type-hints | 484 | 3.5 | modern | type-hints/modern_14.py | from typing import TypeVar, Generic
T = TypeVar('T')
class MyClass(Generic[T]):
def meth_1(self, x: T) -> T: ... # T here
def meth_2(self, x: T) -> T: ... # and here are always the same
a = MyClass() # type: MyClass[int]
a.meth_1(1) # OK
a.meth_2('a') # This is an error!
| pydocs | pep-0484.rst:481 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_15.py | T = TypeVar('T')
S = TypeVar('S')
class Foo(Generic[T]):
def method(self, x: T, y: S) -> S:
...
x = Foo() # type: Foo[int]
y = x.method(0, "abc") # inferred type of y is str
| pydocs | pep-0484.rst:497 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_16.py | T = TypeVar('T')
S = TypeVar('S')
def a_fun(x: T) -> None:
# this is OK
y = [] # type: List[T]
# but below is an error!
y = [] # type: List[S]
class Bar(Generic[T]):
# this is also an error
an_attr = [] # type: List[S]
def do_something(x: S) -> S: # this is OK though
...
| pydocs | pep-0484.rst:509 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_17.py | from typing import List
def a_fun(x: T) -> None:
# This is OK
a_list = [] # type: List[T]
...
# This is however illegal
class MyGeneric(Generic[T]):
...
| pydocs | pep-0484.rst:528 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_18.py | from typing import TypeVar, Generic
T = TypeVar('T')
class Node(Generic[T]):
x = None # type: T # Instance attribute (see below)
def __init__(self, label: T = None) -> None:
...
x = Node('') # Inferred type is Node[str]
y = Node(0) # Inferred type is Node[int]
z = Node() # Inferred type is Nod... | pydocs | pep-0484.rst:579 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_01.py | # (continued from previous example)
a = Node() # type: Node[int]
b = Node() # type: Node[str]
| pydocs | pep-0484.rst:596 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_02.py | data = collections.defaultdict() # type: DefaultDict[int, bytes]
| pydocs | pep-0484.rst:641 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_19.py | from typing import Dict, List, Optional
class Node:
...
class SymbolTable(Dict[str, List[Node]]):
def push(self, name: str, node: Node) -> None:
self.setdefault(name, []).append(node)
def pop(self, name: str) -> Node:
return self[name].pop()
def lookup(self, name: str) -> Optional[No... | pydocs | pep-0484.rst:660 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_20.py | from typing import TypeVar, Sized
ST = TypeVar('ST', bound=Sized)
def longer(x: ST, y: ST) -> ST:
if len(x) > len(y):
return x
else:
return y
longer([1], [1, 2]) # ok, return type List[int]
longer({1}, {1, 2}) # ok, return type Set[int]
longer([1], {1, 2}) # ok, return type Collection[int]... | pydocs | pep-0484.rst:725 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_21.py | from typing import TypeVar, Generic, Iterable, Iterator
T_co = TypeVar('T_co', covariant=True)
class ImmutableList(Generic[T_co]):
def __init__(self, items: Iterable[T_co]) -> None: ...
def __iter__(self) -> Iterator[T_co]: ...
...
class Employee: ...
class Manager(Employee): ...
def dump_employees(emp... | pydocs | pep-0484.rst:783 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_22.py | from typing import TypeVar
class Employee: ...
class Manager(Employee): ...
E = TypeVar('E', bound=Employee)
def dump_employee(e: E) -> None: ...
dump_employee(Manager()) # OK
| pydocs | pep-0484.rst:818 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_23.py | B_co = TypeVar('B_co', covariant=True)
def bad_func(x: B_co) -> B_co: # Flagged as error by a type checker
...
| pydocs | pep-0484.rst:832 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_24.py | class Tree:
def __init__(self, left: Tree, right: Tree):
self.left = left
self.right = right
| pydocs | pep-0484.rst:870 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_25.py | class Tree:
def __init__(self, left: 'Tree', right: 'Tree'):
self.left = left
self.right = right
| pydocs | pep-0484.rst:877 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_26.py | class Tree:
...
def leaves(self) -> List['Tree']:
...
| pydocs | pep-0484.rst:896 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_27.py | # File models/a.py
from models.b import B
class A(Model):
def foo(self, b: B): ...
# File models/b.py
from models.a import A
class B(Model):
def bar(self, a: A): ...
# File main.py
from models.a import A
from models.b import B
| pydocs | pep-0484.rst:907 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:927 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:949 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_30.py | def handle_employee(e: Union[Employee, None]) -> None: ...
| pydocs | pep-0484.rst:965 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:970 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_32.py | def handle_employee(e: Employee = None): ...
| pydocs | pep-0484.rst:977 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_33.py | def handle_employee(e: Optional[Employee] = None) -> None: ...
| pydocs | pep-0484.rst:981 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:993 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1007 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1031 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1063 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1073 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1094 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1113 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1124 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1154 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_41.py | def new_user(user_class: type) -> User:
...
| pydocs | pep-0484.rst:1162 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1168 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1186 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1233 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1249 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1261 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1306 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_48.py | def foo(*args: str, **kwds: int): ...
| pydocs | pep-0484.rst:1328 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_49.py | def foo(x: AnyStr, y: AnyStr = ...) -> AnyStr: ...
| pydocs | pep-0484.rst:1344 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1362 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1376 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1386 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1398 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1409 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1453 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1465 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_08.py | from typing import IO
stream = None # type: IO[str]
| pydocs | pep-0484.rst:1485 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:1527 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_55.py | class UserId(int):
pass
def get_by_user_id(user_id: UserId):
...
| pydocs | pep-0484.rst:1555 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_56.py | class Derived(Base):
def __init__(self, _x: Base) -> None:
...
| pydocs | pep-0484.rst:1567 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_57.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.rst:1576 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_58.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.rst:1595 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_59.py | def __getattr__(name) -> Any: ...
| pydocs | pep-0484.rst:1677 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_60.py | from typing import overload
class bytes:
...
@overload
def __getitem__(self, i: int) -> int: ...
@overload
def __getitem__(self, s: slice) -> bytes: ...
| pydocs | pep-0484.rst:1691 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_61.py | from typing import Union
class bytes:
...
def __getitem__(self, a: Union[int, slice]) -> Union[int, bytes]: ...
| pydocs | pep-0484.rst:1704 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_62.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.rst:1714 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_63.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.rst:1729 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_64.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.rst:1778 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:2160 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:2166 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_65.py | class Node:
"""Binary tree node."""
def __init__(self, left: Node, right: Node):
self.left = left
self.right = right
| pydocs | pep-0484.rst:2309 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | type-hints | 484 | 3.5 | modern | type-hints/modern_66.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.rst:2335 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:24 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:44 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:58 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:85 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variable-annotations | 526 | 3.6 | legacy | variable-annotations/legacy_03.py | path = None # type: Optional[str] # Path to module source
| pydocs | pep-0526.rst:101 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:140 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:148 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:164 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:170 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:179 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_07.py | a: int
print(a) # raises NameError
| pydocs | pep-0526.rst:192 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:198 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:213 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:226 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:267 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:287 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:301 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:315 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:339 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:356 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:374 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:382 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_19.py | def f():
x: NonexistentName # No error.
| pydocs | pep-0526.rst:423 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_20.py | x: NonexistentName # Error!
class X:
var: NonexistentName # Error!
| pydocs | pep-0526.rst:429 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_21.py | from typing import Dict
class Player:
__points: int
players: Dict[str, Player]
print(__annotations__)
# prints: {'players': typing.Dict[str, __main__.Player]}
print(Player.__annotations__)
# prints: {'_Player__points': <class 'int'>}
| pydocs | pep-0526.rst:439 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_22.py | class C:
__annotations__ = 42
x: int = 5 # raises TypeError
| pydocs | pep-0526.rst:456 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:470 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_24.py | alice: 'well done' = 'A+'
bob: 'what a shame' = 'F-'
| pydocs | pep-0526.rst:499 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_25.py | def foo(self):
slef.name: str
| pydocs | pep-0526.rst:659 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:39 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:128 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:148 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:174 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | protocol | 544 | 3.8 | modern | protocol/modern_01.py | from typing import Protocol
class SupportsClose(Protocol):
def close(self) -> None:
...
| pydocs | pep-0544.rst:293 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:338 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:359 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.12 | 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.rst:399 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.