Skip to content

Commit

Permalink
Address the super violations in the codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
PCManticore authored and Pierre-Sassoulas committed May 5, 2020
1 parent e6c9ef5 commit 61e0e6b
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 36 deletions.
13 changes: 8 additions & 5 deletions pylint/checkers/refactoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,15 +751,18 @@ def _check_quit_exit_call(self, node):
def _check_super_with_arguments(self, node):
if not isinstance(node.func, astroid.Name) or node.func.name != "super":
return
if len(node.args) != 2:
return
if not isinstance(node.args[1], astroid.Name) or node.args[1].name != "self":
return

# pylint: disable=too-many-boolean-expressions
if (
not isinstance(node.args[1], astroid.Name)
len(node.args) != 2
or not isinstance(node.args[1], astroid.Name)
or node.args[1].name != "self"
or not isinstance(node.args[0], astroid.Name)
or not isinstance(node.args[1], astroid.Name)
or node.args[0].name != node_frame_class(node).name
):
return

self.add_message("super-with-arguments", node=node)

def _check_raising_stopiteration_in_generator_next_call(self, node):
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/a/access_to_protected_members.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Subclass(MyClass):

def __init__(self):
MyClass._protected = 5
super(Subclass, self)._private_method()
super()._private_method()

INST = Subclass()
INST.attr = 1
Expand Down
12 changes: 6 additions & 6 deletions tests/functional/a/arguments_differ.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Sub(Super):

# pylint: disable=unused-argument
def __init__(self, arg):
super(Sub, self).__init__()
super().__init__()

def __private(self, arg):
pass
Expand Down Expand Up @@ -142,7 +142,7 @@ def close(self, attr):
class StaticmethodChild2(Staticmethod):

def func(self, data):
super(StaticmethodChild2, self).func(data)
super().func(data)


class SuperClass(object):
Expand All @@ -158,7 +158,7 @@ def impl(self, *args, **kwargs):
"""
Acceptable use of vararg in subclass because it does not violate LSP.
"""
super(MyClass, self).impl(*args, **kwargs)
super().impl(*args, **kwargs)


class FirstHasArgs(object):
Expand All @@ -185,7 +185,7 @@ def test(self, *args):
"""
Acceptable use of vararg in subclass because it does not violate LSP.
"""
super(PositionalChild, self).test(args[0], args[1])
super().test(args[0], args[1])

class Mixed(object):

Expand All @@ -199,7 +199,7 @@ def mixed(self, first, *args, **kwargs):
"""
Acceptable use of vararg in subclass because it does not violate LSP.
"""
super(MixedChild1, self).mixed(first, *args, **kwargs)
super().mixed(first, *args, **kwargs)


class MixedChild2(Mixed):
Expand All @@ -208,7 +208,7 @@ def mixed(self, first, *args, third, **kwargs):
"""
Acceptable use of vararg in subclass because it does not violate LSP.
"""
super(MixedChild2, self).mixed(first, *args, third, **kwargs)
super().mixed(first, *args, third, **kwargs)


class HasSpecialMethod(object):
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/a/assigning_non_slot.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self):
self.component = 42
self.member = 24
self.missing = 42 # [assigning-non-slot]
super(Bad3, self).__init__()
super().__init__()

class Good(Empty):
""" missing not in slots, but Empty doesn't
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/d/deprecated_methods_py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SuperCrash(unittest.TestCase):

def __init__(self):
# should not crash.
super(SuperCrash, self)()
super()()

xml.etree.ElementTree.iterparse(None)

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/d/deprecated_methods_py38.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SuperCrash(unittest.TestCase):

def __init__(self):
# should not crash.
super(SuperCrash, self)()
super()()

xml.etree.ElementTree.iterparse(None)

Expand Down
4 changes: 2 additions & 2 deletions tests/functional/i/init_not_called.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ def __init__(self): # [super-init-not-called]
class NewStyleA(object):
"""new style class"""
def __init__(self):
super(NewStyleA, self).__init__()
super().__init__()
print('init', self)

class NewStyleB(NewStyleA):
"""derived new style class"""
def __init__(self):
super(NewStyleB, self).__init__()
super().__init__()

class NoInit(object):
"""No __init__ defined"""
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/m/member_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_no_false_positives(self):
none = None
print(none.whatever)
# No misssing in the parents.
super(Client, self).misssing() # [no-member]
super().misssing() # [no-member]


class Mixin(object):
Expand Down Expand Up @@ -129,7 +129,7 @@ def __getattribute__(self, attr):
class SuperChecks(str, str): # pylint: disable=duplicate-bases
"""Don't fail when the MRO is invalid."""
def test(self):
super(SuperChecks, self).lalala()
super().lalala()

type(Client()).ala # [no-member]
type({}).bala # [no-member]
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/m/member_checks_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self):
class Child(Parent):

def __init__(self):
super(Child, self).__init__()
super().__init__()

self._similar # [no-member]
self._really_similar # [no-member]
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/m/member_checks_no_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self):
class Child(Parent):

def __init__(self):
super(Child, self).__init__()
super().__init__()

self._similar # [no-member]
self._really_similar # [no-member]
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/m/method_hidden.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ def one(self):


class JsonEncoder(js.JSONEncoder):
# pylint: disable=useless-super-delegation
# pylint: disable=useless-super-delegation,super-with-arguments
def default(self, o):
return super(JsonEncoder, self).default(o)
2 changes: 1 addition & 1 deletion tests/functional/n/name_styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class DerivedFromCorrect(CorrectClassName):
zz = 'Now a good class attribute'

def __init__(self):
super(DerivedFromCorrect, self).__init__()
super().__init__()
self._Bad_AtTR_name = None # Ignored

def BadMethodName(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/r/regression_property_no_member_844.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def thing(self):
class Child(Parent):
@Parent.thing.getter
def thing(self):
return super(Child, self).thing + '!'
return super().thing + '!'


print(Child().thing)
2 changes: 1 addition & 1 deletion tests/functional/s/super_checks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pylint: disable=too-few-public-methods,import-error, no-absolute-import,missing-docstring, useless-object-inheritance
# pylint: disable=useless-super-delegation,wrong-import-position,invalid-name, wrong-import-order

# pylint: disable=super-with-arguments
from unknown import Missing

class Aaaa:
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/s/super_with_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ def __init__(self):
class NotSuperCall(Foo):
def __init__(self):
super.test(Bar, self).__init__()


class InvalidSuperCall(Foo):
def __init__(self):
super(InvalidSuperCall.__class__, self).__init__()
12 changes: 6 additions & 6 deletions tests/functional/u/useless_super_delegation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# pylint: disable=missing-docstring, no-member, no-self-use, bad-super-call
# pylint: disable=too-few-public-methods, unused-argument, invalid-name, too-many-public-methods
# pylint: disable=line-too-long, useless-object-inheritance, arguments-out-of-order

# pylint: disable=super-with-arguments

def not_a_method(param, param2):
return super(None, None).not_a_method(param, param2)
Expand Down Expand Up @@ -50,16 +50,16 @@ def with_default_argument_tuple(self, first, default_arg=()):
pass

def with_default_arg_ter(self, first, default_arg="has_been_changed"):
super(Base, self).with_default_arg_ter(first, default_arg)
super().with_default_arg_ter(first, default_arg)

def with_default_arg_quad(self, first, default_arg="has_been_changed"):
super(Base, self).with_default_arg_quad(first, default_arg)
super().with_default_arg_quad(first, default_arg)

class NotUselessSuper(Base):

def multiple_statements(self):
first = 42 * 24
return super(NotUselessSuper, self).multiple_statements() + first
return super().multiple_statements() + first

def not_a_call(self):
return 1 + 2
Expand All @@ -68,7 +68,7 @@ def not_super_call(self):
return type(self).__class__

def not_super_attribute_access(self):
return super(NotUselessSuper, self)
return super()

def invalid_super_call(self):
return super(NotUselessSuper, 1).invalid_super_call()
Expand All @@ -77,7 +77,7 @@ def other_invalid_super_call(self):
return super(2, 3, 4, 5).other_invalid_super_call()

def different_name(self):
return super(NotUselessSuper, self).something()
return super().something()

def different_super_mro_pointer(self):
return super(Base, self).different_super_mro_pointer()
Expand Down
6 changes: 3 additions & 3 deletions tests/functional/u/useless_super_delegation_py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
class NotUselessSuper(object):

def not_passing_keyword_only(self, first, *, second):
return super(NotUselessSuper, self).not_passing_keyword_only(first)
return super().not_passing_keyword_only(first)

def passing_keyword_only_with_modifications(self, first, *, second):
return super(NotUselessSuper, self).passing_keyword_only_with_modifications(
return super().passing_keyword_only_with_modifications(
first, second + 1)


Expand All @@ -19,7 +19,7 @@ def not_passing_keyword_only(self, first, *, second="second"):
class UselessSuper(object):

def useless(self, *, first): # [useless-super-delegation]
super(UselessSuper, self).useless(first=first)
super().useless(first=first)


class Egg():
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/u/useless_super_delegation_py35.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
class NotUselessSuper(object):

def not_passing_all_params(self, first, *args, second=None, **kwargs):
return super(NotUselessSuper, self).not_passing_all_params(*args, second, **kwargs)
return super().not_passing_all_params(*args, second, **kwargs)


class UselessSuper(object):

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

0 comments on commit 61e0e6b

Please sign in to comment.