python / python/mypy

`functools.reduce` over sets: empty set not accepted without annotation

Aperta
#17,693 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Reproducer:

from typing import Collection
from functools import reduce


def _pull_out_loop_nest(
            loop_nests: list[frozenset[str]],
            inames_to_pull_out: frozenset[str]
        ) -> None:
    # OK
    emptyset: frozenset[str] = frozenset()
    assert inames_to_pull_out <= reduce(frozenset.union, loop_nests, emptyset)

    # Error
    assert inames_to_pull_out <= reduce(frozenset.union, loop_nests, frozenset())

Here is what mypy says as of 1.11.1:

mypybug.py:14: error: Argument 1 to "reduce" has incompatible type "Callable[[frozenset[_T_co], VarArg(Iterable[_S])], frozenset[_T_co | _S]]"; expected "Callable[[frozenset[Never], frozenset[str]], frozenset[Never]]"  [arg-type]

This seems spurious: It seems that something picks up the type of the empty set somewhat too eagerly. Pyright (1.1.376) also seems to like this code just fine.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il reproducer in mypybug.py ed eseguilo con mypy 1.11.1, confrontando i casi di frozenset vuoto annotato e non annotato. Traccia l’inferenza dei tipi di reduce coinvolta e aggiungi un test di regressione che mostri che la forma non annotata non produce più la diagnostica segnalata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.