python / python/mypy

Allow ignore to be on any line of multi-line statement

Aperta
#12,341 2 commenti 4 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Feature

Currently, comments can be added to a line to ignore either all errors (# type: ignore), or a specific error code (eg. # type: ignore[call-arg]). When a single statement spans multiple lines, the comment has to be added on the line that mypy "decides" is the one with the error (is it the first line of the expression, rather than first line of the statement?). In some cases, it would make sense to allow the comment to be on a different line of the same statement. I propose allowing it to be on any line of the statement, and therefore to apply to the whole statement.

Pitch
Sometimes it makes more sense for it to be another line, such as when importing something that mypy thinks does not exist, it makes sense to have the ignore on the same line as the attribute that is reported as not existing.

from typing import ( # ignore currently needs to be here
    Union,
    DoesNotExist, # type: ignore[error attr-defined]
)

Sometimes, a formatter like black changes a long line into a multiple line statement. The comment that was at the end of the line is moved to the end of the last line.

def foo(bar:int) -> str: ...
def baz() -> int: ...

a = ( # type: ignore[call-arg]
    "something" 
    if baz() > 0 else 
    foo() # ignore currently needs to be here
)  # formatters (eg. black) often put ignore here

https://mypy-play.net/?mypy=latest&python=3.10&flags=show-error-codes%2Cstrict%2Cwarn-unused-ignores&gist=e41c530d401ac4f21cb7a21e68fea141

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 con i due esempi multilinea e la riproduzione collegata di mypy-play; traccia il modo in cui mypy associa i commenti type: ignore alle istruzioni. Il lavoro è completato quando un ignore su qualsiasi riga di un’unica istruzione multilinea sopprime l’errore rilevante di quell’istruzione, inclusi i codici specifici, senza modificare il comportamento delle istruzioni separate.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Funzionalità
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.