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.7
async-generators
525
3.6
modern
async-generators/modern_10.py
async def ticker(delay, to): for i in range(to): yield i await asyncio.sleep(delay) async def run(): async for i in ticker(1, 10): print(i) import asyncio loop = asyncio.get_event_loop() try: loop.run_until_complete(run()) finally: loop.close()
pydocs
pep-0525.txt:599
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
context-vars
567
3.7
modern
context-vars/modern_01.py
# Declare a context variable 'var' with the default value 42. var = ContextVar('var', default=42)
pydocs
pep-0567.rst:132
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
context-vars
567
3.7
modern
context-vars/modern_02.py
ctx = contextvars.copy_context()
pydocs
pep-0567.rst:216
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
context-vars
567
3.7
modern
context-vars/modern_03.py
var = ContextVar('var') var.set('spam') def main(): # 'var' was set to 'spam' before # calling 'copy_context()' and 'ctx.run(main)', so: # var.get() == ctx[var] == 'spam' var.set('ham') # Now, after setting 'var' to 'ham': # var.get() == ctx[var] == 'ham' ctx = copy_context() # Any changes ...
pydocs
pep-0567.rst:226
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
context-vars
567
3.7
modern
context-vars/modern_04.py
ctx = contextvars.copy_context() # Print all context variables and their values in 'ctx': print(ctx.items()) # Print the value of 'some_variable' in context 'ctx': print(ctx[some_variable])
pydocs
pep-0567.rst:261
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
context-vars
567
3.7
modern
context-vars/modern_05.py
def call_soon(self, callback, *args, context=None): if context is None: context = contextvars.copy_context() # ... some time later context.run(callback, *args)
pydocs
pep-0567.rst:294
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
context-vars
567
3.7
modern
context-vars/modern_06.py
class Task: def __init__(self, coro): ... # Get the current context snapshot. self._context = contextvars.copy_context() self._loop.call_soon(self._step, context=self._context) def _step(self, exc=None): ... # Every advance of the wrapped coroutine is done in ...
pydocs
pep-0567.rst:305
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
context-vars
567
3.7
modern
context-vars/modern_07.py
var = ContextVar('var') ctx = copy_context() ctx[var] = 'value' print(ctx[var]) # Prints 'value' print(var.get()) # Raises a LookupError
pydocs
pep-0567.rst:693
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
context-vars
567
3.7
modern
context-vars/modern_08.py
ctx = copy_context() def func(): ctx[var] = 'value' # Contrary to the previous example, this would work # because 'func()' is running within 'ctx'. print(ctx[var]) print(var.get()) ctx.run(func)
pydocs
pep-0567.rst:703
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
context-vars
567
3.7
modern
context-vars/modern_09.py
# Framework: current_request: ContextVar[Request] = \ ContextVar('current_request') # Later, while handling an HTTP request: request: Request = current_request.get() # Work with the 'request' object: return request.method
pydocs
pep-0567.rst:737
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
context-vars
567
3.7
modern
context-vars/modern_10.py
# Framework: current_request: ContextVar[Optional[Request]] = \ ContextVar('current_request', initial_value=None) # Later, while handling an HTTP request: request: Optional[Request] = current_request.get() # Check if the current request object was set: if request is None: raise RuntimeError # Work with the ...
pydocs
pep-0567.rst:756
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
context-vars
567
3.7
legacy
context-vars/legacy_01.py
class PrecisionStorage(threading.local): # Subclass threading.local to specify a default value. value = 0.0 precision = PrecisionStorage() # To set a new precision: precision.value = 0.5 # To read the current precision: print(precision.value)
pydocs
pep-0567.rst:798
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
context-vars
567
3.7
modern
context-vars/modern_11.py
precision = contextvars.ContextVar('precision', default=0.0) # To set a new precision: precision.set(0.5) # To read the current precision: print(precision.get())
pydocs
pep-0567.rst:813
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
context-vars
567
3.7
modern
context-vars/modern_12.py
executor = ThreadPoolExecutor() current_context = contextvars.copy_context() executor.submit(current_context.run, some_function)
pydocs
pep-0567.rst:828
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
breakpoint
553
3.7
legacy
breakpoint/legacy_01.py
foo() import pdb; pdb.set_trace() bar()
pydocs
pep-0553.rst:28
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
breakpoint
553
3.7
modern
breakpoint/modern_01.py
foo() breakpoint() bar()
pydocs
pep-0553.rst:71
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
breakpoint
553
3.7
modern
breakpoint/modern_02.py
os.environ['PYTHONBREAKPOINT'] = 'foo.bar.baz' breakpoint() # Imports foo.bar and calls foo.bar.baz()
pydocs
pep-0553.rst:142
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
class-creation-hooks
487
3.6
modern
class-creation-hooks/modern_01.py
class QuestBase: # this is implicitly a @classmethod (see below for motivation) def __init_subclass__(cls, swallow, **kwargs): cls.swallow = swallow super().__init_subclass__(**kwargs) class Quest(QuestBase, swallow="african"): pass Quest.swallow
pydocs
pep-0487.txt:69
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
class-creation-hooks
487
3.6
modern
class-creation-hooks/modern_02.py
import weakref class WeakAttribute: def __get__(self, instance, owner): return instance.__dict__[self.name]() def __set__(self, instance, value): instance.__dict__[self.name] = weakref.ref(value) # this is the new initializer: def __set_name__(self, owner, name): self.name = n...
pydocs
pep-0487.txt:110
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
class-creation-hooks
487
3.6
modern
class-creation-hooks/modern_03.py
class PluginBase: subclasses = [] def __init_subclass__(cls, **kwargs): super().__init_subclass__(**kwargs) cls.subclasses.append(cls)
pydocs
pep-0487.txt:197
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
class-creation-hooks
487
3.6
modern
class-creation-hooks/modern_04.py
class Trait: def __init__(self, minimum, maximum): self.minimum = minimum self.maximum = maximum def __get__(self, instance, owner): return instance.__dict__[self.key] def __set__(self, instance, value): if self.minimum < value < self.maximum: instance.__dict__[...
pydocs
pep-0487.txt:216
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
modern
dataclasses/modern_01.py
@dataclass class InventoryItem: '''Class for keeping track of an item in inventory.''' name: str unit_price: float quantity_on_hand: int = 0 def total_cost(self) -> float: return self.unit_price * self.quantity_on_hand
pydocs
pep-0557.rst:43
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
legacy
dataclasses/legacy_01.py
def __init__(self, name: str, unit_price: float, quantity_on_hand: int = 0) -> None: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand def __repr__(self): return f'InventoryItem(name={self.name!r}, unit_price={self.unit_price!r}, quantity_on_hand={self.quantity_on_ha...
pydocs
pep-0557.rst:56
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
modern
dataclasses/modern_02.py
@dataclass class C: ... @dataclass() class C: ... @dataclass(init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False) class C: ...
pydocs
pep-0557.rst:179
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
modern
dataclasses/modern_03.py
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
pydocs
pep-0557.rst:268
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
modern
dataclasses/modern_04.py
@dataclass class C: x: int y: int = field(repr=False) z: int = field(repr=False, default=10) t: int = 20
pydocs
pep-0557.rst:345
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
modern
dataclasses/modern_05.py
@dataclass class C: a: float b: float c: float = field(init=False) def __post_init__(self): self.c = self.a + self.b
pydocs
pep-0557.rst:386
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
modern
dataclasses/modern_06.py
@dataclass class C: i: int j: int = None database: InitVar[DatabaseType] = None def __post_init__(self, database): if self.j is None and database is not None: self.j = database.lookup('j') c = C(10, database=my_database)
pydocs
pep-0557.rst:426
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
modern
dataclasses/modern_07.py
@dataclass class Base: x: Any = 15.0 y: int = 0 @dataclass class C(Base): z: int = 10 x: int = 15
pydocs
pep-0557.rst:467
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
modern
dataclasses/modern_08.py
@dataclass class D: x: List = [] def add(self, element): self.x += element
pydocs
pep-0557.rst:522
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
modern
dataclasses/modern_09.py
@dataclass class D: x: list = field(default_factory=list) assert D().x is not D().x
pydocs
pep-0557.rst:553
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
modern
dataclasses/modern_10.py
@dataclass class Point: x: int y: int @dataclass class C: l: List[Point] p = Point(10, 20) assert asdict(p) == {'x': 10, 'y': 20} c = C([Point(0, 0), Point(10, 4)]) assert asdict(c) == {'l': [{'x': 0, 'y': 0}, {'x': 10, 'y': 4}]}
pydocs
pep-0557.rst:574
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
modern
dataclasses/modern_11.py
C = make_dataclass('C', [('x', int), 'y', ('z', int, field(default=5))], namespace={'add_one': lambda self: self.x + 1})
pydocs
pep-0557.rst:616
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
modern
dataclasses/modern_12.py
@dataclass class C: x: int y: 'typing.Any' z: int = 5 def add_one(self): return self.x + 1
pydocs
pep-0557.rst:624
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
legacy
dataclasses/legacy_02.py
Time = namedtuple('Time', ['hour', 'minute']) def get_time(): return Time(12, 0)
pydocs
pep-0557.rst:715
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
modern
dataclasses/modern_13.py
@dataclass class C: x: int @classmethod def from_file(cls, filename): with open(filename) as fl: file_value = int(fl.read()) return C(file_value) c = C.from_file('file.txt')
pydocs
pep-0557.rst:767
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
modern
dataclasses/modern_14.py
@dataclass class Square: length: float area: float = field(init=False, default=0.0) def __post_init__(self): self.area = self.length * self.length s1 = Square(1.0) s2 = replace(s1, length=2.0)
pydocs
pep-0557.rst:836
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
modern
dataclasses/modern_15.py
@dataclass(init=False) class ArgHolder: args: List[Any] kwargs: Mapping[Any, Any] def __init__(self, *args, **kwargs): self.args = args self.kwargs = kwargs a = ArgHolder(1, 2, three=3)
pydocs
pep-0557.rst:872
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
legacy
dataclasses/legacy_03.py
class Application: def __init__(self, name, requirements, constraints=None, path='', executable_links=None, executables_dir=()): self.name = name self.requirements = requirements self.constraints = {} if constraints is None else constraints self.path = path self.executable_li...
pydocs
pep-0557.rst:888
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.7
dataclasses
557
3.7
modern
dataclasses/modern_16.py
@dataclass class Application: name: str requirements: List[Requirement] constraints: Dict[str, str] = field(default_factory=dict) path: str = '' executable_links: List[str] = field(default_factory=list) executable_dir: Tuple[str] = () additional_items: List[str] = field(init=False, default_f...
pydocs
pep-0557.rst:903
7ef0449603189a7d7f18026972afe733d99b996d
CC0-1.0
train
3.8
type-hints
484
3.5
modern
type-hints/modern_01.py
def greeting(name: str) -> str: return 'Hello ' + name
pydocs
pep-0484.txt:37
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
type-hints
484
3.5
modern
type-hints/modern_09.py
from typing import TypeVar, Text AnyStr = TypeVar('AnyStr', Text, bytes) def concat(x: AnyStr, y: AnyStr) -> AnyStr: return x + y
pydocs
pep-0484.txt:315
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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 elem)
pydocs
pep-0484.txt:344
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
type-hints
484
3.5
modern
type-hints/modern_11.py
from typing import TypeVar, Generic from logging import Logger T = TypeVar('T') class LoggedVar(Generic[T]): def __init__(self, value: T, name: str, logger: Logger) -> None: self.name = name self.logger = logger self.value = value def set(self, new: T) -> None: self.log('Set '...
pydocs
pep-0484.txt:357
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
type-hints
484
3.5
modern
type-hints/modern_12.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:386
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
type-hints
484
3.5
modern
type-hints/modern_13.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 also OK, now T is str
pydocs
pep-0484.txt:469
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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.txt:483
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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.txt:499
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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.txt:511
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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.txt:530
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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.txt:581
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:598
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
type-hints
484
3.5
legacy
type-hints/legacy_02.py
data = collections.defaultdict() # type: DefaultDict[int, bytes]
pydocs
pep-0484.txt:643
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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.txt:662
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
type-hints
484
3.5
modern
type-hints/modern_20.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 typ...
pydocs
pep-0484.txt:728
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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.txt:796
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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.txt:831
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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.txt:845
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:883
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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.txt:890
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
type-hints
484
3.5
modern
type-hints/modern_26.py
class Tree: ... def leaves(self) -> List['Tree']: ...
pydocs
pep-0484.txt:909
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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.txt:920
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:940
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:962
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
type-hints
484
3.5
modern
type-hints/modern_30.py
def handle_employee(e: Union[Employee, None]) -> None: ...
pydocs
pep-0484.txt:978
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:983
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
type-hints
484
3.5
modern
type-hints/modern_32.py
def handle_employee(e: Employee = None): ...
pydocs
pep-0484.txt:990
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
type-hints
484
3.5
modern
type-hints/modern_33.py
def handle_employee(e: Optional[Employee] = None) -> None: ...
pydocs
pep-0484.txt:994
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1006
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1020
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1044
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1076
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1086
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1107
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1126
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1137
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1167
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
type-hints
484
3.5
modern
type-hints/modern_41.py
def new_user(user_class: type) -> User: ...
pydocs
pep-0484.txt:1175
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1181
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1199
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1246
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1262
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1274
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1319
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
type-hints
484
3.5
modern
type-hints/modern_48.py
def foo(*args: str, **kwds: int): ...
pydocs
pep-0484.txt:1341
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
type-hints
484
3.5
modern
type-hints/modern_49.py
def foo(x: AnyStr, y: AnyStr = ...) -> AnyStr: ...
pydocs
pep-0484.txt:1357
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1375
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1389
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1399
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1411
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1422
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1466
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0
train
3.8
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:1478
3c69ec5e81bc7dc5a6a2a90b6ffb6a2d7b626c96
CC0-1.0