Add bound Hashable for types for keys of type Mapping and values of type Set
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 5.1k
- Forks
- 2.1k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 82
Description
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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par examiner stdlib/builtins.pyi au niveau des définitions de set et frozenset référencées dans l’issue, ainsi que les modifications associées aux mappings dans PR 9570. Déterminez comment représenter la variable de type de set et une variable covariante de frozenset bornée par Hashable, puis mettez à jour les stubs de stdlib et vérifiez que les clés non hachables ou les valeurs de type set sont rejetées par les outils de vérification de types concernés.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- developer-experience
- Type d'issue
- Fonctionnalité
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100