python / python/mypy

emit a warning for "unused Any"

Offen
#20,078 1 Kommentar 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature topic-disallow-any
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

A common problem afflicting novices getting into python static type annotations is that they assume Any is the top type in Python, like how Never is its bottom type. Hence, they use Any too much, where it is not necessary.

I think we should emit a warning when an explicit Any annotation is used but didn't have to be, much like what we do for warn-redundant-casts.

In fact, I think there are three related warnings we should emit:

  1. If Any is used but object would have worked
  2. If Any is used but mypy was able to deduce a type that worked (using the regular mypy deduction)
  3. If there is a Callable special form with ... (Ellipsis) in place of the list of parameter types (basically just another way to spell Any, see https://typing.python.org/en/latest/spec/callables.html#meaning-of-in-callable) but one of the previous two cases holds

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das bestehende Verhalten von mypy bei warn-redundant-casts und die Semantik von Ellipsen in Callable der typing-Spezifikation zu prüfen. Die Arbeit ist abgeschlossen, wenn mypy die drei vorgeschlagenen Fälle unterscheiden kann: unnötiges Any, wenn object funktioniert, Any, wenn die Inferenz einen verwendbaren Typ liefert, und die entsprechende Callable-Form, mit Warnungen für jeden zutreffenden Fall.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers, tooling
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.