python / python/typeshed

Set dunder operators too permissive?

Offen
#3,184 4 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stubs: improvement
Vorherrschende Sprache
Python
Sterne
5.1k
Forks
2.1k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
82

Beschreibung

Similar to #1840

With #3181 I made KeysView consistent with Set but I really think the operators are confusingly permissive.

Let's look at them

# difference
{'1'} - {2}
reveal_type({'1'} - {2})  # => Set[str]

This should fail because this is always a null operation and thus
a programmer error.

# intersection
{'1'} & {2}  # => Set[str]
reveal_type({'1'} & {2})  # => Set[str]

This really should fail because it is unlikely meaningful; the
return type is always an empty set thus it is also not a Set[str]

# sym difference
{'1'} ^ {2}
reveal_type({'1'} ^ {2})  # => Set[Union[str, int]]

Although the assumed union type is correct, Set[A] & Set[B] => Set[A|B] just doesn't make sense. The operator can only meaningful be applied to same type sets. What we do here is just a form of plain concat or union.

# union
{'1'} | {2} # MAYBE OK
reveal_type({'1'} | {2})  # => Set[Union[str, int]]

If we look at Sets as just containers without some
properties, we could accept it to construct mixed
type Sets using 'union'. I think it is okay for Python,
the name union makes it meaningful, but I maybe never did
that in practice.
Since it adds to the type it is also likely that
the following part of the program catches mistakes.

(But lists don't allow that [1] + ['2'] although they're really
arbitrary containers. Except my PR #3183.)

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 mit den Beispielen dieses Issues und vergleiche die zugehörigen Diskussionen in #1840, #3181 und #3183. Ermittle, welche Set- und KeysView-Operatoren gemischte Elementtypen ablehnen sollten, einschließlich der Frage, ob union weiterhin zulässig bleibt. Die Arbeit ist abgeschlossen, wenn das Projekt für jeden Operator eine vereinbarte Regel hat und das relevante Typing-Verhalten diese Entscheidung widerspiegelt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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