Skip to content

Commit

Permalink
Rename the new old-style super with super-with-arguments
Browse files Browse the repository at this point in the history
Also move it from the Python 3 checker to the refactoring one, as it's a
better fit for it.
  • Loading branch information
PCManticore authored and Pierre-Sassoulas committed May 5, 2020
1 parent b170461 commit e6c9ef5
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 62 deletions.
9 changes: 5 additions & 4 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ Release date: TBA

* bad-continuation and bad-whitespace have been removed, black or another formatter can help you with this better than Pylint

Close #246, #289, #638, #747, #1148, #1179, #1943, #2041, #2301, #2304, #2944, #3565
Close #246, #289, #638, #747, #1148, #1179, #1943, #2041, #2301, #2304, #2944, #3565

* The no-space-check option has been removed. It's no longer possible to consider empty line like a `trailing-whitespace` by using clever options

Close #1368
Close #1368

* mixed-indentation has been removed, it is no longer useful since TabError is included directly in python3

* Add `old-style-super` check for flagging instances of Python 2 style super calls.
Close #2984 #3573

* Add `super-with-arguments` check for flagging instances of Python 2 style super calls.

Close #2984 #3573

What's New in Pylint 2.5.1?
===========================
Expand Down
2 changes: 1 addition & 1 deletion doc/whatsnew/2.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Summary -- Release highlights
New checkers
============

* Add `old-style-super` check for flagging instances of Python 2 style super calls.
* Add `super-with-arguments` check for flagging instances of Python 2 style super calls.

Other Changes
=============
Expand Down
20 changes: 0 additions & 20 deletions pylint/checkers/python3.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,12 +598,6 @@ class Python3Checker(checkers.BaseChecker):
"variables will be deleted outside of the "
"comprehension.",
),
"C1601": (
"Consider using Python 3 style super() without arguments",
"old-style-super",
"Emitted when calling the super builtin with the current class "
"and instance. On Python 3 these arguments are the default.",
),
}

_bad_builtins = frozenset(
Expand Down Expand Up @@ -1245,20 +1239,6 @@ def visit_call(self, node):
if kwarg.arg == "encoding":
self._validate_encoding(kwarg.value, node)
break
elif node.func.name == "super":
if len(node.args) != 2:
return
if (
not isinstance(node.args[1], astroid.Name)
or node.args[1].name != "self"
):
return
if (
not isinstance(node.args[1], astroid.Name)
or node.args[0].name != node.scope().parent.name
):
return
self.add_message("old-style-super", node=node)

def _validate_encoding(self, encoding, node):
if isinstance(encoding, astroid.Const):
Expand Down
23 changes: 23 additions & 0 deletions pylint/checkers/refactoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from pylint import checkers, interfaces
from pylint import utils as lint_utils
from pylint.checkers import utils
from pylint.checkers.utils import node_frame_class

KNOWN_INFINITE_ITERATORS = {"itertools.count"}
BUILTIN_EXIT_FUNCS = frozenset(("quit", "exit"))
Expand Down Expand Up @@ -309,6 +310,12 @@ class RefactoringChecker(checkers.BaseTokenChecker):
"following a chain of ifs, all of them containing a "
"continue statement.",
),
"R1725": (
"Consider using Python 3 style super() without arguments",
"super-with-arguments",
"Emitted when calling the super() builtin with the current class "
"and instance. On Python 3 these arguments are the default and they can be omitted.",
),
}
options = (
(
Expand Down Expand Up @@ -715,11 +722,13 @@ def _check_consider_using_comprehension_constructor(self, node):
"consider-using-dict-comprehension",
"consider-using-set-comprehension",
"consider-using-sys-exit",
"super-with-arguments",
)
def visit_call(self, node):
self._check_raising_stopiteration_in_generator_next_call(node)
self._check_consider_using_comprehension_constructor(node)
self._check_quit_exit_call(node)
self._check_super_with_arguments(node)

@staticmethod
def _has_exit_in_scope(scope):
Expand All @@ -739,6 +748,20 @@ def _check_quit_exit_call(self, node):
return
self.add_message("consider-using-sys-exit", node=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
if (
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):
"""Check if a StopIteration exception is raised by the call to next function
Expand Down
34 changes: 0 additions & 34 deletions tests/checkers/unittest_python3.py
Original file line number Diff line number Diff line change
Expand Up @@ -1153,37 +1153,3 @@ def next(cls): #@
message = testutils.Message("next-method-defined", node=node)
with self.assertAddsMessages(message):
self.checker.visit_functiondef(node)

def test_old_style_super(self):
node = astroid.extract_node(
"""
class Foo(object):
def __init__():
super(Foo, self).__init__() #@
"""
).func.expr
message = testutils.Message("old-style-super", node=node)
with self.assertAddsMessages(message):
self.checker.visit_call(node)

def test_super_non_default_args(self):
node = astroid.extract_node(
"""
class Foo(object):
def __init__():
super(Bar, self).__init__() #@
"""
).func.expr
with self.assertNoMessages():
self.checker.visit_call(node)

def test_new_style_super(self):
node = astroid.extract_node(
"""
class Foo(object):
def __init__():
super().__init__() #@
"""
).func.expr
with self.assertNoMessages():
self.checker.visit_call(node)
1 change: 0 additions & 1 deletion tests/functional/s/super_style.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Foo:

class Bar(Foo):
def __init__(self):
super(Bar, self).__init__() # [old-style-super]
super(Bar, self).__init__() # [super-with-arguments]


class Baz(Foo):
Expand All @@ -15,3 +15,8 @@ def __init__(self):
class Qux(Foo):
def __init__(self):
super(Bar, self).__init__()


class NotSuperCall(Foo):
def __init__(self):
super.test(Bar, self).__init__()
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[Messages Control]
disable=all
enable=old-style-super
enable=super-with-arguments
1 change: 1 addition & 0 deletions tests/functional/s/super_with_arguments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
super-with-arguments:7:Bar.__init__:Consider using Python 3 style super() without arguments

0 comments on commit e6c9ef5

Please sign in to comment.