Bad error message surrounding generic type aliases being `Any` when it's really just unbound
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die Reproduktion des Issues durch mypy laufen zu lassen, und vergleiche die Diagnosen für die rohe generische Klasse, A_Alias1, A_Alias2 und foo. Verfolge die beteiligte Behandlung der Typanzeige und generischer Aliase; abgeschlossen ist die Aufgabe, wenn die Diagnose eine ungebundene Typvariable korrekt von Any unterscheidet und das gemeldete Verhalten über die Beispiele hinweg konsistent ist.
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
- 30/100