Skip to content

Commit

Permalink
Fix bug with calling different superclass method
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Sep 25, 2021
1 parent 17cb81f commit 5ad1d1e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pyupgrade/_plugins/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from pyupgrade._token_helpers import parse_call_args

FUNC_TYPES = (ast.Lambda, ast.FunctionDef, ast.AsyncFunctionDef)
NON_LAMBDA_FUNC_TYPES = (ast.FunctionDef, ast.AsyncFunctionDef)


def _fix_yield(i: int, tokens: List[Token]) -> None:
Expand Down Expand Up @@ -132,16 +133,18 @@ def visit_Call(self, node: ast.Call) -> None:
):
self.super_offsets.add(ast_to_offset(node))
elif (
# base.funcname(funcarg1, ...)
isinstance(node.func, ast.Attribute) and
len(node.args) >= 1 and
isinstance(node.args[0], ast.Name) and
len(self._scopes) >= 2 and
# last stack is a function whose first argument is the first
# argument of this function
len(node.args) >= 1 and
isinstance(node.args[0], ast.Name) and
isinstance(self._scopes[-1].node, FUNC_TYPES) and
isinstance(self._scopes[-1].node, NON_LAMBDA_FUNC_TYPES) and
node.func.attr == self._scopes[-1].node.name and
len(self._scopes[-1].node.args.args) >= 1 and
node.args[0].id == self._scopes[-1].node.args.args[0].arg and
# the function is an attribute of the contained class name
isinstance(node.func, ast.Attribute) and
isinstance(self._scopes[-2].node, ast.ClassDef) and
len(self._scopes[-2].node.bases) == 1 and
_is_simple_base(self._scopes[-2].node.bases[0]) and
Expand Down
9 changes: 9 additions & 0 deletions tests/features/super_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ def test_fix_super(s, expected):
' a().b.f(self)\n',
id='non simple attribute base',
),
pytest.param(
'class C:\n'
' @classmethod\n'
' def make(cls, instance):\n'
' ...\n'
'class D(C):\n'
' def find(self):\n'
' return C.make(self)\n',
),
),
)
def test_old_style_class_super_noop(s):
Expand Down

0 comments on commit 5ad1d1e

Please sign in to comment.