python / python/mypy

New type inference processes `itertools.groupby` key as `Any`

Aperta
#16,986 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

Bug Report

Given code like:

import itertools

for grouper, members in itertools.groupby('aBcD', key=lambda x: x.isupper()):
    reveal_type(grouper)
    reveal_type(members)

Running mypy at 1.6.1 without arguments or on any more recent version with --old-type-inference results in the expected output:

$ mypy demo.py --old-type-inference
demo.py:4: note: Revealed type is "builtins.bool"
demo.py:5: note: Revealed type is "typing.Iterator[builtins.str]"
Success: no issues found in 1 source file

However on more recent versions without --old-type-inference we get:

$ mypy demo.py 
demo.py:4: note: Revealed type is "Any"
demo.py:5: note: Revealed type is "typing.Iterator[builtins.str]"
Success: no issues found in 1 source file

Original case

In my original use-case, the grouper key was a tuple which I was spraying into a function call:

    for source, errors_group in itertools.groupby(errors, key=lambda x: x.source):
            messages = [x.message for x in errors_group]
            report_errors(*source, messages)

mypy was previously happy with that, however since the change to process the grouper key as Any I now get errors:

sr/comp/validation.py:113:13: error: Too many arguments for "report_errors"  [call-arg]

Your Environment

  • Mypy version used: bisected to 93d4cb0a2ef1723ce92f39ae61fe6a0c010eb90b being the commit which introduced the change (from which I then tested --old-type-inference); I realise that commit doesn't introduce the new inference logic, though I haven't chased beyond that commit
  • Mypy command-line flags: none required
  • Mypy configuration options from mypy.ini (and other config files): none required
  • Python version used: 3.10

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 eseguendo la riproduzione di demo.py con e senza --old-type-inference, quindi esamina la modifica relativa al commit 93d4cb0a2ef1723ce92f39ae61fe6a0c010eb90b. Il lavoro è completato quando la chiave di itertools.groupby viene rivelata come builtins.bool, mentre i membri rimangono typing.Iterator[builtins.str], senza interrompere la chiamata con chiave tupla mostrata nel caso originale.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.