python / python/mypy

Optional strict checking of *args and/or multiple assignment

Offen
#7,408 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature needs discussion topic-calls
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Currently mypy does optimistic checking of *args -- we don't generate error if it could be valid. This seems like a reasonable default, since it avoids false positive errors. However, some users might prefer stricter checking as an option. When this option is enabled, mypy would only allow variable-length (non-Any) *args in a call if the callee also accepts *args.

Example:

def f(a: int) -> None: pass

x = [1, 2]
f(*x)  # Currently accepted, since mypy doesn't know the length of x

PR #7392 relaxed the rules so that we assume that any list *args argument could be empty and thus we don't complain about even if a caller *args argument is only valid when it's empty.

Similarly this option (or a similar option) could reject variable-length rvalues in multiple assignment like this:

x = [1, 2]
a, b, c = x  # Currently accepted

If we'd have support for enabling/disabling specific error codes, these checks could easily be implemented through error codes that are disabled by default.

Refactoring code such as the above (if correct) to pass mypy could be somewhat painful, however. It could be written like this:

def f(a: int, b: int) -> None: pass

def g(x: List[int]) -> None:
    assert len(x) == 2
    f(x[0], x[1])

(Of course, a # type: ignore would also work.)

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

Es werden keine Quelldateien oder Tests genannt. Beginne mit der Durchsicht von PR #7392 und der bestehenden Behandlung von Argumenten variabler Länge bei *args und Mehrfachzuweisungen; abgeschlossen wäre die Arbeit mit einem festgelegten, optionalen Design für strikte Prüfungen dieser Fälle, einschließlich der Frage, wie die Prüfungen aktiviert werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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