Skip to content

Commit

Permalink
Teach pytype that zip is actually a class.
Browse files Browse the repository at this point in the history
This has been the case since Python 3.0.

PiperOrigin-RevId: 605122089
  • Loading branch information
Solumin authored and rchen152 committed Feb 8, 2024
1 parent b2351e1 commit 58cff8b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
44 changes: 30 additions & 14 deletions pytype/stubs/builtins/builtins.pytd
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,37 @@ def sum(iterable: Iterable[_T], start: _T) -> _T: ...
def vars(*args, **kwargs) -> dict[str, Any]: ...

if sys.version_info >= (3, 10):
def zip(*, strict: bool = ...) -> Iterator[nothing]: ...
def zip(seq1: Iterable[nothing], *, strict: bool = ...) -> Iterator[nothing]: ...
def zip(seq1: Iterable[_T], *, strict: bool = ...) -> Iterator[Tuple[_T]]: ...
def zip(seq1: Iterable[nothing], seq2: Iterable, *, strict: bool = ...) -> Iterator[nothing]: ...
def zip(seq1: Iterable, seq2: Iterable[nothing], *, strict: bool = ...) -> Iterator[nothing]: ...
def zip(seq1: Iterable[_T], seq2: Iterable[_T2], *, strict: bool = ...) -> Iterator[Tuple[_T, _T2]]: ...
def zip(seq1, seq2, seq3, *seqs: Iterable, strict: bool = ...) -> Iterator[tuple]: ...
class zip(Iterable[_S]):
@overload
def __new__(cls, *, strict: bool = ...) -> zip[nothing]: ...
@overload
def __new__(cls, seq: Iterable[nothing], *, strict: bool = ...) -> zip[nothing]: ...
@overload
def __new__(cls, seq: Iterable[_T], *, strict: bool = ...) -> zip[Tuple[_T]]: ...
@overload
def __new__(cls, seq1: Iterable[nothing], seq2: Iterable, *, strict: bool = ...) -> zip[nothing]: ...
@overload
def __new__(cls, seq1: Iterable, seq2: Iterable[nothing], *, strict: bool = ...) -> zip[nothing]: ...
@overload
def __new__(cls, seq1: Iterable[_T], seq2: Iterable[_T2], *, strict: bool = ...) -> zip[Tuple[_T, _T2]]: ...
@overload
def __new__(cls, seq1, seq2, seq3, *seqs: Iterable, strict: bool = ...) -> zip[tuple]: ...
else:
def zip() -> Iterator[nothing]: ...
def zip(seq1: Iterable[nothing]) -> Iterator[nothing]: ...
def zip(seq1: Iterable[_T]) -> Iterator[Tuple[_T]]: ...
def zip(seq1: Iterable[nothing], seq2: Iterable) -> Iterator[nothing]: ...
def zip(seq1: Iterable, seq2: Iterable[nothing]) -> Iterator[nothing]: ...
def zip(seq1: Iterable[_T], seq2: Iterable[_T2]) -> Iterator[Tuple[_T, _T2]]: ...
def zip(seq1, seq2, seq3, *seqs: Iterable) -> Iterator[tuple]: ...
class zip(Iterable[_S]):
@overload
def __new__(cls) -> zip[nothing]: ...
@overload
def __new__(cls, seq: Iterable[nothing]) -> zip[nothing]: ...
@overload
def __new__(cls, seq: Iterable[_T]) -> zip[Tuple[_T]]: ...
@overload
def __new__(cls, seq1: Iterable[nothing], seq2: Iterable) -> zip[nothing]: ...
@overload
def __new__(cls, seq1: Iterable, seq2: Iterable[nothing]) -> zip[nothing]: ...
@overload
def __new__(cls, seq1: Iterable[_T], seq2: Iterable[_T2]) -> zip[Tuple[_T, _T2]]: ...
@overload
def __new__(cls, seq1, seq2, seq3, *seqs: Iterable) -> zip[tuple]: ...

# Keyword constants cannot be assigned here; they are defined in the parser.
# False: bool = ...
Expand Down
4 changes: 2 additions & 2 deletions pytype/test_data/complex.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, Iterator
from typing import Any

v = ... # type: list
w = ... # type: list
x = ... # type: list
y = ... # type: list
z = ... # type: Iterator[tuple]
z = ... # type: zip[tuple]
12 changes: 6 additions & 6 deletions pytype/tests/test_builtins4.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,11 @@ def test_zip(self):
""")
self.assertTypesMatchPytd(ty, """
from typing import Iterator, Tuple, Union
a : Iterator[nothing]
b : Iterator[Tuple[Union[int, complex]]]
c : Iterator[nothing]
d : Iterator[nothing]
e : Iterator[Tuple[complex, int]]
a: zip[nothing]
b: zip[Tuple[Union[int, complex]]]
c: zip[nothing]
d: zip[nothing]
e: zip[Tuple[complex, int]]
""")

def test_dict(self):
Expand Down Expand Up @@ -615,7 +615,7 @@ def test_iterator_builtins(self):
self.assertTypesMatchPytd(ty, """
from typing import Iterator, Tuple
v1 : Iterator[int]
v2 : Iterator[Tuple[int, int]]
v2 : zip[Tuple[int, int]]
""")

def test_next(self):
Expand Down

0 comments on commit 58cff8b

Please sign in to comment.