Skip to content

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

Open
@KotlinIsland

Description

@KotlinIsland
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions