python / python/mypy

Cryptic error message for two generic callables with different variable binding

Aperta
#17,551 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-type-variables topic-usability
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

If a @contextmanager yields a generic type alias, mypy 1.11 errors with Argument 1 to "contextmanager" has incompatible type "$TYPE"; expected "$TYPE" [arg-type], where the inferred and expected types are identical.

This does not happen if the generic type is inlined (no type alias), or if the type alias is not generic; see playground link below for examples.

This started happening in 1.11 (didn't happen in 1.10). This may be related to https://github.com/python/mypy/issues/8696, but I opened a new issue because it looks like a regression.

Real code that tripped this: link.

To Reproduce

Short version (more examples in playground links below):

GenericFn = Callable[[_T], _T]

@contextmanager
def make_generic() -> Iterator[GenericFn[_T]]:
    def rv(obj: _T) -> _T:
        return obj
    yield rv

Playground:

Expected Behavior

Either fail with a useful error message (the inferred and expected types being exactly the same is confusing; I am not sure what would fix the error), or don't fail (like 1.10).

Actual Behavior

error: Argument 1 to "contextmanager" has incompatible type "Callable[[], Iterator[Callable[[_T], _T]]]"; expected "Callable[[], Iterator[Callable[[_T], _T]]]"  [arg-type]

Your Environment

  • Mypy version used: 1.11.0
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.12

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

Riproduci l’esempio minimo di @contextmanager con mypy 1.11.0 su Python 3.12 e --strict, quindi confrontalo con 1.10.1 e con i casi collegati del playground. Traccia la gestione degli alias di tipo generici attorno a make_generic e contextmanager; il lavoro è completato quando si evita la regressione oppure si sostituiscono i tipi inferito e atteso identici con un errore utile.

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
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.