From e0ad95296037446fccb398b8dadc54ae0751df46 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Fri, 8 Mar 2024 07:05:05 -0800 Subject: [PATCH] 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 --- mypy/checkmember.py | 8 +------- test-data/unit/check-abstract.test | 12 ++++++------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/mypy/checkmember.py b/mypy/checkmember.py index c24edacf0ee1..afa8f37ff7d5 100644 --- a/mypy/checkmember.py +++ b/mypy/checkmember.py @@ -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: diff --git a/test-data/unit/check-abstract.test b/test-data/unit/check-abstract.test index 7f91eb8e7145..3b0b9c520b75 100644 --- a/test-data/unit/check-abstract.test +++ b/test-data/unit/check-abstract.test @@ -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 @@ -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 @@ -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