python / python/mypy

Bad error message surrounding generic type aliases being `Any` when it's really just unbound

Aperta
#11,549 1 commento 1 reazione 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

from __future__ import annotations
from typing import Generic, TypeVar, ClassVar, Any

T = TypeVar("T")


class A(Generic[T]):
    @classmethod
    def bar(cls: type[A[int]]) -> None:
        ...


A.bar()  # error: Invalid self argument "Type[A[Any]]" to attribute function "bar" with type "Callable[[Type[A[int]]], None]"
A_Alias1 = A
A_Alias1.bar()  # error: Invalid self argument "Type[A[Any]]" to attribute function "bar" with type "Callable[[Type[A[int]]], None]"
reveal_type(A_Alias1)  # note: Revealed type is "def [T] () -> __main__.A[T`1]"

A_Alias2 = A[Any]
A_Alias2.bar()  # valid
reveal_type(A_Alias2)  # note: Revealed type is "def () -> __main__.A[Any]"


def foo(a: type[A[int]]) -> None:
    ...
    
foo(A)  # no error
foo(A_Alias1)  # error: Argument 1 to "foo" has incompatible type "Type[A[Any]]"; expected "Type[A[int]]"

Invalid self argument "Type[A[Any]]" to attribute function "bar" with type "Callable[[Type[A[int]]], None]"

This error message is confusing, because it's implying it's Any when it's actually not, it looks like the error message is wrong.

I would think an error message like Invalid self argument "Type[A[T]]" to attribute function "bar" with type "Callable[[Type[A[int]]], None]" would be more accurate.

Also, with the foo example, why is it only complaining on the type alias and not on the raw usage? This is inconsistent with the class method example

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

Inizia eseguendo la riproduzione dell'issue tramite mypy e confronta le diagnosi per la classe generica grezza, A_Alias1, A_Alias2 e foo. Traccia la gestione coinvolta della visualizzazione dei tipi e degli alias generici; il lavoro è completo quando la diagnosi distingue correttamente una variabile di tipo non associata da Any e il comportamento riportato è coerente tra gli esempi.

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
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.