Skip to content

Commit f89cff3

Browse files
authored
Fixing product and combinations (#5213)
1 parent ff91f56 commit f89cff3

File tree

2 files changed

+63
-50
lines changed

2 files changed

+63
-50
lines changed

stdlib/itertools.pyi

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from _typeshed import _T_co
23
from typing import (
34
Any,
45
Callable,
@@ -122,60 +123,74 @@ _T3 = TypeVar("_T3")
122123
_T4 = TypeVar("_T4")
123124
_T5 = TypeVar("_T5")
124125
_T6 = TypeVar("_T6")
125-
@overload
126-
def product(__iter1: Iterable[_T1]) -> Iterator[Tuple[_T1]]: ...
127-
@overload
128-
def product(__iter1: Iterable[_T1], __iter2: Iterable[_T2]) -> Iterator[Tuple[_T1, _T2]]: ...
129-
@overload
130-
def product(__iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3]) -> Iterator[Tuple[_T1, _T2, _T3]]: ...
131-
@overload
132-
def product(
133-
__iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3], __iter4: Iterable[_T4]
134-
) -> Iterator[Tuple[_T1, _T2, _T3, _T4]]: ...
135-
@overload
136-
def product(
137-
__iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3], __iter4: Iterable[_T4], __iter5: Iterable[_T5]
138-
) -> Iterator[Tuple[_T1, _T2, _T3, _T4, _T5]]: ...
139-
@overload
140-
def product(
141-
__iter1: Iterable[_T1],
142-
__iter2: Iterable[_T2],
143-
__iter3: Iterable[_T3],
144-
__iter4: Iterable[_T4],
145-
__iter5: Iterable[_T5],
146-
__iter6: Iterable[_T6],
147-
) -> Iterator[Tuple[_T1, _T2, _T3, _T4, _T5, _T6]]: ...
148-
@overload
149-
def product(
150-
__iter1: Iterable[Any],
151-
__iter2: Iterable[Any],
152-
__iter3: Iterable[Any],
153-
__iter4: Iterable[Any],
154-
__iter5: Iterable[Any],
155-
__iter6: Iterable[Any],
156-
__iter7: Iterable[Any],
157-
*iterables: Iterable[Any],
158-
) -> Iterator[Tuple[Any, ...]]: ...
159-
@overload
160-
def product(*iterables: Iterable[_T1], repeat: int) -> Iterator[Tuple[_T1, ...]]: ...
161-
@overload
162-
def product(*iterables: Iterable[Any], repeat: int = ...) -> Iterator[Tuple[Any, ...]]: ...
126+
127+
class product(Iterator[_T_co], Generic[_T_co]):
128+
@overload
129+
def __new__(cls, __iter1: Iterable[_T1]) -> product[Tuple[_T1]]: ...
130+
@overload
131+
def __new__(cls, __iter1: Iterable[_T1], __iter2: Iterable[_T2]) -> product[Tuple[_T1, _T2]]: ...
132+
@overload
133+
def __new__(cls, __iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3]) -> product[Tuple[_T1, _T2, _T3]]: ...
134+
@overload
135+
def __new__(
136+
cls, __iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3], __iter4: Iterable[_T4]
137+
) -> product[Tuple[_T1, _T2, _T3, _T4]]: ...
138+
@overload
139+
def __new__(
140+
cls,
141+
__iter1: Iterable[_T1],
142+
__iter2: Iterable[_T2],
143+
__iter3: Iterable[_T3],
144+
__iter4: Iterable[_T4],
145+
__iter5: Iterable[_T5],
146+
) -> product[Tuple[_T1, _T2, _T3, _T4, _T5]]: ...
147+
@overload
148+
def __new__(
149+
cls,
150+
__iter1: Iterable[_T1],
151+
__iter2: Iterable[_T2],
152+
__iter3: Iterable[_T3],
153+
__iter4: Iterable[_T4],
154+
__iter5: Iterable[_T5],
155+
__iter6: Iterable[_T6],
156+
) -> product[Tuple[_T1, _T2, _T3, _T4, _T5, _T6]]: ...
157+
@overload
158+
def __new__(
159+
cls,
160+
__iter1: Iterable[Any],
161+
__iter2: Iterable[Any],
162+
__iter3: Iterable[Any],
163+
__iter4: Iterable[Any],
164+
__iter5: Iterable[Any],
165+
__iter6: Iterable[Any],
166+
__iter7: Iterable[Any],
167+
*iterables: Iterable[Any],
168+
) -> product[Tuple[Any, ...]]: ...
169+
@overload
170+
def __new__(cls, *iterables: Iterable[_T1], repeat: int) -> product[Tuple[_T1, ...]]: ...
171+
@overload
172+
def __new__(cls, *iterables: Iterable[Any], repeat: int = ...) -> product[Tuple[Any, ...]]: ...
173+
def __iter__(self) -> Iterator[_T_co]: ...
174+
def __next__(self) -> _T_co: ...
163175

164176
class permutations(Iterator[Tuple[_T, ...]], Generic[_T]):
165177
def __init__(self, iterable: Iterable[_T], r: Optional[int] = ...) -> None: ...
166178
def __iter__(self) -> Iterator[Tuple[_T, ...]]: ...
167179
def __next__(self) -> Tuple[_T, ...]: ...
168180

169-
@overload
170-
def combinations(iterable: Iterable[_T], r: Literal[2]) -> Iterator[Tuple[_T, _T]]: ...
171-
@overload
172-
def combinations(iterable: Iterable[_T], r: Literal[3]) -> Iterator[Tuple[_T, _T, _T]]: ...
173-
@overload
174-
def combinations(iterable: Iterable[_T], r: Literal[4]) -> Iterator[Tuple[_T, _T, _T, _T]]: ...
175-
@overload
176-
def combinations(iterable: Iterable[_T], r: Literal[5]) -> Iterator[Tuple[_T, _T, _T, _T, _T]]: ...
177-
@overload
178-
def combinations(iterable: Iterable[_T], r: int) -> Iterator[Tuple[_T, ...]]: ...
181+
class combinations(Iterator[_T_co], Generic[_T_co]):
182+
@overload
183+
def __new__(cls, iterable: Iterable[_T], r: Literal[2]) -> combinations[Tuple[_T, _T]]: ...
184+
@overload
185+
def __new__(cls, iterable: Iterable[_T], r: Literal[3]) -> combinations[Tuple[_T, _T, _T]]: ...
186+
@overload
187+
def __new__(cls, iterable: Iterable[_T], r: Literal[4]) -> combinations[Tuple[_T, _T, _T, _T]]: ...
188+
@overload
189+
def __new__(cls, iterable: Iterable[_T], r: Literal[5]) -> combinations[Tuple[_T, _T, _T, _T, _T]]: ...
190+
@overload
191+
def __new__(cls, iterable: Iterable[_T], r: int) -> combinations[Tuple[_T, ...]]: ...
192+
def __iter__(self) -> Iterator[_T_co]: ...
193+
def __next__(self) -> _T_co: ...
179194

180195
class combinations_with_replacement(Iterator[Tuple[_T, ...]], Generic[_T]):
181196
def __init__(self, iterable: Iterable[_T], r: int) -> None: ...

tests/stubtest_whitelists/py3_common.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,6 @@ ipaddress._BaseAddress.is_unspecified
184184
ipaddress._BaseAddress.max_prefixlen
185185
ipaddress._BaseAddress.packed
186186
ipaddress._BaseNetwork.max_prefixlen
187-
itertools.combinations # not a function at runtime, cannot represent with __init__ or __new__
188-
itertools.product # not a function at runtime, cannot represent with __init__ or __new__
189187
lib2to3.pygram.pattern_symbols
190188
lib2to3.pygram.python_symbols
191189
lib2to3.pytree.Base.__new__

0 commit comments

Comments
 (0)