python / python/mypy

When using`--disallow-any-expr`, silence complaints about Any where object would work

Aperta
#9,153 23 commenti 4 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

topic-disallow-any
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Problem

Using Any is not type safe, therefore Mypy provides options to disallow Any. However, there are many uses of Any from third-party and stdlib, so these options tend to result in a lot of errors even when they are not relevant.

I'm finding many situations where I get an error about Any, but the code would be deemed safe if it was object.

Examples

This produces errors about Any on both lines even though the value is not used:

type_hints = typing.get_type_hints(...)  # Dict[str, Any]
if k in type_hints:
    ...

This produces errors as well, but object would not produce errors, as Mypy can infer everything necessary here to be type safe.

origin = typing.get_origin(...)  # Any
if origin is Literal:
    ...
elif origin is list:
    ...

Solution

Add an option, or change the behaviour of the current disallow Any options, such that Any is treated like object. Then, rather than producing errors when Any is present, just show normal errors with Any (as if it was object) in them (e.g. 'Any has no attribute foo').

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 riproducendo i due esempi con --disallow-any-expr e confronta le relative diagnosi con annotazioni object equivalenti. Esamina la gestione esistente delle opzioni disallow-Any, quindi definisci test per trattare Any come object mantenendo al contempo gli errori ordinari, come gli attributi mancanti.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.