python / python/mypy

Add "is invariant" warning to Set as well as List for situations where a Collection should be used

Offen
#16,631 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Feature

mypy has a lovely warning about List being invariant in situations like this:

ls: List[int] = [1, 2, 3]

def process(stuff: List[Union[int, str]]):
    ...

process(ls)  # type error here, w/ nice explanation of invariance and suggestion to use Sequence

However, it does not have the same warning when Set is used in the same way:

s: Set[int] = {1, 2, 3}

def process(stuff: Set[Union[int, str]]):
    ...

process(s)  # merely states that the type is incompatible, no mention of invariance

Would be nice if the same warning mechanism could trigger here, suggesting Collection.

Pitch

Hopefully this is a simple widening of a rule that's special-cased for lists to get sets too? Not sure if other types like Dict/Tuple might be included too; I haven't tested them. If it's a lot of effort probably not worth it.

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, die bestehende Warnung zur Invarianz von List und ihre Tests zu finden; das Issue nennt keine bestimmten Dateien oder Einstiegspunkte. Erweitere dasselbe Warnverhalten auf Set und verifiziere, dass die Diagnose die Invarianz erklärt und Collection vorschlägt, wie im Beispiel gezeigt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
Issue-Typ
Feature
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

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