python / python/mypy

Support override for `attr-defined` errors (something like a promise to mypy that the attr is defined going forward)

Aperta
#18,109 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Feature

There are cases where mypy spits out an attr-defined error when accessing bar.foo; these can be ignore per-use with a # type: ignore[attr-defined], however, there doesn't currently seem to be a way to tell mypy that bar does indeed define foo.

In this case, it'd be nice if there was a way to tell mypy that bar does define foo, so that bar.foo can be used without spamming # type: ignore[attr-defined] comments everywhere.

How this is done I don't have an opinion about, but for example, it could be something like: # type: bar.foo: int.

Pitch

There are multiple useful cases of this:

  1. Although this is not the primary motivation, it would cover the case brought up here: https://github.com/python/mypy/issues/15085

  2. This could allow type hinting for attributes added after a class definition. One example would be for users who enjoy using a TRACE log level with the logging module. That is, if a user adds a TRACE logging level to the logging module such as shown here: https://stackoverflow.com/questions/2183233/how-to-add-a-custom-loglevel-to-pythons-logging-facility
    In this instance, this would allow use of logging.trace and logging.TRACE without requiring #type: ignore[attr-defined] used at every occurrence.
    In general, this handles the case where you have something like:

class A:
    pass
A.B = "foo"    # Some comment to promise mypy that A has an attr named B defined
  1. Additionally, this can allow users to better handle when 3rd party libraries do not play nice with mypy. For example:
from cryptography.hazmat.primitives.serialization import load_ssh_private_key

This gives an attr-defined warning, although this import statement works 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 tracciando il modo in cui mypy segnala le diagnosi attr-defined e gestisce i commenti type: ignore[attr-defined] per singolo utilizzo. Definisci la sintassi di promise proposta e il suo ambito usando gli esempi nell’issue, quindi aggiungi test che mostrino che gli attributi promessi non producono più errori ripetuti, mentre gli attributi mancanti non correlati continuano a produrli.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.