Skip to content

Commit

Permalink
properly handle literals in unnecessary-comprehension checker, cl…
Browse files Browse the repository at this point in the history
…oses #3148
  • Loading branch information
RemiCardona authored and PCManticore committed Oct 3, 2019
1 parent 252efa1 commit 0094ab6
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTORS.txt
Expand Up @@ -329,3 +329,5 @@ contributors:

* Trevor Bekolay: contributor
- Added --list-msgs-enabled command

* Rémi Cardona: contributor
4 changes: 3 additions & 1 deletion pylint/checkers/refactoring.py
Expand Up @@ -1050,7 +1050,9 @@ def _check_unnecessary_comprehension(self, node):
if isinstance(expr, astroid.Name):
expr_list = expr.name
elif isinstance(expr, astroid.Tuple):
expr_list = [elt.name for elt in expr.elts if isinstance(elt, astroid.Name)]
if any(not isinstance(elt, astroid.Name) for elt in expr.elts):
return
expr_list = [elt.name for elt in expr.elts]
else:
expr_list = []
target = node.parent.generators[0].target
Expand Down
1 change: 1 addition & 0 deletions tests/functional/u/unnecessary_comprehension.py
Expand Up @@ -11,6 +11,7 @@
[x for x in iterable if condition] # exclude comp_if
[y for x in iterable for y in x] # exclude nested comprehensions
[2 * x for x in iterable] # exclude useful comprehensions
[(x, y, 1) for x, y in iterable] # exclude useful comprehensions

# Set comprehensions
{x for x in iterable} # [unnecessary-comprehension]
Expand Down
8 changes: 4 additions & 4 deletions tests/functional/u/unnecessary_comprehension.txt
@@ -1,8 +1,8 @@
unnecessary-comprehension:5::Unnecessary use of a comprehension
unnecessary-comprehension:8::Unnecessary use of a comprehension
unnecessary-comprehension:9::Unnecessary use of a comprehension
unnecessary-comprehension:16::Unnecessary use of a comprehension
unnecessary-comprehension:19::Unnecessary use of a comprehension
unnecessary-comprehension:17::Unnecessary use of a comprehension
unnecessary-comprehension:20::Unnecessary use of a comprehension
unnecessary-comprehension:28::Unnecessary use of a comprehension
unnecessary-comprehension:30::Unnecessary use of a comprehension
unnecessary-comprehension:21::Unnecessary use of a comprehension
unnecessary-comprehension:29::Unnecessary use of a comprehension
unnecessary-comprehension:31::Unnecessary use of a comprehension

0 comments on commit 0094ab6

Please sign in to comment.