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

does not prevent overriding of ClassVar with instance setter #18790

Open
asottile opened this issue Mar 11, 2025 · 0 comments · May be fixed by #18853
Open

does not prevent overriding of ClassVar with instance setter #18790

asottile opened this issue Mar 11, 2025 · 0 comments · May be fixed by #18853
Labels
bug mypy got something wrong good-second-issue

Comments

@asottile
Copy link
Contributor

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
@asottile asottile added the bug mypy got something wrong label Mar 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong good-second-issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants