Skip to content

itertools' all_equal recipe causes false arg-type failures #15150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
me-and opened this issue Apr 27, 2023 · 0 comments · May be fixed by #18976
Open

itertools' all_equal recipe causes false arg-type failures #15150

me-and opened this issue Apr 27, 2023 · 0 comments · May be fixed by #18976
Labels
bug mypy got something wrong topic-type-context Type context / bidirectional inference

Comments

@me-and
Copy link

me-and commented Apr 27, 2023

Bug Report

Adding annotations to the itertools recipe for all_equal results in a false "arg-type" error message from mypy. This can be worked around by adding an intermediate argument, but the intermediate argument doesn't have any functional impact, so I think this must be a bug.

To Reproduce

Playground link: https://mypy-play.net/?mypy=latest&python=3.11&gist=012171f435d6fd0119ac028acc794877

The following two functions, both based on the all_equal recipe in the official Python itertools documentation, work identically. However all_equal_error produces an "arg-type" error from mypy, while all_equal_pass does not.

from itertools import groupby
from typing import TypeVar
from collections.abc import Iterable

T = TypeVar('T')

def all_equal_pass(i: Iterable[T]) -> bool:
    g = groupby(i)
    ans = next(g, True) and not next(g, False)
    return ans
    
def all_equal_error(i: Iterable[T]) -> bool:
    g = groupby(i)
    return next(g, True) and not next(g, False)

Expected Behavior

Either (a) my typing annotations are correct and mypy shouldn't report a problem with either function, or (b) my typing annotations are bogus and mypy should report a problem with both functions.

Actual Behavior

mypy produces the following error:

main.py:14: error: Argument 1 to "next" has incompatible type "groupby[T, T]"; expected "SupportsNext[bool]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Your Environment

I've reproduced this both on the playground (mypy latest 1.2.0, Python 3.11, default options) and on my Debian testing box (mypy=1.0.1-1, python3.11=3.11.2-6, no command line options, no changes to out-of-the-box configuration).

@me-and me-and added the bug mypy got something wrong label Apr 27, 2023
@JelleZijlstra JelleZijlstra added the topic-type-context Type context / bidirectional inference label Apr 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-context Type context / bidirectional inference
Projects
None yet
2 participants