Bad error message surrounding generic type aliases being `Any` when it's really just unbound
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the issue's reproduction through mypy and compare diagnostics for the raw generic class, A_Alias1, A_Alias2, and foo. Trace the type-display and generic-alias handling involved; done means the diagnostic accurately distinguishes an unbound type variable from Any and the reported behavior is consistent across the examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100