Skip to content

does not prevent overriding of ClassVar with instance setter #18790

Open
@asottile

Description

@asottile

Bug Report

this seems to be a special case for @property but probably shouldn't be

a settable instance @property currently is allowed to override a ClassVar but I believe it should produce a diagnostic

To Reproduce

from typing import ClassVar

class C:
    u: ClassVar[str]

class D(C):
    u: str

class E(C):
    @property
    def u(self) -> str: return ''
    @u.setter
    def u(self, s: str) -> None: ...

Expected Behavior

I expect both D and E to produce errors (cannot override ClassVar with instance variable)

Actual Behavior

but only D does

$ mypy t2.py 
t2.py:7: error: Cannot override class variable (previously declared on base class "C") with instance variable  [misc]
Found 1 error in 1 file (checked 1 source file)

interestingly enough, if you leave out the setter it does produce a diagnostic -- so I suspect this should be an easy patch to that particular codepath:

$ diff -u t2.py t3.py
--- t2.py	2025-03-11 19:31:11.656092408 -0400
+++ t3.py	2025-03-11 19:38:46.827393165 -0400
@@ -9,5 +9,3 @@
 class E(C):
     @property
     def u(self) -> str: return ''
-    @u.setter
-    def u(self, s: str) -> None: ...
$ mypy t3.py 
t3.py:7: error: Cannot override class variable (previously declared on base class "C") with instance variable  [misc]
t3.py:11: error: Cannot override writeable attribute with read-only property  [override]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: n/a
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.12.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions