Skip to content

Commit e0ad952

Browse files
authored
Disallow all super calls to methods with trivial bodies (#16756)
Relates to: https://discuss.python.org/t/calling-abstract-methods/42576 I think this makes mypy's behaviour more predictable
1 parent 0b8fed5 commit e0ad952

File tree

2 files changed

+7
-13
lines changed

2 files changed

+7
-13
lines changed

mypy/checkmember.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,7 @@ def validate_super_call(node: FuncBase, mx: MemberContext) -> None:
359359
impl = node.impl if isinstance(node.impl, FuncDef) else node.impl.func
360360
unsafe_super = impl.is_trivial_body
361361
if unsafe_super:
362-
ret_type = (
363-
impl.type.ret_type
364-
if isinstance(impl.type, CallableType)
365-
else AnyType(TypeOfAny.unannotated)
366-
)
367-
if not subtypes.is_subtype(NoneType(), ret_type):
368-
mx.msg.unsafe_super(node.name, node.info.name, mx.context)
362+
mx.msg.unsafe_super(node.name, node.info.name, mx.context)
369363

370364

371365
def analyze_type_callable_member_access(name: str, typ: FunctionLike, mx: MemberContext) -> Type:

test-data/unit/check-abstract.test

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -896,9 +896,9 @@ class A(metaclass=ABCMeta):
896896
class B(A):
897897
@property
898898
def x(self) -> int:
899-
return super().x.y # E: "int" has no attribute "y"
899+
return super().x.y # E: Call to abstract method "x" of "A" with trivial body via super() is unsafe \
900+
# E: "int" has no attribute "y"
900901
[builtins fixtures/property.pyi]
901-
[out]
902902

903903
[case testSuperWithReadWriteAbstractProperty]
904904
from abc import abstractproperty, ABCMeta
@@ -1659,10 +1659,10 @@ class Abstract:
16591659

16601660
class SubProto(Proto):
16611661
def meth(self) -> int:
1662-
return super().meth()
1662+
return super().meth() # E: Call to abstract method "meth" of "Proto" with trivial body via super() is unsafe
16631663
class SubAbstract(Abstract):
16641664
def meth(self) -> int:
1665-
return super().meth()
1665+
return super().meth() # E: Call to abstract method "meth" of "Abstract" with trivial body via super() is unsafe
16661666

16671667
[case testEmptyBodyNoSuperWarningOptionalReturn]
16681668
from typing import Protocol, Optional
@@ -1676,10 +1676,10 @@ class Abstract:
16761676

16771677
class SubProto(Proto):
16781678
def meth(self) -> Optional[int]:
1679-
return super().meth()
1679+
return super().meth() # E: Call to abstract method "meth" of "Proto" with trivial body via super() is unsafe
16801680
class SubAbstract(Abstract):
16811681
def meth(self) -> Optional[int]:
1682-
return super().meth()
1682+
return super().meth() # E: Call to abstract method "meth" of "Abstract" with trivial body via super() is unsafe
16831683

16841684
[case testEmptyBodyTypeCheckingOnly]
16851685
from typing import TYPE_CHECKING

0 commit comments

Comments
 (0)