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.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_01.py | def greeting(name: str) -> str:
return 'Hello ' + name
| pydocs | pep-0484.txt:37 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_02.py | Url = str
def retry(url: Url, retry_count: int) -> None: ...
| pydocs | pep-0484.txt:203 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_03.py | from typing import TypeVar, Iterable, Tuple
T = TypeVar('T', int, float, complex)
Vector = Iterable[Tuple[T, T]]
def inproduct(v: Vector[T]) -> T:
return sum(x*y for x, y in v)
def dilate(v: Vector[T], scale: T) -> Vector[T]:
return ((x * scale, y * scale) for x, y in v)
vec = [] # type: Vector[float]
| pydocs | pep-0484.txt:215 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_04.py | from typing import TypeVar, Iterable, Tuple
T = TypeVar('T', int, float, complex)
def inproduct(v: Iterable[Tuple[T, T]]) -> T:
return sum(x*y for x, y in v)
def dilate(v: Iterable[Tuple[T, T]], scale: T) -> Iterable[Tuple[T, T]]:
return ((x * scale, y * scale) for x, y in v)
vec = [] # type: Iterable[Tuple[... | pydocs | pep-0484.txt:229 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_05.py | from typing import Callable
def feeder(get_next_item: Callable[[], str]) -> None:
...
# Body
def async_query(on_success: Callable[[int], None],
on_error: Callable[[int, Exception], None]) -> None:
...
# Body
| pydocs | pep-0484.txt:247 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_06.py | def partial(func: Callable[..., str], *args) -> Callable[..., str]:
...
# Body
| pydocs | pep-0484.txt:260 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_07.py | from typing import Mapping, Set
def notify_by_email(employees: Set[Employee], overrides: Mapping[str, str]) -> None: ...
| pydocs | pep-0484.txt:287 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_08.py | from typing import Sequence, TypeVar
T = TypeVar('T') # Declare type variable
def first(l: Sequence[T]) -> T: # Generic function
return l[0]
| pydocs | pep-0484.txt:294 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_09.py | from typing import TypeVar
AnyStr = TypeVar('AnyStr', str, bytes)
def concat(x: AnyStr, y: AnyStr) -> AnyStr:
return x + y
| pydocs | pep-0484.txt:314 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_10.py | def count_truthy(elements: List[Any]) -> int:
return sum(1 for elem in elements if element)
| pydocs | pep-0484.txt:343 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_11.py | from typing import Iterable
def zero_all_vars(vars: Iterable[LoggedVar[int]]) -> None:
for var in vars:
var.set(0)
| pydocs | pep-0484.txt:384 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_12.py | from typing import TypeVar, Generic
T = TypeVar('T')
def fun_1(x: T) -> T: ... # T here
def fun_2(x: T) -> T: ... # and here could be different
fun_1(1) # This is OK, T is inferred to be int
fun_2('a') # This is aslo OK, now T is str
| pydocs | pep-0484.txt:467 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_13.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.txt:481 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_14.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.txt:497 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_15.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.txt:509 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_16.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.txt:528 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_17.py | from typing import TypeVar, Generic
T = TypeVar('T')
class Node(Generic[T]):
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 Node[Any]
| pydocs | pep-0484.txt:566 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | 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.txt:582 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_18.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.txt:619 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_19.py | from typing import TypeVar
class Comparable(metaclass=ABCMeta):
@abstractmethod
def __lt__(self, other: Any) -> bool: ...
... # __gt__ etc. as well
CT = TypeVar('CT', bound=Comparable)
def min(x: CT, y: CT) -> CT:
if x < y:
return x
else:
return y
min(1, 2) # ok, return type int
... | pydocs | pep-0484.txt:685 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_20.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.txt:753 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_21.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.txt:788 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_22.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.txt:802 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_23.py | class Tree:
def __init__(self, left: Tree, right: Tree):
self.left = left
self.right = right
| pydocs | pep-0484.txt:840 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | 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.txt:847 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_25.py | class Tree:
...
def leaves(self) -> List['Tree']:
...
| pydocs | pep-0484.txt:866 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_26.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.txt:877 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_27.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:897 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_28.py | from typing import Union
def handle_employees(e: Union[Employee, Sequence[Employee]]) -> None:
if isinstance(e, Employee):
e = [e]
...
| pydocs | pep-0484.txt:919 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_29.py | def handle_employee(e: Union[Employee, None]) -> None: ...
| pydocs | pep-0484.txt:935 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_30.py | from typing import Optional
def handle_employee(e: Optional[Employee]) -> None: ...
| pydocs | pep-0484.txt:940 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_31.py | def handle_employee(e: Employee = None): ...
| pydocs | pep-0484.txt:947 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_32.py | def handle_employee(e: Optional[Employee] = None) -> None: ...
| pydocs | pep-0484.txt:951 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_02.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:961 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_33.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:975 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_34.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:999 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_35.py | from typing import Mapping
def use_map(m: Mapping) -> None: # Same as Mapping[Any, Any]
...
| pydocs | pep-0484.txt:1031 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_36.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:1041 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_03.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:1075 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_37.py | def new_user(user_class: type) -> User:
...
| pydocs | pep-0484.txt:1083 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_38.py | U = TypeVar('U', bound=User)
def new_user(user_class: Type[U]) -> U:
...
| pydocs | pep-0484.txt:1089 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_39.py | def new_non_team_user(user_class: Type[Union[BasicUser, ProUser]]):
user = new_user(user_class)
...
| pydocs | pep-0484.txt:1107 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_40.py | def new_pro_user(pro_user_class: Type[ProUser]):
user = new_user(pro_user_class) # OK
...
| pydocs | pep-0484.txt:1150 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_41.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:1166 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_42.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:1178 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_43.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:1223 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_44.py | def foo(*args: str, **kwds: int): ...
| pydocs | pep-0484.txt:1245 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_45.py | def foo(x: AnyStr, y: AnyStr = ...) -> AnyStr: ...
| pydocs | pep-0484.txt:1261 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_46.py | def quux(__x: int) -> None: ...
quux(3) # This call is fine.
quux(__x=3) # This call is an error.
| pydocs | pep-0484.txt:1279 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_47.py | def echo_round() -> Generator[int, float, str]:
res = yield
while res:
res = yield round(res)
return 'OK'
| pydocs | pep-0484.txt:1293 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_48.py | async def spam(ignored: int) -> str:
return 'spam'
async def foo() -> None:
bar = await spam(42) # type: str
| pydocs | pep-0484.txt:1303 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_49.py | def op() -> typing.Awaitable[str]:
if cond:
return spam(42)
else:
return asyncio.Future(...)
| pydocs | pep-0484.txt:1326 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_04.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])
x = [
1,
2,
] # type: List[int]
| pydocs | pep-0484.txt:1370 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_05.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:1384 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_06.py | from typing import IO
stream = ... # type: IO[str]
| pydocs | pep-0484.txt:1396 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_07.py | from typing import IO
stream = None # type: IO[str]
| pydocs | pep-0484.txt:1402 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_50.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:1437 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_51.py | class Derived(Base):
def __init__(self, _x: Base) -> None:
...
| pydocs | pep-0484.txt:1477 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_52.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:1486 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_53.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:1504 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_54.py | def __getattr__(name) -> Any: ...
| pydocs | pep-0484.txt:1570 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_55.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:1584 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_56.py | from typing import Union
class bytes:
...
def __getitem__(self, a: Union[int, slice]) -> Union[int, bytes]: ...
| pydocs | pep-0484.txt:1597 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_57.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:1622 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_58.py | from typing import TypeVar
AnyStr = TypeVar('AnyStr', str, 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:1671 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_08.py | def add(a, b): # type: (int, int) -> int
return a + b
| pydocs | pep-0484.txt:2042 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | legacy | type-hints/legacy_09.py | def f():
'''Docstring'''
# type: () -> None # Error!
def g():
'''Docstring'''
# # type: () -> None # This is OK
| pydocs | pep-0484.txt:2048 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_59.py | class Node:
"""Binary tree node."""
def __init__(self, left: Node, right: Node):
self.left = left
self.right = right
| pydocs | pep-0484.txt:2183 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | type-hints | 484 | 3.5 | modern | type-hints/modern_60.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:2209 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | 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 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | 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 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | 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 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | 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 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | 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 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | 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 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | 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 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | 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 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | 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 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_06.py | # Tuple packing with variable annotation syntax
t: Tuple[int, ...] = (1, 2, 3)
# Tuple unpacking with variable annotation syntax
header: str
kind: int
body: Optional[List[str]]
header, kind, body = message
| pydocs | pep-0526.txt:178 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_07.py | a: int
print(a) # raises NameError
| pydocs | pep-0526.txt:189 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | 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:195 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | 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:210 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | 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:223 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | 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:260 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_12.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:294 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_13.py | (x): int # Annotates x with int, (x) treated as expression by compiler.
(y): int = 0 # Same situation here.
| pydocs | pep-0526.txt:308 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_14.py | a: int
for a in my_iter:
...
f: MyFile
with myfunc() as f:
...
| pydocs | pep-0526.txt:332 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_15.py | def f():
x: NonexistentName # No error.
| pydocs | pep-0526.txt:375 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_16.py | x: NonexistentName # Error!
class X:
var: NonexistentName # Error!
| pydocs | pep-0526.txt:381 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_17.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:391 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_18.py | class C:
__annotations__ = 42
x: int = 5 # raises TypeError
| pydocs | pep-0526.txt:408 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_19.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:422 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_20.py | alice: 'well done' = 'A+'
bob: 'what a shame' = 'F-'
| pydocs | pep-0526.txt:451 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | variable-annotations | 526 | 3.6 | modern | variable-annotations/modern_21.py | def foo(self):
slef.name: str
| pydocs | pep-0526.txt:606 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | fstring | 498 | 3.6 | modern | fstring/modern_01.py | import datetime
name = 'Fred'
age = 50
anniversary = datetime.date(1991, 10, 12)
f'My name is {name}, my age next year is {age+1}, my anniversary is {anniversary:%A, %B %d, %Y}.'
f'He said his name is {name!r}.'
| pydocs | pep-0498.txt:36 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | fstring | 498 | 3.6 | legacy | fstring/legacy_01.py | msg = 'disk failure'
'error: %s' % msg
| pydocs | pep-0498.txt:62 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | fstring | 498 | 3.6 | legacy | fstring/legacy_02.py | msg = ('disk failure', 32)
'error: %s' % msg
| pydocs | pep-0498.txt:68 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | fstring | 498 | 3.6 | legacy | fstring/legacy_03.py | 'error: %s' % (msg,)
| pydocs | pep-0498.txt:76 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | fstring | 498 | 3.6 | legacy | fstring/legacy_04.py | value = 4 * 20
'The value is {value}.'.format(value=value)
| pydocs | pep-0498.txt:90 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
train | 3.6 | fstring | 498 | 3.6 | legacy | fstring/legacy_05.py | 'The value is {}.'.format(value)
| pydocs | pep-0498.txt:98 | e4c4632b001a0832f85245e6f5cc988fd0f72fe0 | CC0-1.0 |
End of preview. Expand in Data Studio
IdiomBench
Python idioms, the ways of writing code that Python Enhancement Proposals (PEPs) introduce, with examples per Python release. Each idiom has a modern form (what its PEP introduces) and may have legacy forms (what it replaces).
from datasets import load_dataset
ds = load_dataset("SwashBuckler001/idiombench", "python")
The cuts are also here as built: Python/<cut>/<version>/<idiom_id>/<form>_NN.py, with a
manifest.json per release. A release holds an idiom only from the release that introduced it,
and an example in every release where it is valid Python, so a detector can be run on a release
folder directly.
- train: code blocks from the idioms' PEPs, labelled modern or legacy. A train release folder
is everything the rule writer was given at that release: the examples, each idiom's PEP
(
<idiom_id>/pep-NNNN.rst) and Python's documentation (docs/), with the rule writer's own manifest inmanifest.json'spydocs. - validation: cases from independent test suites.
Examples per release
| cut | version | modern | legacy | unchanged | idioms |
|---|---|---|---|---|---|
| train | 3.6 | 126 | 22 | 0 | 6 |
| train | 3.7 | 164 | 28 | 0 | 9 |
| train | 3.8 | 277 | 47 | 0 | 15 |
| train | 3.9 | 334 | 73 | 0 | 20 |
| train | 3.10 | 387 | 79 | 0 | 26 |
| train | 3.11 | 518 | 89 | 0 | 33 |
| train | 3.12 | 593 | 93 | 0 | 39 |
| train | 3.13 | 643 | 93 | 0 | 43 |
| train | 3.14 | 685 | 95 | 0 | 46 |
| train | 3.15 | 776 | 111 | 0 | 53 |
| val | 3.6 | 44 | 17 | 22 | 4 |
| val | 3.7 | 77 | 18 | 21 | 7 |
| val | 3.8 | 140 | 30 | 33 | 13 |
| val | 3.9 | 170 | 44 | 41 | 18 |
| val | 3.10 | 224 | 63 | 58 | 24 |
| val | 3.11 | 259 | 66 | 62 | 32 |
| val | 3.12 | 290 | 66 | 66 | 37 |
| val | 3.13 | 324 | 72 | 68 | 41 |
| val | 3.14 | 358 | 99 | 85 | 44 |
| val | 3.15 | 401 | 125 | 99 | 51 |
Fields
| field | meaning |
|---|---|
cut |
train (the PEP examples the rule writer was given) or val (independent test suites) |
version |
the Python release this example is for |
idiom_id |
the idiom, as in the project's taxonomy |
pep |
the PEP that introduced the idiom |
introduced_in |
the release that introduced the idiom |
form |
modern (the form the PEP introduces), legacy (a form it replaces) or unchanged (a lookalike) |
file |
the example's file, /_NN.py, in Python/// |
code |
the example's code |
source |
where the example comes from: pydocs or a test suite |
origin |
where in the source, e.g. pep-0604.rst:52 or a test's id |
origin_revision |
the source's commit or tag |
license |
the source's licence |
Licence
Each row names its source's licence (CC0-1.0, MIT, PSF-2.0). PEPs are placed in the public
domain or under the CC0-1.0-Universal license. Python's documentation in docs/ is under the
PSF-2.0 licence.
- Downloads last month
- 56