python / python/mypy

Mypy gives confusing/misleading error message

Aperta
#17,531 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

Mypy reports error: Name "..." is not defined [name-defined] when using an instance attribute that was annotated.

To Reproduce

from inspect import Parameter


class ListElementDescriptor[T]:
    name: str
    item_type: type[T]

    @property
    def signature_parameter(self) -> Parameter:
        return Parameter(name=self.name, kind=Parameter.KEYWORD_ONLY, annotation=list[self.item_type])

Expected Behavior

Definitely not an error that self.item_type is not defined. I suspect mypy tries to to convey a different error, maybe related to using variables for type parameters in generics, though I disagree with that as well since list[] is not used here in a typing context to specify a type, but is used as a value.

Actual Behavior

typing-7.py:11: error: Name "self.item_type" is not defined  [name-defined]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.12.0+dev.6a0657e5959ba1777c4d427f8f355d499035d145 (compiled: no)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files):
[tool.mypy]
enable_incomplete_feature = "NewGenericSyntax"
disable_bytearray_promotion = true
disable_memoryview_promotion = true
check_untyped_defs = true
warn_unreachable = true
warn_redundant_casts = true
warn_unused_ignores = true
  • 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

Inizia eseguendo l’esempio ListElementDescriptor fornito con mypy 1.12.0+dev.6a0657e5959ba1777c4d427f8f355d499035d145 e la configurazione Python 3.12 mostrata. Traccia la diagnostica per list[self.item_type] e aggiornala in modo che il problema segnalato sia accurato, invece di affermare che self.item_type non è definito; esegui nuovamente la riproduzione per confermare che l’errore name-defined fuorviante sia scomparso.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.