This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: itertools.combinations has wrong type when using the typing package
Type: behavior Stage: resolved
Components: Demos and Tools Versions: Python 3.6
process
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: josh.r, vaibhavkarve, xtreak
Priority: normal Keywords:

Created on 2018-10-17 00:38 by vaibhavkarve, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg327849 - (view) Author: Vaibhav Karve (vaibhavkarve) Date: 2018-10-17 00:38
If I run mypy on the following file called test.py, I get an error:

    # test.py
    from typing import Iterator, Tuple
    import itertools as it

    a : Iterator[Tuple[int, ...]]
    a = it.product([1,2,3], repeat = 2)
    
    b : Iterator[Tuple[int, ...]]
    b = it.combinations([1,2,3], 2)

The line about a goes through without complain. But mypy complains about b by printing the following error message--

test.py:8: error: Incompatible types in assignment (expression has type "Iterable[Tuple[int, ...]]", variable has type "Iterator[Tuple[int, ...]]")
test.py:8: note: 'Iterable' is missing following 'Iterator' protocol member:
test.py:8: note:     __next__

So basically, it.product is an Iterator but it.combinations is an Iterable (I think it should be an iterator too). I think (without a lot of evidence) that this is a bug in itertools and not in typing.

PS: I apologize if my comment is not formatted according to best practices. This is my first time registering a new issue.
msg327852 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2018-10-17 02:34
Looks like a bug in the typeshed (which mypy depends on to provide typing info for most of the stdlib, which isn't explicitly typed). Affects both combinations and combinations_with_replacement from a quick check of the code: https://github.com/python/typeshed/blob/94485f9e4f86df143801c1810a58df993b2b79b3/stdlib/3/itertools.pyi#L103

Presumably this should be opened on the typeshed tracker. https://github.com/python/typeshed/issues
msg327864 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-10-17 06:10
Thanks for the report. I agree with Josh. I propose closing this as third-party and raising an issue in typeshed GitHub repo.
History
Date User Action Args
2022-04-11 14:59:07adminsetgithub: 79187
2018-10-17 17:59:22brett.cannonsetstatus: open -> closed
resolution: third party
stage: resolved
2018-10-17 06:10:11xtreaksetnosy: + xtreak
messages: + msg327864
2018-10-17 02:34:10josh.rsetnosy: + josh.r
messages: + msg327852
2018-10-17 00:38:14vaibhavkarvecreate