python / python/mypy

Error message for `TypeForm` arguments with a single unresolvable forward reference is misleading

Aperta
#21,695 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

When passing a single strategized type expression to a function accepting a TypeForm argument, which cannot be fully resolved, due to e.g. a missing import, we get an arg-type error of the form:

Instead of an error, telling us why the type expression is invalid.

To Reproduce
https://mypy-play.net/?mypy=master&python=3.14&gist=891447236311046360886ba63c1fac83

from typing_extensions import TypeForm

def foo[T](x: TypeForm[T]) -> None:
    pass

foo("Unresolvable")

Expected Behavior

mypy should emit a name-error for Unresolvable, since it was never defined. The given example is probably the most controversial case though, since someone may actually accidentally be passing an invalid string literal, but the same thing happens for less controversial cases like "list[Unresolvable]". But I think either way it makes more sense to treat the given string literal as a type expression and emit the same errors you would emit if you saw the same string literal in an annotation.

Actual Behavior

error: Argument 1 to "foo" has incompatible type "str"; expected "TypeForm[Never]"  [arg-type]

This error does not help us solve the problem in our code at all.

Your Environment

  • Mypy version used: 2.2
  • Python version used: 3.14

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 fornita di mypy-play con mypy 2.2 e Python 3.14, quindi traccia il percorso di controllo degli argomenti di TypeForm per una singola espressione stringa non risolvibile. Il lavoro è completato quando l’esempio segnala un name-error per Unresolvable, invece di segnalare soltanto il fuorviante errore arg-type.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.