diff --git a/mypy/checker.py b/mypy/checker.py index 12afa4d3edf5..0183a78ed05d 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -2280,6 +2280,25 @@ def check_method_override_for_base_with_name( if inner is not None: typ = inner typ = get_property_type(typ) + + if ( + isinstance(original_node, Var) + and original_node.is_classvar + and defn.name == original_node.name + and (isinstance(defn, (Decorator, OverloadedFuncDef))) + ): + decorator_func = None + if isinstance(defn, Decorator): + decorator_func = defn.func + elif isinstance(defn.items[0], Decorator): + decorator_func = defn.items[0].func + + if decorator_func: + self.fail( + message_registry.CANNOT_OVERRIDE_CLASS_VAR.format(base.name), + decorator_func, + ) + if ( isinstance(original_node, Var) and not original_node.is_final diff --git a/test-data/unit/check-classvar.test b/test-data/unit/check-classvar.test index 918926627bfd..1b7998786baf 100644 --- a/test-data/unit/check-classvar.test +++ b/test-data/unit/check-classvar.test @@ -269,6 +269,21 @@ class A(metaclass=ABCMeta): class B(A): x = 0 # type: ClassVar[int] +[case testOverrideWithPropertyDecorator] +from typing import ClassVar + +class A: + x: ClassVar[int] +class B(A): + @property + def x(self) -> int: ... + + @x.setter + def x(self, value: int) -> None: ... +[builtins fixtures/property.pyi] +[out] +main:7: error: Cannot override class variable (previously declared on base class "A") with instance variable + [case testAcrossModules] import m reveal_type(m.A().x)