python / python/typeshed

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

Abierto
#9,571 7 comentarios 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

status: deferred
Lenguaje dominante
Python
Estrellas
5.1k
Forks
2.1k
Merge medio
1 d 19 h
PR fusionados (30 d)
82

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza revisando stdlib/builtins.pyi en las definiciones de set y frozenset referenciadas en la issue, junto con los cambios relacionados en los mappings de PR 9570. Decide cómo deben representarse la variable de tipo de set y una variable covariante de frozenset acotada por Hashable; después, actualiza los stubs de stdlib y verifica que las claves no hasheables o los valores set sean rechazados por las herramientas de comprobación de tipos relevantes.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
developer-experience
Tipo de issue
Nueva funcionalidad
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.