Skip to content

Commit

Permalink
Disallow all super calls to methods with trivial bodies (#16756)
Browse files Browse the repository at this point in the history
Relates to:
https://discuss.python.org/t/calling-abstract-methods/42576

I think this makes mypy's behaviour more predictable
  • Loading branch information
hauntsaninja committed Mar 8, 2024
1 parent 0b8fed5 commit e0ad952
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
8 changes: 1 addition & 7 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,7 @@ def validate_super_call(node: FuncBase, mx: MemberContext) -> None:
impl = node.impl if isinstance(node.impl, FuncDef) else node.impl.func
unsafe_super = impl.is_trivial_body
if unsafe_super:
ret_type = (
impl.type.ret_type
if isinstance(impl.type, CallableType)
else AnyType(TypeOfAny.unannotated)
)
if not subtypes.is_subtype(NoneType(), ret_type):
mx.msg.unsafe_super(node.name, node.info.name, mx.context)
mx.msg.unsafe_super(node.name, node.info.name, mx.context)


def analyze_type_callable_member_access(name: str, typ: FunctionLike, mx: MemberContext) -> Type:
Expand Down
12 changes: 6 additions & 6 deletions test-data/unit/check-abstract.test
Original file line number Diff line number Diff line change
Expand Up @@ -896,9 +896,9 @@ class A(metaclass=ABCMeta):
class B(A):
@property
def x(self) -> int:
return super().x.y # E: "int" has no attribute "y"
return super().x.y # E: Call to abstract method "x" of "A" with trivial body via super() is unsafe \
# E: "int" has no attribute "y"
[builtins fixtures/property.pyi]
[out]

[case testSuperWithReadWriteAbstractProperty]
from abc import abstractproperty, ABCMeta
Expand Down Expand Up @@ -1659,10 +1659,10 @@ class Abstract:

class SubProto(Proto):
def meth(self) -> int:
return super().meth()
return super().meth() # E: Call to abstract method "meth" of "Proto" with trivial body via super() is unsafe
class SubAbstract(Abstract):
def meth(self) -> int:
return super().meth()
return super().meth() # E: Call to abstract method "meth" of "Abstract" with trivial body via super() is unsafe

[case testEmptyBodyNoSuperWarningOptionalReturn]
from typing import Protocol, Optional
Expand All @@ -1676,10 +1676,10 @@ class Abstract:

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

[case testEmptyBodyTypeCheckingOnly]
from typing import TYPE_CHECKING
Expand Down

0 comments on commit e0ad952

Please sign in to comment.