diff options
author | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-06-26 11:03:10 +0300 |
---|---|---|
committer | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-06-26 11:03:10 +0300 |
commit | 84c8b402e9a85989cc5f89febbecb3bf83f73994 (patch) | |
tree | 618fe6a2933980eb30cdcd1f5696f0fdda588e1d | |
parent | dde7afa51359b0a51b0a336a3dfbce4da0d719ee (diff) | |
download | pylint-84c8b402e9a85989cc5f89febbecb3bf83f73994.tar.gz |
Add a new error, 'star-needs-assignment-target'.
This error is emitted on Python 3 when a Starred expression (*x) is
not used in an assignment target. This is not caught when parsing the
AST on Python 3, so it needs to be a separate check.
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | pylint/checkers/base.py | 19 | ||||
-rw-r--r-- | pylint/test/functional/star_needs_assignment_target.py | 4 | ||||
-rw-r--r-- | pylint/test/functional/star_needs_assignment_target.rc | 2 | ||||
-rw-r--r-- | pylint/test/functional/star_needs_assignment_target.txt | 1 |
5 files changed, 28 insertions, 2 deletions
@@ -192,6 +192,10 @@ ChangeLog for Pylint which is used when a star import is detected in another scope than the module level, which is an error on Python 3. Using this will emit a SyntaxWarning on Python 2. + + * Add a new error, 'star-needs-assignment-target', emitted on Python 3 when + a Starred expression (*x) is not used in an assignment target. This is not + caught when parsing the AST on Python 3, so it needs to be a separate check. diff --git a/pylint/checkers/base.py b/pylint/checkers/base.py index 75f8ad7..e70f428 100644 --- a/pylint/checkers/base.py +++ b/pylint/checkers/base.py @@ -317,6 +317,11 @@ class BasicErrorChecker(_BasicChecker): 'Emitted when a star expression is used as a starred ' 'assignment target.', {'minversion': (3, 0)}), + 'E0114': ('Can use starred expression only in assignment target', + 'star-needs-assignment-target', + 'Emitted when a star expression is not used in an ' + 'assignment target.', + {'minversion': (3, 0)}), } @check_messages('function-redefined') @@ -326,14 +331,24 @@ class BasicErrorChecker(_BasicChecker): @check_messages('too-many-star-expressions', 'invalid-star-assignment-target') def visit_assign(self, node): - starred = node.targets[0].nodes_of_class(astroid.Starred) - if len(list(starred)) > 1: + starred = list(node.targets[0].nodes_of_class(astroid.Starred)) + if len(starred) > 1: self.add_message('too-many-star-expressions', node=node) # Check *a = b if isinstance(node.targets[0], astroid.Starred): self.add_message('invalid-star-assignment-target', node=node) + @check_messages('star-needs-assignment-target') + def visit_starred(self, node): + """Check that a Starred expression is used in an assignment target.""" + stmt = node.statement() + if not isinstance(stmt, astroid.Assign): + return + + if stmt.value is node or stmt.value.parent_of(node): + self.add_message('star-needs-assignment-target', node=node) + @check_messages('init-is-generator', 'return-in-init', 'function-redefined', 'return-arg-in-generator', 'duplicate-argument-name') diff --git a/pylint/test/functional/star_needs_assignment_target.py b/pylint/test/functional/star_needs_assignment_target.py new file mode 100644 index 0000000..5421c22 --- /dev/null +++ b/pylint/test/functional/star_needs_assignment_target.py @@ -0,0 +1,4 @@ +"""Test for a = *b"""
+
+FIRST = *[1, 2] # [star-needs-assignment-target]
+*THIRD, FOURTH = [1, 2, 3,]
diff --git a/pylint/test/functional/star_needs_assignment_target.rc b/pylint/test/functional/star_needs_assignment_target.rc new file mode 100644 index 0000000..c093be2 --- /dev/null +++ b/pylint/test/functional/star_needs_assignment_target.rc @@ -0,0 +1,2 @@ +[testoptions] +min_pyver=3.0 diff --git a/pylint/test/functional/star_needs_assignment_target.txt b/pylint/test/functional/star_needs_assignment_target.txt new file mode 100644 index 0000000..b66975c --- /dev/null +++ b/pylint/test/functional/star_needs_assignment_target.txt @@ -0,0 +1 @@ +star-needs-assignment-target:3::Can use starred expression only in assignment target
|