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

Issue detect useless generator pattern #520

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
/.tox
/build/
/dist/
venv
```
18 changes: 17 additions & 1 deletion src/flake8_comprehensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(self, tree: ast.AST) -> None:
"C419 Unnecessary list comprehension passed to {func}() prevents "
+ "short-circuiting - rewrite as a generator."
),
"C420": "C420 Unnecessary generator - remove it or rewrite with as iterator",
}

def run(self) -> Generator[tuple[int, int, str, type[Any]], None, None]:
Expand Down Expand Up @@ -73,7 +74,7 @@ def run(self) -> Generator[tuple[int, int, str, type[Any]], None, None]:
elif (
num_positional_args == 1
and node.func.id == "dict"
and len(node.keywords) == 0
and num_keyword_args == 0
and isinstance(node.args[0], (ast.GeneratorExp, ast.ListComp))
and isinstance(node.args[0].elt, ast.Tuple)
and len(node.args[0].elt.elts) == 2
Expand Down Expand Up @@ -199,6 +200,21 @@ def run(self) -> Generator[tuple[int, int, str, type[Any]], None, None]:
type(self),
)

elif (
node.func.id == "sorted"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the original issue:

the (a for a in b) generator construct doesn't seem particularly useful and just adds an unnecessary context switch of the generator.

I think we can check every generator, not only those inside sorted call.
This mean that the check would be only the isinstance of node as ast.GeneratorExp and isinstance of node.elt as ast.Name. What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be right.
We should verify if the Generator do something, in all cases, not only for sorted call.
I need to put this on my desktop : KISS 👍

and num_positional_args > 0
and (
isinstance(node.args[0], ast.GeneratorExp)
and isinstance(node.args[0].elt, ast.Name)
)
Karine-Bauch marked this conversation as resolved.
Show resolved Hide resolved
):
yield (
node.lineno,
node.col_offset,
self.messages["C420"],
type(self),
)

elif (
node.func.id in {"list", "reversed"}
and num_positional_args > 0
Expand Down