Skip to content

Commit

Permalink
don't rewrite old-super for staticmethods
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Sep 27, 2021
1 parent a2f517f commit 76f6a74
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pyupgrade/_plugins/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import List
from typing import Set
from typing import Tuple
from typing import Union

from tokenize_rt import Offset
from tokenize_rt import Token
Expand All @@ -25,6 +26,7 @@

FUNC_TYPES = (ast.Lambda, ast.FunctionDef, ast.AsyncFunctionDef)
NON_LAMBDA_FUNC_TYPES = (ast.FunctionDef, ast.AsyncFunctionDef)
NonLambdaFuncTypes_T = Union[ast.FunctionDef, ast.AsyncFunctionDef]


def _fix_yield(i: int, tokens: List[Token]) -> None:
Expand All @@ -44,6 +46,14 @@ def _is_simple_base(base: ast.AST) -> bool:
)


def _is_staticmethod_decorated(node: NonLambdaFuncTypes_T) -> bool:
for decorator in node.decorator_list:
if isinstance(decorator, ast.Name) and decorator.id == 'staticmethod':
return True
else:
return False


class Scope:
def __init__(self, node: ast.AST) -> None:
self.node = node
Expand Down Expand Up @@ -127,6 +137,7 @@ def visit_Call(self, node: ast.Call) -> None:
isinstance(self._scopes[-1].node, NON_LAMBDA_FUNC_TYPES) and
node.func.attr == self._scopes[-1].node.name and
node.func.attr != '__new__' and
not _is_staticmethod_decorated(self._scopes[-1].node) 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
Expand Down
18 changes: 18 additions & 0 deletions tests/features/super_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ def test_fix_super(s, expected):
' return tuple.__new__(cls, (arg,))\n',
id='super() does not work properly for __new__',
),
pytest.param(
'class C(B):\n'
' @staticmethod\n'
' def f(arg):\n'
' return B.f(arg)\n',
id='skip staticmethod',
),
),
)
def test_old_style_class_super_noop(s):
Expand All @@ -207,6 +214,17 @@ def test_old_style_class_super_noop(s):
' super().f()\n'
' super().f(arg, arg)\n',
),
pytest.param(
'class C(B):\n'
' @classmethod\n'
' def f(cls):\n'
' B.f(cls)\n',
'class C(B):\n'
' @classmethod\n'
' def f(cls):\n'
' super().f()\n',
id='@classmethod',
),
pytest.param(
'class C(B):\n'
' def f(self, a):\n'
Expand Down

0 comments on commit 76f6a74

Please sign in to comment.