python / python/mypy

The result of `type(TypeVar)` is not hashable

Abierto
#20,409 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Bug Report

It seems that x: type[_T] is not hashable, and neither is y: _T = ... type(y).

To Reproduce

import functools
from typing import TypeVar


_T = TypeVar('_T')


@functools.lru_cache(maxsize=2048)
def do_something_expensive_with_type[_T](t_type: type[_T]) -> list[_T]:
    return []


def MyFunc1[_T](t: _T, tt: type[_T]) -> list[_T]:
    # arg-type - Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "type[_T]"; expected "Hashable"
    do_something_expensive_with_type(type(t))

    # arg-type - Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "type[_T]"; expected "Hashable"
    do_something_expensive_with_type(tt)

    # but type(t)/tt is indeed hashable
    reveal_type(type(t).__hash__)
    reveal_type(tt.__hash__)
    return []


@functools.lru_cache(maxsize=2048)
def do_something_expensive_with_str(t_type: type[str]) -> list[str]:
    return []


def MyFunc2() -> list[str]:
    do_something_expensive_with_str(type(''))
    reveal_type(str.__hash__)
    return []

Expected Behavior

I'd expect the following not to give any errors for mypy --strict example.py

Actual Behavior

foo.py:15: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "type[_T]"; expected "Hashable"  [arg-type]
foo.py:18: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "type[_T]"; expected "Hashable"  [arg-type]
foo.py:21: note: Revealed type is "def (self: builtins.object) -> builtins.int"
foo.py:22: note: Revealed type is "def (self: builtins.object) -> builtins.int"
foo.py:35: note: Revealed type is "def (self: builtins.str) -> builtins.int"

Your Environment

Tested on mypy 1.19.0 (compiled: yes), like so: mypy --strict foo.py

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

Reproduce el informe con mypy 1.19.0 usando el ejemplo proporcionado y ejecuta mypy --strict foo.py. Rastrea cómo se comprueban los argumentos genéricos type[_T] frente a Hashable y añade cobertura para las llamadas reportadas, conservando el caso concreto aceptado type[str]; se considera terminado cuando las llamadas genéricas no producen errores.

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

Evaluación

Stack tecnológico
python
Área
devtools
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
38/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.