You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 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__ importannotationsfromtypingimportTYPE_CHECKING, ClassVar, Generic, Self, TypeVar, overloadT=TypeVar("T")
classDescriptor(Generic[T]):
ifTYPE_CHECKING:
@overloaddef__get__(self, instance: None, owner) ->Self:
...
@overloaddef__get__(self, instance: object, owner) ->T:
...
def__get__(self, instance: object|None, owner) ->Self|T:
...
def__set__(self, instance: object, value: T) ->None:
...
defis_null(self) ->bool:
...
classExample:
field1: ClassVar=Descriptor[str]()
field2: ClassVar=""defreset(self) ->None:
self.field1=""# This should generate an error because field2 isn't# a descriptor object.self.field2=""
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
The text was updated successfully, but these errors were encountered:
Bug Report
Have came across this issue from pyright
To Reproduce
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
Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: