Skip to content

Commit

Permalink
Suppress useless-super-delegation if return type changed (#5822) (#…
Browse files Browse the repository at this point in the history
…6141)

* Reinstate warning if base method has no return type annotation
  • Loading branch information
timmartin authored and Pierre-Sassoulas committed Apr 20, 2022
1 parent 709fc58 commit cca0d02
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pylint/checkers/classes/class_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,14 @@ def form_annotations(arguments):
if called_annotations != overridden_annotations:
return

if (
function.returns is not None
and meth_node.returns is not None
and meth_node.returns.as_string() != function.returns.as_string()
):
# Override adds typing information to the return type
return

if _definition_equivalent_to_call(params, args):
self.add_message(
"useless-super-delegation", node=function, args=(function.name,)
Expand Down
34 changes: 34 additions & 0 deletions tests/functional/u/useless/useless_super_delegation_py35.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,37 @@ class UselessSuper(object):

def useless(self, first, *, second=None, **kwargs): # [useless-super-delegation]
return super().useless(first, second=second, **kwargs)

# pylint: disable=wrong-import-position
import random
from typing import Any

class ReturnTypeAny:
choices = ['a', 1, (2, 3)]

def draw(self) -> Any:
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: # [useless-super-delegation]
return super().draw()

class ReturnTypeSame(ReturnTypeAny):
choices = ['a', 'b']

def draw(self) -> Any: # [useless-super-delegation]
return super().draw()
2 changes: 2 additions & 0 deletions tests/functional/u/useless/useless_super_delegation_py35.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
useless-super-delegation:11:4:11:15:UselessSuper.useless:Useless super delegation in method 'useless':UNDEFINED
useless-super-delegation:39:4:39:12:ReturnTypeSpecified.draw:Useless super delegation in method 'draw':UNDEFINED
useless-super-delegation:45:4:45:12:ReturnTypeSame.draw:Useless super delegation in method 'draw':UNDEFINED

0 comments on commit cca0d02

Please sign in to comment.