Skip to content

Commit e4c26a5

Browse files
afriedergvanrossum
authored andcommitted
Raise errors on classmethods or staticmethods subclassing conflicts (#2843)
Fixes #1867
1 parent 0044eaa commit e4c26a5

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎mypy/checker.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,13 @@ def check_override(self, override: FunctionLike, original: FunctionLike,
907907
# this could be unsafe with reverse operator methods.
908908
fail = True
909909

910+
if isinstance(original, CallableType) and isinstance(override, CallableType):
911+
if (isinstance(original.definition, FuncItem) and
912+
isinstance(override.definition, FuncItem)):
913+
if ((original.definition.is_static or original.definition.is_class) and
914+
not (override.definition.is_static or override.definition.is_class)):
915+
fail = True
916+
910917
if fail:
911918
emitted_msg = False
912919
if (isinstance(override, CallableType) and

‎test-data/unit/check-classes.test‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,23 @@ class C:
809809
pass
810810
[builtins fixtures/classmethod.pyi]
811811

812+
[case testClassMethodSubclassing]
813+
class A:
814+
@classmethod
815+
def f(cls) -> None: pass
816+
817+
def g(self) -> None: pass
818+
819+
class B(A):
820+
def f(self) -> None: pass # E: Signature of "f" incompatible with supertype "A"
821+
822+
@classmethod
823+
def g(cls) -> None: pass
824+
825+
class C(A):
826+
@staticmethod
827+
def f() -> None: pass
828+
[builtins fixtures/classmethod.pyi]
812829

813830
-- Properties
814831
-- ----------
@@ -2700,6 +2717,26 @@ class B(A):
27002717
[out]
27012718
main:8: error: Incompatible types in assignment (expression has type Callable[[str], None], base class "A" defined the type as Callable[[int], None])
27022719

2720+
[case testClassStaticMethodSubclassing]
2721+
class A:
2722+
@staticmethod
2723+
def a() -> None: pass
2724+
2725+
def b(self) -> None: pass
2726+
2727+
@staticmethod
2728+
def c() -> None: pass
2729+
2730+
class B(A):
2731+
def a(self) -> None: pass # E: Signature of "a" incompatible with supertype "A"
2732+
2733+
@classmethod
2734+
def b(cls) -> None: pass
2735+
2736+
@staticmethod
2737+
def c() -> None: pass
2738+
[builtins fixtures/classmethod.pyi]
2739+
27032740
[case testTempNode]
27042741
class A():
27052742
def a(self) -> None: pass

0 commit comments

Comments
 (0)