python / python/mypy

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

Aperta
#20,409 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci la segnalazione con mypy 1.19.0 usando l'esempio fornito ed esegui mypy --strict foo.py. Traccia come vengono verificati gli argomenti generici type[_T] rispetto a Hashable, quindi aggiungi la copertura per le chiamate segnalate preservando il caso concreto accettato type[str]; il lavoro è concluso quando le chiamate generiche non producono errori.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.