python / python/typeshed

Mypy complains about the `all_equal` recipe from the itertools docs

Offen
#10,980 5 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stubs: false positive
Vorherrschende Sprache
Python
Sterne
5.1k
Forks
2.1k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
82

Beschreibung

The following function is given as a recipe in the itertools docs, indicating that it's an idiomatic usage of groupby:

from itertools import groupby

def all_equal(iterable):
    g = groupby(iterable)
    return next(g, True) and not next(g, False)

I think the correct way of adding type annotations to this function would be as follows, since it will work on arbitrary iterables:

from collections.abc import Iterable
from itertools import groupby

def all_equal(iterable: Iterable[object]) -> bool:
    g = groupby(iterable)
    return next(g, True) and not next(g, False)

Unfortunately, however, mypy complains about this function:

error: Argument 1 to "next" has incompatible type "groupby[object, object]"; expected "SupportsNext[bool]"  [arg-type]

(Mypy gives a similar error if I use Iterable[Any] instead of Iterable[object] for the argument annotation.)

Perhaps we should consider copy-and-pasting all the itertools recipes into our test_cases directory. They're all meant to be idiomatic uses of itertools, so if any of them fail to type check, there's probably a problem somewhere.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das all_equal-Rezept aus der itertools-Dokumentation mit der vorgeschlagenen Iterable-Annotation und dem aktuellen mypy-Verhalten zu reproduzieren. Überprüfe das vorhandene test_cases-Verzeichnis und erwäge, dort die itertools-Rezepte hinzuzufügen, wobei die erfolgreiche Typprüfung der Rezepte als Abschlusskriterium verwendet wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
testing, tooling
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.