python / python/mypy

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

Offen
#20,409 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

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

Reproduziere den Bericht mit mypy 1.19.0 anhand des bereitgestellten Beispiels und führe mypy --strict foo.py aus. Verfolge, wie generische type[_T]-Argumente gegen Hashable geprüft werden, und ergänze Tests für die gemeldeten Aufrufe, wobei der akzeptierte konkrete Fall type[str] erhalten bleibt; fertig ist die Änderung, wenn die generischen Aufrufe keine Fehler erzeugen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.