Skip to content

Commit 7ebc422

Browse files
authored
Merge branch 'master' into 585
2 parents cbb1ad3 + a24b765 commit 7ebc422

10 files changed

+309
-52
lines changed

.flake8

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
# F405 defined from star imports
1919

2020
# Rules that we'd like to enable in the future:
21-
# Y026 Use typing_extensions.TypeAlias for type aliases (blocked by #4913)
2221
# Y027 Disallow importing typing.ContextManager, typing.OrderedDict &
2322
# typing_extensions.OrderedDict (cannot be globally enabled while typeshed
2423
# still contains stubs supporting Python 2).
@@ -32,12 +31,12 @@
3231
[flake8]
3332
per-file-ignores =
3433
*.py: E203, E501
35-
*.pyi: E301, E302, E305, E501, E701, E741, F401, F403, F405, F822, Y026, Y037
34+
*.pyi: E301, E302, E305, E501, E701, E741, F401, F403, F405, F822, Y037
3635
# Since typing.pyi defines "overload" this is not recognized by flake8 as typing.overload.
3736
# Unfortunately, flake8 does not allow to "noqa" just a specific error inside the file itself.
3837
# https://github.com/PyCQA/flake8/issues/1079
3938
# F811 redefinition of unused '...'
40-
stubs/*.pyi: E301, E302, E305, E501, E701, E741, F401, F403, F405, F822, Y026, Y027, Y037
41-
stdlib/typing.pyi: E301, E302, E305, E501, E701, E741, F401, F403, F405, F811, F822, Y026, Y034, Y037
39+
stubs/*.pyi: E301, E302, E305, E501, E701, E741, F401, F403, F405, F822, Y027, Y037
40+
stdlib/typing.pyi: E301, E302, E305, E501, E701, E741, F401, F403, F405, F811, F822, Y034, Y037
4241

4342
exclude = .venv*,.git,*_pb2.pyi,stdlib/@python2/*

requirements-tests.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pytype==2022.03.29; platform_system != "Windows" and python_version < "3.10"
44
black==22.3.0
55
flake8==4.0.1
66
flake8-bugbear==21.11.29
7-
flake8-pyi==22.3.0
7+
flake8-pyi==22.4.0
88
# must match .pre-commit-config.yaml
99
isort==5.10.1
1010
tomli==1.2.2

stdlib/_ast.pyi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ class Try(stmt):
172172
orelse: list[stmt]
173173
finalbody: list[stmt]
174174

175+
if sys.version_info >= (3, 11):
176+
class TryStar(stmt):
177+
__match_args__ = ("body", "handlers", "orelse", "finalbody")
178+
body: list[stmt]
179+
handlers: list[ExceptHandler]
180+
orelse: list[stmt]
181+
finalbody: list[stmt]
182+
175183
class Assert(stmt):
176184
if sys.version_info >= (3, 10):
177185
__match_args__ = ("test", "msg")

stdlib/_weakref.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ class ReferenceType(Generic[_T]):
2929
ref = ReferenceType
3030

3131
def getweakrefcount(__object: Any) -> int: ...
32-
def getweakrefs(object: Any) -> list[Any]: ...
33-
@overload
34-
def proxy(object: _C, callback: Callable[[_C], Any] | None = ...) -> CallableProxyType[_C]: ...
32+
def getweakrefs(__object: Any) -> list[Any]: ...
3533

3634
# Return CallableProxyType if object is callable, ProxyType otherwise
3735
@overload
38-
def proxy(object: _T, callback: Callable[[_T], Any] | None = ...) -> Any: ...
36+
def proxy(__object: _C, __callback: Callable[[_C], Any] | None = ...) -> CallableProxyType[_C]: ...
37+
@overload
38+
def proxy(__object: _T, __callback: Callable[[_T], Any] | None = ...) -> Any: ...

stdlib/builtins.pyi

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,29 @@ class int:
218218
if sys.version_info >= (3, 10):
219219
def bit_count(self) -> int: ...
220220

221-
def to_bytes(self, length: SupportsIndex, byteorder: Literal["little", "big"], *, signed: bool = ...) -> bytes: ...
222-
@classmethod
223-
def from_bytes(
224-
cls: type[Self],
225-
bytes: Iterable[SupportsIndex] | SupportsBytes | ReadableBuffer,
226-
byteorder: Literal["little", "big"],
227-
*,
228-
signed: bool = ...,
229-
) -> Self: ...
221+
if sys.version_info >= (3, 11):
222+
def to_bytes(
223+
self, length: SupportsIndex = ..., byteorder: Literal["little", "big"] = ..., *, signed: bool = ...
224+
) -> bytes: ...
225+
@classmethod
226+
def from_bytes(
227+
cls: type[Self],
228+
bytes: Iterable[SupportsIndex] | SupportsBytes | ReadableBuffer,
229+
byteorder: Literal["little", "big"] = ...,
230+
*,
231+
signed: bool = ...,
232+
) -> Self: ...
233+
else:
234+
def to_bytes(self, length: SupportsIndex, byteorder: Literal["little", "big"], *, signed: bool = ...) -> bytes: ...
235+
@classmethod
236+
def from_bytes(
237+
cls: type[Self],
238+
bytes: Iterable[SupportsIndex] | SupportsBytes | ReadableBuffer,
239+
byteorder: Literal["little", "big"],
240+
*,
241+
signed: bool = ...,
242+
) -> Self: ...
243+
230244
def __add__(self, __x: int) -> int: ...
231245
def __sub__(self, __x: int) -> int: ...
232246
def __mul__(self, __x: int) -> int: ...
@@ -1275,7 +1289,7 @@ def next(__i: SupportsNext[_T]) -> _T: ...
12751289
def next(__i: SupportsNext[_T], __default: _VT) -> _T | _VT: ...
12761290
def oct(__number: int | SupportsIndex) -> str: ...
12771291

1278-
_OpenFile = StrOrBytesPath | int
1292+
_OpenFile = StrOrBytesPath | int # noqa: Y026 # TODO: Use TypeAlias once mypy bugs are fixed
12791293
_Opener: TypeAlias = Callable[[str, int], int]
12801294

12811295
# Text mode: always returns a TextIOWrapper
@@ -1394,7 +1408,9 @@ class _SupportsPow3NoneOnly(Protocol[_E, _T_co]):
13941408
class _SupportsPow3(Protocol[_E, _M, _T_co]):
13951409
def __pow__(self, __other: _E, __modulo: _M) -> _T_co: ...
13961410

1397-
_SupportsSomeKindOfPow = _SupportsPow2[Any, Any] | _SupportsPow3NoneOnly[Any, Any] | _SupportsPow3[Any, Any, Any]
1411+
_SupportsSomeKindOfPow = ( # noqa: Y026 # TODO: Use TypeAlias once mypy bugs are fixed
1412+
_SupportsPow2[Any, Any] | _SupportsPow3NoneOnly[Any, Any] | _SupportsPow3[Any, Any, Any]
1413+
)
13981414

13991415
if sys.version_info >= (3, 8):
14001416
@overload
@@ -1783,6 +1799,7 @@ if sys.version_info >= (3, 11):
17831799
@overload
17841800
def split(self: Self, __condition: Callable[[_BaseExceptionT_co], bool]) -> tuple[Self | None, Self | None]: ...
17851801
def derive(self: Self, __excs: Sequence[_BaseExceptionT_co]) -> Self: ...
1802+
def __class_getitem__(cls, __item: Any) -> GenericAlias: ...
17861803

17871804
class ExceptionGroup(BaseExceptionGroup[_ExceptionT_co], Exception):
17881805
def __new__(cls: type[Self], __message: str, __exceptions: Sequence[_ExceptionT_co]) -> Self: ...

stdlib/dis.pyi

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,60 @@ __all__ = [
3939
_HaveCodeType: TypeAlias = types.MethodType | types.FunctionType | types.CodeType | type | Callable[..., Any]
4040
_HaveCodeOrStringType: TypeAlias = _HaveCodeType | str | bytes
4141

42-
class Instruction(NamedTuple):
43-
opname: str
44-
opcode: int
45-
arg: int | None
46-
argval: Any
47-
argrepr: str
48-
offset: int
49-
starts_line: int | None
50-
is_jump_target: bool
42+
if sys.version_info >= (3, 11):
43+
class Positions(NamedTuple):
44+
lineno: int | None = ...
45+
end_lineno: int | None = ...
46+
col_offset: int | None = ...
47+
end_col_offset: int | None = ...
48+
49+
if sys.version_info >= (3, 11):
50+
class Instruction(NamedTuple):
51+
opname: str
52+
opcode: int
53+
arg: int | None
54+
argval: Any
55+
argrepr: str
56+
offset: int
57+
starts_line: int | None
58+
is_jump_target: bool
59+
positions: Positions | None = ...
60+
61+
else:
62+
class Instruction(NamedTuple):
63+
opname: str
64+
opcode: int
65+
arg: int | None
66+
argval: Any
67+
argrepr: str
68+
offset: int
69+
starts_line: int | None
70+
is_jump_target: bool
5171

5272
class Bytecode:
5373
codeobj: types.CodeType
5474
first_line: int
55-
def __init__(self, x: _HaveCodeOrStringType, *, first_line: int | None = ..., current_offset: int | None = ...) -> None: ...
75+
if sys.version_info >= (3, 11):
76+
def __init__(
77+
self,
78+
x: _HaveCodeOrStringType,
79+
*,
80+
first_line: int | None = ...,
81+
current_offset: int | None = ...,
82+
show_caches: bool = ...,
83+
) -> None: ...
84+
@classmethod
85+
def from_traceback(cls: type[Self], tb: types.TracebackType, *, show_caches: bool = ...) -> Self: ...
86+
else:
87+
def __init__(
88+
self, x: _HaveCodeOrStringType, *, first_line: int | None = ..., current_offset: int | None = ...
89+
) -> None: ...
90+
@classmethod
91+
def from_traceback(cls: type[Self], tb: types.TracebackType) -> Self: ...
92+
5693
def __iter__(self) -> Iterator[Instruction]: ...
5794
def info(self) -> str: ...
5895
def dis(self) -> str: ...
59-
@classmethod
60-
def from_traceback(cls: type[Self], tb: types.TracebackType) -> Self: ...
6196

6297
COMPILER_FLAG_NAMES: dict[int, str]
6398

@@ -66,14 +101,27 @@ def findlinestarts(code: _HaveCodeType) -> Iterator[tuple[int, int]]: ...
66101
def pretty_flags(flags: int) -> str: ...
67102
def code_info(x: _HaveCodeOrStringType) -> str: ...
68103

69-
if sys.version_info >= (3, 7):
104+
if sys.version_info >= (3, 11):
105+
def dis(
106+
x: _HaveCodeOrStringType | None = ..., *, file: IO[str] | None = ..., depth: int | None = ..., show_caches: bool = ...
107+
) -> None: ...
108+
109+
elif sys.version_info >= (3, 7):
70110
def dis(x: _HaveCodeOrStringType | None = ..., *, file: IO[str] | None = ..., depth: int | None = ...) -> None: ...
71111

72112
else:
73113
def dis(x: _HaveCodeOrStringType | None = ..., *, file: IO[str] | None = ...) -> None: ...
74114

75-
def distb(tb: types.TracebackType | None = ..., *, file: IO[str] | None = ...) -> None: ...
76-
def disassemble(co: _HaveCodeType, lasti: int = ..., *, file: IO[str] | None = ...) -> None: ...
77-
def disco(co: _HaveCodeType, lasti: int = ..., *, file: IO[str] | None = ...) -> None: ...
115+
if sys.version_info >= (3, 11):
116+
def disassemble(co: _HaveCodeType, lasti: int = ..., *, file: IO[str] | None = ..., show_caches: bool = ...) -> None: ...
117+
def disco(co: _HaveCodeType, lasti: int = ..., *, file: IO[str] | None = ..., show_caches: bool = ...) -> None: ...
118+
def distb(tb: types.TracebackType | None = ..., *, file: IO[str] | None = ..., show_caches: bool = ...) -> None: ...
119+
def get_instructions(x: _HaveCodeType, *, first_line: int | None = ..., show_caches: bool = ...) -> Iterator[Instruction]: ...
120+
121+
else:
122+
def disassemble(co: _HaveCodeType, lasti: int = ..., *, file: IO[str] | None = ...) -> None: ...
123+
def disco(co: _HaveCodeType, lasti: int = ..., *, file: IO[str] | None = ...) -> None: ...
124+
def distb(tb: types.TracebackType | None = ..., *, file: IO[str] | None = ...) -> None: ...
125+
def get_instructions(x: _HaveCodeType, *, first_line: int | None = ...) -> Iterator[Instruction]: ...
126+
78127
def show_code(co: _HaveCodeType, *, file: IO[str] | None = ...) -> None: ...
79-
def get_instructions(x: _HaveCodeType, *, first_line: int | None = ...) -> Iterator[Instruction]: ...

stdlib/itertools.pyi

Lines changed: 87 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from _typeshed import Self, _T_co
2+
from _typeshed import Self
33
from collections.abc import Callable, Iterable, Iterator
44
from typing import Any, Generic, SupportsComplex, SupportsFloat, SupportsInt, TypeVar, overload
55
from typing_extensions import Literal, SupportsIndex, TypeAlias
@@ -10,6 +10,14 @@ if sys.version_info >= (3, 9):
1010
_T = TypeVar("_T")
1111
_S = TypeVar("_S")
1212
_N = TypeVar("_N", int, float, SupportsFloat, SupportsInt, SupportsIndex, SupportsComplex)
13+
_T_co = TypeVar("_T_co", covariant=True)
14+
_T1 = TypeVar("_T1")
15+
_T2 = TypeVar("_T2")
16+
_T3 = TypeVar("_T3")
17+
_T4 = TypeVar("_T4")
18+
_T5 = TypeVar("_T5")
19+
_T6 = TypeVar("_T6")
20+
1321
_Step: TypeAlias = int | float | SupportsFloat | SupportsInt | SupportsIndex | SupportsComplex
1422

1523
Predicate: TypeAlias = Callable[[_T], object]
@@ -77,9 +85,6 @@ class filterfalse(Iterator[_T], Generic[_T]):
7785
def __iter__(self: Self) -> Self: ...
7886
def __next__(self) -> _T: ...
7987

80-
_T1 = TypeVar("_T1")
81-
_T2 = TypeVar("_T2")
82-
8388
class groupby(Iterator[tuple[_T, Iterator[_S]]], Generic[_T, _S]):
8489
@overload
8590
def __new__(cls, iterable: Iterable[_T1], key: None = ...) -> groupby[_T1, _T1]: ...
@@ -108,15 +113,85 @@ class takewhile(Iterator[_T], Generic[_T]):
108113

109114
def tee(__iterable: Iterable[_T], __n: int = ...) -> tuple[Iterator[_T], ...]: ...
110115

111-
class zip_longest(Iterator[Any]):
112-
def __init__(self, *p: Iterable[Any], fillvalue: Any = ...) -> None: ...
116+
class zip_longest(Iterator[_T_co], Generic[_T_co]):
117+
# one iterable (fillvalue doesn't matter)
118+
@overload
119+
def __new__(cls, __iter1: Iterable[_T1], *, fillvalue: object = ...) -> zip_longest[tuple[_T1]]: ...
120+
# two iterables
121+
@overload
122+
# In the overloads without fillvalue, all of the tuple members could theoretically be None,
123+
# but we return Any instead to avoid false positives for code where we know one of the iterables
124+
# is longer.
125+
def __new__(cls, __iter1: Iterable[_T1], __iter2: Iterable[_T2]) -> zip_longest[tuple[_T1 | Any, _T2 | Any]]: ...
126+
@overload
127+
def __new__(
128+
cls, __iter1: Iterable[_T1], __iter2: Iterable[_T2], *, fillvalue: _T
129+
) -> zip_longest[tuple[_T1 | _T, _T2 | _T]]: ...
130+
# three iterables
131+
@overload
132+
def __new__(
133+
cls, __iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3]
134+
) -> zip_longest[tuple[_T1 | Any, _T2 | Any, _T3 | Any]]: ...
135+
@overload
136+
def __new__(
137+
cls, __iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3], *, fillvalue: _T
138+
) -> zip_longest[tuple[_T1 | _T, _T2 | _T, _T3 | _T]]: ...
139+
# four iterables
140+
@overload
141+
def __new__(
142+
cls, __iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3], __iter4: Iterable[_T4]
143+
) -> zip_longest[tuple[_T1 | Any, _T2 | Any, _T3 | Any, _T4 | Any]]: ...
144+
@overload
145+
def __new__(
146+
cls, __iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3], __iter4: Iterable[_T4], *, fillvalue: _T
147+
) -> zip_longest[tuple[_T1 | _T, _T2 | _T, _T3 | _T, _T4 | _T]]: ...
148+
# five iterables
149+
@overload
150+
def __new__(
151+
cls,
152+
__iter1: Iterable[_T1],
153+
__iter2: Iterable[_T2],
154+
__iter3: Iterable[_T3],
155+
__iter4: Iterable[_T4],
156+
__iter5: Iterable[_T5],
157+
) -> zip_longest[tuple[_T1 | Any, _T2 | Any, _T3 | Any, _T4 | Any, _T5 | Any]]: ...
158+
@overload
159+
def __new__(
160+
cls,
161+
__iter1: Iterable[_T1],
162+
__iter2: Iterable[_T2],
163+
__iter3: Iterable[_T3],
164+
__iter4: Iterable[_T4],
165+
__iter5: Iterable[_T5],
166+
*,
167+
fillvalue: _T,
168+
) -> zip_longest[tuple[_T1 | _T, _T2 | _T, _T3 | _T, _T4 | _T, _T5 | _T]]: ...
169+
# six or more iterables
170+
@overload
171+
def __new__(
172+
cls,
173+
__iter1: Iterable[_T],
174+
__iter2: Iterable[_T],
175+
__iter3: Iterable[_T],
176+
__iter4: Iterable[_T],
177+
__iter5: Iterable[_T],
178+
__iter6: Iterable[_T],
179+
*iterables: Iterable[_T],
180+
) -> zip_longest[tuple[_T | Any, ...]]: ...
181+
@overload
182+
def __new__(
183+
cls,
184+
__iter1: Iterable[_T],
185+
__iter2: Iterable[_T],
186+
__iter3: Iterable[_T],
187+
__iter4: Iterable[_T],
188+
__iter5: Iterable[_T],
189+
__iter6: Iterable[_T],
190+
*iterables: Iterable[_T],
191+
fillvalue: _T,
192+
) -> zip_longest[tuple[_T, ...]]: ...
113193
def __iter__(self: Self) -> Self: ...
114-
def __next__(self) -> Any: ...
115-
116-
_T3 = TypeVar("_T3")
117-
_T4 = TypeVar("_T4")
118-
_T5 = TypeVar("_T5")
119-
_T6 = TypeVar("_T6")
194+
def __next__(self) -> _T_co: ...
120195

121196
class product(Iterator[_T_co], Generic[_T_co]):
122197
@overload

stdlib/random.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ class Random(_random.Random):
8989
cum_weights: Sequence[float | Fraction] | None = ...,
9090
k: int = ...,
9191
) -> list[_T]: ...
92-
def shuffle(self, x: MutableSequence[Any], random: Callable[[], float] | None = ...) -> None: ...
92+
if sys.version_info >= (3, 11):
93+
def shuffle(self, x: MutableSequence[Any]) -> None: ...
94+
else:
95+
def shuffle(self, x: MutableSequence[Any], random: Callable[[], float] | None = ...) -> None: ...
9396
if sys.version_info >= (3, 11):
9497
def sample(self, population: Sequence[_T], k: int, *, counts: Iterable[int] | None = ...) -> list[_T]: ...
9598
elif sys.version_info >= (3, 9):

0 commit comments

Comments
 (0)