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

Descriptors and ClassVars #14969

Open
klen opened this issue Mar 28, 2023 · 2 comments · May be fixed by #17170
Open

Descriptors and ClassVars #14969

klen opened this issue Mar 28, 2023 · 2 comments · May be fixed by #17170
Labels
bug mypy got something wrong topic-descriptors Properties, class vs. instance attributes

Comments

@klen
Copy link

klen commented Mar 28, 2023

Bug Report

Have came across this issue from pyright

To Reproduce

# This sample tests the case where a member access is performed through
# an object using a field that is annotated as a ClassVar. Normally this
# is disallowed, but it is permitted if the type of the ClassVar is
# a descriptor object.
from __future__ import annotations

from typing import TYPE_CHECKING, ClassVar, Generic, Self, TypeVar, overload

T = TypeVar("T")


class Descriptor(Generic[T]):
    if TYPE_CHECKING:

        @overload
        def __get__(self, instance: None, owner) -> Self:
            ...

        @overload
        def __get__(self, instance: object, owner) -> T:
            ...

        def __get__(self, instance: object | None, owner) -> Self | T:
            ...

        def __set__(self, instance: object, value: T) -> None:
            ...

        def is_null(self) -> bool:
            ...


class Example:
    field1: ClassVar = Descriptor[str]()

    field2: ClassVar = ""

    def reset(self) -> None:
        self.field1 = ""

        # This should generate an error because field2 isn't
        # a descriptor object.
        self.field2 = ""

Expected Behavior

Copied @erictraut comment:

ClassVar should work fine. If the class variable is assigned a descriptor, then the class variable will not be overwritten if you set the same attribute at the object level. So it sounds like mypy has a bug here. Pyright had a similar bug, which I recently fixed. Please report the bug to the maintainers of mypy.

Actual Behavior

classvar.py:39: error: Cannot assign to class variable "field1" via instance  [misc]
classvar.py:43: error: Cannot assign to class variable "field2" via instance  [misc]

Your Environment

  • Mypy version used: 1.1.1
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.11.2
@klen klen added the bug mypy got something wrong label Mar 28, 2023
@AlexWaygood AlexWaygood added the topic-descriptors Properties, class vs. instance attributes label Mar 28, 2023
@lhanekamp
Copy link

Hello! I would like to work on this issue can you please assign it to me?

@sobolevn
Copy link
Member

@lhanekamp feel free to take this issue :)
We usually don't assign people on issues, it does not change anything.

If you have any questions, please feel free to ask them here!

@bzoracler bzoracler linked a pull request Apr 25, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-descriptors Properties, class vs. instance attributes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants