Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
KotlinIsland opened this issue Nov 14, 2021 · 0 comments
Labels
bug mypy got something wrong

Comments

@KotlinIsland
Copy link
Contributor

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

@KotlinIsland KotlinIsland added the bug mypy got something wrong label Nov 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant