python / python/typeshed

Add bound Hashable for types for keys of type Mapping and values of type Set

Offen
#9,571 7 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Problem

Types used for keys in mappings and values in sets are too broad.

How to reproduce
d: [list, int] = {}  # Tools that use typeshed see this code as valid.
d[[1]] = 2 # TypeError: unhashable type: 'list'
Solution

Change type of keys in mappings: _KT = TypeVar("_KT") to _KT = TypeVar("_KT", bound=Hashable)

  • Added PR with fixes to stdlib

But I still have questions about how to fix the types in set and frozenset.

  • For set, we using _T, which is also used for values in the list. Can I change it to _KT? The requirements for the type of values in the set are the same as for the keys in the dictionary. I understand that it is a value, not a key, and the name can be confusing. Do you think of a better option?

  • For frozenset, we using covariant _T_co, which is also used for values in the tuple. It's the same problem here. We need a new type that is covariant and bound to Hashable.

  • set

  • frozenset

And once we figure out what to do with types in stdlib, I will be happy to add the same changes to third-party libraries.

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

Beginnen Sie mit der Durchsicht von stdlib/builtins.pyi bei den in der Issue referenzierten Definitionen von set und frozenset sowie der zugehörigen Mapping-Änderungen in PR 9570. Entscheiden Sie, wie die Typvariable für set und eine kovariante, an Hashable gebundene Variable für frozenset dargestellt werden sollen, aktualisieren Sie anschließend die stdlib-Stubs und verifizieren Sie, dass nicht hashbare Schlüssel oder set-Werte von den relevanten Typprüfungs-Tools abgelehnt werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
developer-experience
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
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.