cut
stringclasses
1 value
version
stringclasses
10 values
idiom_id
stringclasses
53 values
pep
stringclasses
53 values
introduced_in
stringclasses
11 values
form
stringclasses
2 values
file
stringclasses
887 values
code
stringclasses
916 values
source
stringclasses
1 value
origin
stringlengths
15
17
origin_revision
stringclasses
10 values
license
stringclasses
1 value
train
3.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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.15
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
train
3.15
protocol
544
3.8
modern
protocol/modern_05.py
class RGB(Protocol): rgb: Tuple[int, int, int] @abstractmethod def intensity(self) -> int: return 0 class Point(RGB): def __init__(self, red: int, green: int, blue: str) -> None: self.rgb = red, green, blue # Error, 'blue' must be 'int' # Type checker might warn that 'intensity' ...
pydocs
pep-0544.rst:436
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_06.py
from typing import Sized, Protocol class SizedAndClosable(Sized, Protocol): def close(self) -> None: ...
pydocs
pep-0544.rst:473
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_07.py
from typing import Sized class SupportsClose(Protocol): def close(self) -> None: ... class SizedAndClosable(Sized, SupportsClose, Protocol): pass
pydocs
pep-0544.rst:486
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_08.py
class Iterable(Protocol[T]): @abstractmethod def __iter__(self) -> Iterator[T]: ...
pydocs
pep-0544.rst:516
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_09.py
T = TypeVar('T') T_co = TypeVar('T_co', covariant=True) T_contra = TypeVar('T_contra', contravariant=True) class Box(Protocol[T_co]): def content(self) -> T_co: ... box: Box[float] second_box: Box[int] box = second_box # This is OK due to the covariance of 'Box'. class Sender(Protocol[T_contra]): de...
pydocs
pep-0544.rst:528
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_10.py
T = TypeVar('T') class AnotherBox(Protocol[T]): # Error, this protocol is covariant in T, def content(self) -> T: # not invariant. ...
pydocs
pep-0544.rst:559
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_11.py
class Traversable(Protocol): def leaves(self) -> Iterable['Traversable']: ...
pydocs
pep-0544.rst:574
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_12.py
C = TypeVar('C', bound='Copyable') class Copyable(Protocol): def copy(self: C) -> C: ... class One: def copy(self) -> 'One': ... T = TypeVar('T', bound='Other') class Other: def copy(self: T) -> T: ... c: Copyable c = One() # OK c = Other() # Also OK
pydocs
pep-0544.rst:605
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_13.py
from typing import Optional, List, Protocol class Combiner(Protocol): def __call__(self, *vals: bytes, maxlen: Optional[int] = None) -> List[bytes]: ... def good_cb(*vals: bytes, maxlen: Optional[int] = None) -> List[bytes]: ... def bad_cb(*vals: bytes, maxitems: Optional[int]) -> List[bytes]...
pydocs
pep-0544.rst:631
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_14.py
# file lib.py from typing import Sized T = TypeVar('T', contravariant=True) class ListLike(Sized, Protocol[T]): def append(self, x: T) -> None: pass def populate(lst: ListLike[int]) -> None: ... # file main.py from lib import populate # Note that ListLike is NOT imported class MockStack: def __...
pydocs
pep-0544.rst:677
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_15.py
from typing import Union, Optional, Protocol class Exitable(Protocol): def exit(self) -> int: ... class Quittable(Protocol): def quit(self) -> Optional[int]: ... def finish(task: Union[Exitable, Quittable]) -> int: ... class DefaultJob: ... def quit(self) -> int: return 0 f...
pydocs
pep-0544.rst:707
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_16.py
from typing import Iterable, Hashable class HashableFloats(Iterable[float], Hashable, Protocol): pass def cached_func(args: HashableFloats) -> float: ... cached_func((1, 2, 3)) # OK, tuple is both hashable and iterable
pydocs
pep-0544.rst:727
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_17.py
class Proto(Protocol): @abstractmethod def meth(self) -> int: ... class Concrete: def meth(self) -> int: return 42 def fun(cls: Type[Proto]) -> int: return cls().meth() # OK fun(Proto) # Error fun(Concrete) # OK
pydocs
pep-0544.rst:748
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_18.py
from typing import Any, Protocol class ProtoA(Protocol): def meth(self, x: int) -> int: ... class ProtoB(Protocol): def meth(self, obj: Any, x: int) -> int: ... class C: def meth(self, x: int) -> int: ... a: ProtoA = C # Type check error, signatures don't match! b: ProtoB = C # OK
pydocs
pep-0544.rst:777
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_19.py
from typing import NewType, Protocol, Iterator class Id(Protocol): code: int secrets: Iterator[bytes] UserId = NewType('UserId', Id) # Error, can't provide distinct type
pydocs
pep-0544.rst:798
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_20.py
from typing import TypeVar, Reversible, Iterable, Sized T = TypeVar('T') class SizedIterable(Iterable[T], Sized, Protocol): pass CompatReversible = Union[Reversible[T], SizedIterable[T]]
pydocs
pep-0544.rst:809
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_21.py
# file default_config.py timeout = 100 one_flag = True other_flag = False # file main.py import default_config from typing import Protocol class Options(Protocol): timeout: int one_flag: bool other_flag: bool def setup(options: Options) -> None: ... setup(default_config) # OK
pydocs
pep-0544.rst:824
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_22.py
# callbacks.py def on_error(x: int) -> None: ... def on_success() -> None: ... # main.py import callbacks from typing import Protocol class Reporter(Protocol): def on_error(self, x: int) -> None: ... def on_success(self) -> None: ... rp: Reporter = callbacks # Passes type check
pydocs
pep-0544.rst:846
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_23.py
from typing import runtime_checkable, Protocol @runtime_checkable class SupportsClose(Protocol): def close(self): ... assert isinstance(open('some/file'), SupportsClose)
pydocs
pep-0544.rst:884
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_24.py
class Foo(Protocol): @property def c(self) -> int: return 42 # Default value can be provided for property... @abstractproperty def d(self) -> int: # ... or it can be abstract return 0
pydocs
pep-0544.rst:943
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_25.py
from typing import Protocol class Base: attr: str class Proto(Base, Protocol): def meth(self) -> int: ... class C: attr: str def meth(self) -> int: return 0
pydocs
pep-0544.rst:1080
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_26.py
class Proto(Protocol): @abstractmethod def first(self) -> int: raise NotImplementedError def second(self) -> int: return self.first() + 1 def fun(arg: Proto) -> None: arg.second()
pydocs
pep-0544.rst:1152
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_27.py
class P(Protocol): x: int class C: def initialize(self) -> None: self.x = 0 c = C() isinstance(c, P) # False c.initialize() isinstance(c, P) # True def f(x: Union[P, int]) -> None: if isinstance(x, P): # Static type of x is P here. ... else: # Static type of x is int...
pydocs
pep-0544.rst:1239
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_28.py
class Proto(Proto1, Proto2, ..., Protocol): pass
pydocs
pep-0544.rst:1276
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_29.py
class P(Protocol): x: float def f(arg: P) -> None: arg.x = 0.42 class C: x: int c = C() f(c) # Would typecheck if covariant subtyping # of mutable attributes were allowed. c.x >> 1 # But this fails at runtime
pydocs
pep-0544.rst:1319
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_30.py
T = TypeVar('T') class P(Protocol[T]): # Protocol is declared as invariant. def meth(self) -> T: ... class C: def meth(self) -> float: ... class D(C): def meth(self) -> int: ...
pydocs
pep-0544.rst:1347
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
protocol
544
3.8
modern
protocol/modern_31.py
class OldIterable(Sized, Protocol[T]): def __getitem__(self, item: int) -> T: ... CompatIterable = Union[Iterable[T], OldIterable[T]] class A: def __iter__(self) -> Iterator[str]: ... class B: def __len__(self) -> int: ... def __getitem__(self, item: int) -> str: ... def iterate(it: CompatIterable[st...
pydocs
pep-0544.rst:1394
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
positional-only-params
570
3.8
modern
positional-only-params/modern_01.py
def name(positional_only_parameters, /, positional_or_keyword_parameters, *, keyword_only_parameters): ...
pydocs
pep-0570.rst:440
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
positional-only-params
570
3.8
modern
positional-only-params/modern_02.py
def name(p1, p2, /, p_or_kw, *, kw): ... def name(p1, p2=None, /, p_or_kw=None, *, kw): ... def name(p1, p2=None, /, *, kw): ... def name(p1, p2=None, /): ... def name(p1, p2, /, p_or_kw): ... def name(p1, p2, /): ...
pydocs
pep-0570.rst:458
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
positional-only-params
570
3.8
modern
positional-only-params/modern_03.py
def foo(name, /, **kwds): return 'name' in kwds
pydocs
pep-0570.rst:536
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
positional-only-params
570
3.8
modern
positional-only-params/modern_04.py
def standard_arg(arg): print(arg) def pos_only_arg(arg, /): print(arg) def kwd_only_arg(*, arg): print(arg) def combined_example(pos_only, /, standard, *, kwd_only): print(pos_only, standard, kwd_only)
pydocs
pep-0570.rst:632
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
positional-only-params
570
3.8
modern
positional-only-params/modern_05.py
def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2): ...
pydocs
pep-0570.rst:702
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
builtin-generics
585
3.9
modern
builtin-generics/modern_01.py
from __future__ import annotations def find(haystack: dict[str, list[int]]) -> int: ...
pydocs
pep-0585.rst:67
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
builtin-generics
585
3.9
modern
builtin-generics/modern_02.py
list[str] tuple[int, ...] ChainMap[str, list[str]]
pydocs
pep-0585.rst:143
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
builtin-generics
585
3.9
modern
builtin-generics/modern_03.py
l = list[str]() list is list[str] list == list[str] list[str] == list[str] list[str] == list[int] isinstance([1, 2, 3], list[str]) issubclass(list, list[str]) isinstance(list[str], types.GenericAlias)
pydocs
pep-0585.rst:168
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
builtin-generics
585
3.9
modern
builtin-generics/modern_04.py
l: list[str] = []
pydocs
pep-0585.rst:194
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
builtin-generics
585
3.9
modern
builtin-generics/modern_05.py
l = list[str]()
pydocs
pep-0585.rst:198
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
builtin-generics
585
3.9
modern
builtin-generics/modern_06.py
list[str] tuple[int, ...] collections.ChainMap[str, list[str]]
pydocs
pep-0585.rst:244
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
builtin-generics
585
3.9
legacy
builtin-generics/legacy_01.py
from typing import List, Tuple, ChainMap List[str] Tuple[int, ...] ChainMap[str, List[str]]
pydocs
pep-0585.rst:255
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
builtin-generics
585
3.9
modern
builtin-generics/modern_07.py
isinstance([1, 2, 3], list[str])
pydocs
pep-0585.rst:323
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0
train
3.15
literal-types
586
3.8
modern
literal-types/modern_01.py
from typing import Literal def accepts_only_four(x: Literal[4]) -> None: pass accepts_only_four(4) # OK accepts_only_four(19) # Rejected
pydocs
pep-0586.rst:25
e449c7e446faec3ecf953e83fbe677b771bf5d7c
CC0-1.0