does not prevent overriding of `ClassVar` with instance setter
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Use the supplied C/D/E reproduction as the starting regression case, then inspect mypy's property-setter handling and the existing ClassVar override diagnostic path. Done means both D and E produce the expected cannot-override error while the existing read-only-property behavior remains covered.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100