Skip to content

Commit

Permalink
Issue adamchainz#503 - add rule for useless generator pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Karine-Bauch committed Jul 21, 2023
1 parent 1f4af99 commit b0f2085
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/flake8_comprehensions/__init__.py
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 @@ -199,6 +200,20 @@ def run(self) -> Generator[tuple[int, int, str, type[Any]], None, None]:
type(self),
)

elif (
num_positional_args == 1
and node.func.id == "print"
and isinstance(node.args[0], ast.Call)
and isinstance(node.args[0].func, ast.Name)
and node.args[0].func.id == "sorted"
):
yield (
node.lineno,
node.col_offset,
self.messages["C412"],
type(self),
)

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

0 comments on commit b0f2085

Please sign in to comment.