From 61e0e6b65476f083960ce5563745f337fb2454be Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 5 May 2020 08:45:09 +0200 Subject: [PATCH] Address the super violations in the codebase --- pylint/checkers/refactoring.py | 13 ++++++++----- tests/functional/a/access_to_protected_members.py | 2 +- tests/functional/a/arguments_differ.py | 12 ++++++------ tests/functional/a/assigning_non_slot.py | 2 +- tests/functional/d/deprecated_methods_py3.py | 2 +- tests/functional/d/deprecated_methods_py38.py | 2 +- tests/functional/i/init_not_called.py | 4 ++-- tests/functional/m/member_checks.py | 4 ++-- tests/functional/m/member_checks_hints.py | 2 +- tests/functional/m/member_checks_no_hints.py | 2 +- tests/functional/m/method_hidden.py | 2 +- tests/functional/n/name_styles.py | 2 +- .../r/regression_property_no_member_844.py | 2 +- tests/functional/s/super_checks.py | 2 +- tests/functional/s/super_with_arguments.py | 5 +++++ tests/functional/u/useless_super_delegation.py | 12 ++++++------ tests/functional/u/useless_super_delegation_py3.py | 6 +++--- tests/functional/u/useless_super_delegation_py35.py | 4 ++-- 18 files changed, 44 insertions(+), 36 deletions(-) diff --git a/pylint/checkers/refactoring.py b/pylint/checkers/refactoring.py index fbef4ded8c..72153269b2 100644 --- a/pylint/checkers/refactoring.py +++ b/pylint/checkers/refactoring.py @@ -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): diff --git a/tests/functional/a/access_to_protected_members.py b/tests/functional/a/access_to_protected_members.py index a88dee82b6..737d8b9d68 100644 --- a/tests/functional/a/access_to_protected_members.py +++ b/tests/functional/a/access_to_protected_members.py @@ -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 diff --git a/tests/functional/a/arguments_differ.py b/tests/functional/a/arguments_differ.py index ebe142cad7..eda0c326f3 100644 --- a/tests/functional/a/arguments_differ.py +++ b/tests/functional/a/arguments_differ.py @@ -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 @@ -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): @@ -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): @@ -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): @@ -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): @@ -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): diff --git a/tests/functional/a/assigning_non_slot.py b/tests/functional/a/assigning_non_slot.py index 6fd299f375..600a2d5a22 100644 --- a/tests/functional/a/assigning_non_slot.py +++ b/tests/functional/a/assigning_non_slot.py @@ -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 diff --git a/tests/functional/d/deprecated_methods_py3.py b/tests/functional/d/deprecated_methods_py3.py index d390c8d790..80428c79f3 100644 --- a/tests/functional/d/deprecated_methods_py3.py +++ b/tests/functional/d/deprecated_methods_py3.py @@ -32,7 +32,7 @@ class SuperCrash(unittest.TestCase): def __init__(self): # should not crash. - super(SuperCrash, self)() + super()() xml.etree.ElementTree.iterparse(None) diff --git a/tests/functional/d/deprecated_methods_py38.py b/tests/functional/d/deprecated_methods_py38.py index 4ca5e5197e..57047f9263 100644 --- a/tests/functional/d/deprecated_methods_py38.py +++ b/tests/functional/d/deprecated_methods_py38.py @@ -27,7 +27,7 @@ class SuperCrash(unittest.TestCase): def __init__(self): # should not crash. - super(SuperCrash, self)() + super()() xml.etree.ElementTree.iterparse(None) diff --git a/tests/functional/i/init_not_called.py b/tests/functional/i/init_not_called.py index 0e5aacbeae..9c9d24daee 100644 --- a/tests/functional/i/init_not_called.py +++ b/tests/functional/i/init_not_called.py @@ -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""" diff --git a/tests/functional/m/member_checks.py b/tests/functional/m/member_checks.py index ad7793fd51..8559926c06 100644 --- a/tests/functional/m/member_checks.py +++ b/tests/functional/m/member_checks.py @@ -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): @@ -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] diff --git a/tests/functional/m/member_checks_hints.py b/tests/functional/m/member_checks_hints.py index 407c522b5f..3925f4741e 100644 --- a/tests/functional/m/member_checks_hints.py +++ b/tests/functional/m/member_checks_hints.py @@ -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] diff --git a/tests/functional/m/member_checks_no_hints.py b/tests/functional/m/member_checks_no_hints.py index 407c522b5f..3925f4741e 100644 --- a/tests/functional/m/member_checks_no_hints.py +++ b/tests/functional/m/member_checks_no_hints.py @@ -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] diff --git a/tests/functional/m/method_hidden.py b/tests/functional/m/method_hidden.py index 7131ba21ac..47031d8d0f 100644 --- a/tests/functional/m/method_hidden.py +++ b/tests/functional/m/method_hidden.py @@ -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) diff --git a/tests/functional/n/name_styles.py b/tests/functional/n/name_styles.py index be526e68d9..6a3e9015a8 100644 --- a/tests/functional/n/name_styles.py +++ b/tests/functional/n/name_styles.py @@ -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): diff --git a/tests/functional/r/regression_property_no_member_844.py b/tests/functional/r/regression_property_no_member_844.py index 2c919fe2f4..759b7bfe3d 100644 --- a/tests/functional/r/regression_property_no_member_844.py +++ b/tests/functional/r/regression_property_no_member_844.py @@ -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) diff --git a/tests/functional/s/super_checks.py b/tests/functional/s/super_checks.py index 241531b374..3f132dff1f 100644 --- a/tests/functional/s/super_checks.py +++ b/tests/functional/s/super_checks.py @@ -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: diff --git a/tests/functional/s/super_with_arguments.py b/tests/functional/s/super_with_arguments.py index 8ad53ee649..ea8c0e4f1d 100644 --- a/tests/functional/s/super_with_arguments.py +++ b/tests/functional/s/super_with_arguments.py @@ -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__() diff --git a/tests/functional/u/useless_super_delegation.py b/tests/functional/u/useless_super_delegation.py index e6cf8680e7..5cf435b967 100644 --- a/tests/functional/u/useless_super_delegation.py +++ b/tests/functional/u/useless_super_delegation.py @@ -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) @@ -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 @@ -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() @@ -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() diff --git a/tests/functional/u/useless_super_delegation_py3.py b/tests/functional/u/useless_super_delegation_py3.py index 09bc25b4c1..b32ced873b 100644 --- a/tests/functional/u/useless_super_delegation_py3.py +++ b/tests/functional/u/useless_super_delegation_py3.py @@ -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) @@ -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(): diff --git a/tests/functional/u/useless_super_delegation_py35.py b/tests/functional/u/useless_super_delegation_py35.py index ffc3c6c74b..16030f8a4d 100644 --- a/tests/functional/u/useless_super_delegation_py35.py +++ b/tests/functional/u/useless_super_delegation_py35.py @@ -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)