Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 3148 #3149

Merged
merged 2 commits into from Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
17 changes: 8 additions & 9 deletions pylint/checkers/refactoring.py
Expand Up @@ -1047,15 +1047,14 @@ def _check_unnecessary_comprehension(self, node):

elif isinstance(node.parent, (astroid.ListComp, astroid.SetComp)):
expr = node.parent.elt
expr_list = (
expr.name
if isinstance(expr, astroid.Name)
else (
[elt.name for elt in expr.elts if isinstance(elt, astroid.Name)]
if isinstance(expr, astroid.Tuple)
else []
)
)
if isinstance(expr, astroid.Name):
expr_list = expr.name
elif isinstance(expr, astroid.Tuple):
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
target_list = (
target.name
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