Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow all super calls to methods with trivial bodies #16756

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading