From 672bf9a607fccf25d1579d373b70fbe383437a74 Mon Sep 17 00:00:00 2001 From: Tim Martin Date: Fri, 25 Mar 2022 20:21:34 +0000 Subject: [PATCH] Suppress ``useless-super-delegation`` if return type changed (#5822) --- pylint/checkers/classes/class_checker.py | 7 +++++ .../u/useless/useless_super_delegation_py3.py | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/pylint/checkers/classes/class_checker.py b/pylint/checkers/classes/class_checker.py index e8218ddef99..91f53f0128d 100644 --- a/pylint/checkers/classes/class_checker.py +++ b/pylint/checkers/classes/class_checker.py @@ -1263,6 +1263,13 @@ def _check_useless_super_delegation(self, function): if meth_node is not None: + if ( + function.returns is not None + and (meth_node.returns is None or meth_node.returns.as_string() != function.returns.as_string()) + ): + # Override adds typing information to the return type + return + def form_annotations(arguments): annotations = chain( (arguments.posonlyargs_annotations or []), arguments.annotations diff --git a/tests/functional/u/useless/useless_super_delegation_py3.py b/tests/functional/u/useless/useless_super_delegation_py3.py index b32ced873bd..6abdf6d3bd9 100644 --- a/tests/functional/u/useless/useless_super_delegation_py3.py +++ b/tests/functional/u/useless/useless_super_delegation_py3.py @@ -41,3 +41,30 @@ def __init__(self, thing: object) -> None: # [useless-super-delegation] class Test: def __init__(self, _arg: List[int]) -> None: super().__init__() + + +import random + +class ReturnTypeAny: + choices = ['a', 1, (2, 3)] + + def draw(self) -> None: + return random.choice(self.choices) + +class ReturnTypeNarrowed(ReturnTypeAny): + choices = [1, 2, 3] + + def draw(self) -> int: + return super().draw() + +class NoReturnType: + choices = ['a', 1, (2, 3)] + + def draw(self): + return random.choice(self.choices) + +class ReturnTypeSpecified(NoReturnType): + choices = ['a', 'b'] + + def draw(self) -> str: + return super().draw()