Skip to content

Commit 9f157d9

Browse files
authored
Fix synced itertools (python#20700)
Fixes python#20697 Changed the base type of `itertools.batched` from `Iterator[tuple[_T_co, ...]]` to `Iterator[_T_co]` in the stub and patch files so that mypy no longer reports an error due to a mismatched return type of `__next__`.
1 parent 84e8f1f commit 9f157d9

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

misc/typeshed_patches/0001-Revert-Remove-redundant-inheritances-from-Iterator.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ index 8a924ad8b..5c2bf7f83 100644
289289
if sys.version_info >= (3, 12):
290290
@disjoint_base
291291
- class batched(Generic[_T_co]):
292-
+ class batched(Iterator[tuple[_T_co, ...]], Generic[_T_co]):
292+
+ class batched(Iterator[_T_co], Generic[_T_co]):
293293
if sys.version_info >= (3, 13):
294294
@overload
295295
def __new__(cls, iterable: Iterable[_T], n: Literal[1], *, strict: Literal[True]) -> batched[tuple[_T]]: ...

mypy/typeshed/stdlib/itertools.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ if sys.version_info >= (3, 10):
341341

342342
if sys.version_info >= (3, 12):
343343
@disjoint_base
344-
class batched(Iterator[tuple[_T_co, ...]], Generic[_T_co]):
344+
class batched(Iterator[_T_co], Generic[_T_co]):
345345
if sys.version_info >= (3, 13):
346346
@overload
347347
def __new__(cls, iterable: Iterable[_T], n: Literal[1], *, strict: Literal[True]) -> batched[tuple[_T]]: ...
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[case testItertoolsBatched312]
2+
# flags: --python-version 3.12
3+
4+
from itertools import batched
5+
6+
b = batched([0], 1)
7+
reveal_type(b) # N: Revealed type is "itertools.batched[builtins.tuple[builtins.int, ...]]"
8+
reveal_type(b.__next__()) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
9+
10+
reveal_type(batched([0, 0], 2)) # N: Revealed type is "itertools.batched[builtins.tuple[builtins.int, ...]]"
11+
reveal_type(batched([0, 0, 0], 3)) # N: Revealed type is "itertools.batched[builtins.tuple[builtins.int, ...]]"
12+
reveal_type(batched([0, 0, 0, 0], 4)) # N: Revealed type is "itertools.batched[builtins.tuple[builtins.int, ...]]"
13+
reveal_type(batched([0, 0, 0, 0, 0], 5)) # N: Revealed type is "itertools.batched[builtins.tuple[builtins.int, ...]]"
14+
15+
reveal_type(batched([0], 2)) # N: Revealed type is "itertools.batched[builtins.tuple[builtins.int, ...]]"
16+
reveal_type(batched([0], 2)) # N: Revealed type is "itertools.batched[builtins.tuple[builtins.int, ...]]"
17+
18+
def f() -> int:
19+
return 3
20+
21+
reveal_type(batched([0, 0, 0], f())) # N: Revealed type is "itertools.batched[builtins.tuple[builtins.int, ...]]"
22+
23+
[builtins fixtures/tuple.pyi]
24+
25+
[case testItertoolsBatched313]
26+
# flags: --python-version 3.13
27+
28+
from itertools import batched
29+
30+
b = batched([0], 1, strict=True)
31+
reveal_type(b) # N: Revealed type is "itertools.batched[tuple[builtins.int]]"
32+
reveal_type(b.__next__()) # N: Revealed type is "tuple[builtins.int]"
33+
34+
reveal_type(batched([0, 0], 2, strict=True)) # N: Revealed type is "itertools.batched[tuple[builtins.int, builtins.int]]"
35+
reveal_type(batched([0, 0, 0], 3, strict=True)) # N: Revealed type is "itertools.batched[tuple[builtins.int, builtins.int, builtins.int]]"
36+
reveal_type(batched([0, 0, 0, 0], 4, strict=True)) # N: Revealed type is "itertools.batched[tuple[builtins.int, builtins.int, builtins.int, builtins.int]]"
37+
reveal_type(batched([0, 0, 0, 0, 0], 5, strict=True)) # N: Revealed type is "itertools.batched[tuple[builtins.int, builtins.int, builtins.int, builtins.int, builtins.int]]"
38+
39+
reveal_type(batched([0], 2),) # N: Revealed type is "itertools.batched[builtins.tuple[builtins.int, ...]]"
40+
reveal_type(batched([0], 2, strict=False)) # N: Revealed type is "itertools.batched[builtins.tuple[builtins.int, ...]]"
41+
42+
def f() -> int:
43+
return 3
44+
45+
reveal_type(batched([0, 0, 0], f(), strict=True)) # N: Revealed type is "itertools.batched[builtins.tuple[builtins.int, ...]]"
46+
47+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)