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

classmethods shouldn't be allowed to access generic attributes #11537

Open
KotlinIsland opened this issue Nov 13, 2021 · 2 comments
Open

classmethods shouldn't be allowed to access generic attributes #11537

KotlinIsland opened this issue Nov 13, 2021 · 2 comments
Labels
bug mypy got something wrong

Comments

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Nov 13, 2021

from typing import Generic, TypeVar

T = TypeVar("T")


class A(Generic[T]):
    class_attr: T

    @classmethod
    def set_attr(cls, arg: T) -> None:
        cls.class_attr = arg  # no error?


B = A[str]
B.set_attr("1")
B.class_attr = "1"  # error: Access to generic instance variables via class is ambiguous

C = A[int]
C.set_attr(1)
C.class_attr = 1  # error: Access to generic instance variables via class is ambiguous

value = B().class_attr

reveal_type(value)  # revealed: str, actual value: int
@KotlinIsland KotlinIsland added the bug mypy got something wrong label Nov 13, 2021
@KotlinIsland KotlinIsland changed the title classmethods shouldn't be allowed to have generics in their signatures classmethods shouldn't be allowed to access generic attributes Nov 13, 2021
@DetachHead
Copy link
Contributor

related: #9456 (comment)

@Dreamsorcerer
Copy link
Contributor

As per #9456 (comment), the error here is that mypy allows a TypeVar for a class attribute (which should be annotated with ClassVar, unlike in the example), and also allows assigning to an instance attribute in a classmethod.

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

3 participants