-
Notifications
You must be signed in to change notification settings - Fork 10
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
add rule prefer-simpler-iterator
#90
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
from __future__ import annotations | ||
|
||
import ast | ||
import string | ||
from collections.abc import Iterable | ||
from typing import NamedTuple | ||
|
||
from typing_extensions import TypeGuard | ||
|
||
from flake8_pie.base import Error | ||
|
||
KNOWN_FUNCTIONS = {"items"} | ||
|
||
|
||
def is_name_list(val: Iterable[ast.expr]) -> TypeGuard[Iterable[ast.Name]]: | ||
return all(isinstance(x, ast.Name) for x in val) | ||
|
||
|
||
class UsedVarRes(NamedTuple): | ||
method: str | ||
var: str | ||
|
||
|
||
def get_used_var(vars: Iterable[ast.Name]) -> UsedVarRes | None: | ||
for idx, var in enumerate(vars): | ||
if not var.id.startswith("_"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should do proper usage tracking to avoid false positives There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pyflakes implement this usage tracking: https://github.com/PyCQA/pyflakes/blob/da197ee94e791640d82940396c19d2d82ca8defb/pyflakes/checker.py#L1207 |
||
if idx == 0: | ||
method = "keys" | ||
elif idx == 1: | ||
method = "values" | ||
else: | ||
raise ValueError(f"Unexpected index: {idx}") | ||
|
||
return UsedVarRes(method=method, var=var.id) | ||
return None | ||
|
||
|
||
def has_unused_var(vars: Iterable[ast.Name]) -> bool: | ||
return any(x.id.startswith("_") for x in vars) | ||
|
||
|
||
def pie805_prefer_simple_iterator_for(node: ast.For, errors: list[Error]) -> None: | ||
if ( | ||
isinstance(node.target, ast.Tuple) | ||
and len(node.target.elts) == 2 | ||
and is_name_list(node.target.elts) | ||
and has_unused_var(node.target.elts) | ||
): | ||
res = get_used_var(node.target.elts) | ||
if res is None: | ||
return | ||
|
||
if ( | ||
isinstance(node.iter, ast.Call) | ||
and isinstance(node.iter.func, ast.Attribute) | ||
and node.iter.func.attr == "items" | ||
): | ||
errors.append( | ||
PIE805( | ||
lineno=node.lineno, | ||
col_offset=node.col_offset, | ||
suggestion=f"use `for {res.var} in foo.{res.method}()`", | ||
) | ||
) | ||
|
||
if ( | ||
isinstance(node.iter, ast.Call) | ||
and isinstance(node.iter.func, ast.Name) | ||
and node.iter.func.id == "enumerate" | ||
): | ||
errors.append( | ||
PIE805( | ||
lineno=node.lineno, | ||
col_offset=node.col_offset, | ||
suggestion=f"use `for {res.var} in enumerate(...)`", | ||
) | ||
) | ||
|
||
|
||
def pie805_prefer_simple_iterator_generator( | ||
node: ast.GeneratorExp | ast.ListComp, errors: list[Error] | ||
) -> None: | ||
for comprehension in node.generators: | ||
if ( | ||
isinstance(comprehension.iter, ast.Call) | ||
and isinstance(comprehension.iter.func, ast.Attribute) | ||
and comprehension.iter.func.attr == "items" | ||
and isinstance(comprehension.target, ast.Tuple) | ||
and len(comprehension.target.elts) == 2 | ||
and is_name_list(comprehension.target.elts) | ||
and has_unused_var(comprehension.target.elts) | ||
): | ||
res = get_used_var(comprehension.target.elts) | ||
if res is None: | ||
continue | ||
errors.append( | ||
PIE805( | ||
lineno=node.lineno, | ||
col_offset=node.col_offset, | ||
suggestion=f"use `for {res.var} in foo.{res.method}()`", | ||
) | ||
) | ||
|
||
|
||
def PIE805(lineno: int, col_offset: int, suggestion: str) -> Error: | ||
return Error( | ||
lineno=lineno, | ||
col_offset=col_offset, | ||
message=f"PIE805: prefer-simple-iterator: {suggestion}", | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from __future__ import annotations | ||
|
||
import ast | ||
|
||
import pytest | ||
|
||
from flake8_pie import Flake8PieCheck | ||
from flake8_pie.base import Error | ||
from flake8_pie.pie805_prefer_simple_iterator import PIE805 | ||
from flake8_pie.tests.utils import ex, to_errors | ||
|
||
EXAMPLES = [ | ||
ex( | ||
code=""" | ||
for _idx, foo in enumerate(bar): | ||
... | ||
""", | ||
errors=[ | ||
PIE805(lineno=2, col_offset=0, suggestion="use `for foo in enumerate(...)`") | ||
], | ||
), | ||
ex( | ||
code=""" | ||
for _key, value in foo.items(): | ||
... | ||
""", | ||
errors=[ | ||
PIE805(lineno=2, col_offset=0, suggestion="use `for value in foo.values()`") | ||
], | ||
), | ||
ex( | ||
code=""" | ||
for key, _value in foo.items(): | ||
... | ||
""", | ||
errors=[ | ||
PIE805(lineno=2, col_offset=0, suggestion="use `for key in foo.keys()`") | ||
], | ||
), | ||
ex( | ||
code=""" | ||
fields = [ | ||
k for k, _v in serialize(user).fields.items() if k != "internal" | ||
] | ||
""", | ||
errors=[PIE805(lineno=3, col_offset=4, suggestion="use `for k in foo.keys()`")], | ||
), | ||
ex( | ||
code=""" | ||
users = ( | ||
User(data) | ||
for _id, data in user_map.items() | ||
for f, _y in blah.items() | ||
) | ||
""", | ||
errors=[ | ||
PIE805(lineno=3, col_offset=4, suggestion="use `for data in foo.values()`"), | ||
PIE805(lineno=3, col_offset=4, suggestion="use `for f in foo.keys()`"), | ||
], | ||
), | ||
# we need to do usage analysis on the variables to determine if they are actually unused instead of looking at `_`. | ||
ex( | ||
code=""" | ||
[dict(_name=_name, data=data) for _name, data in users.items()] | ||
""", | ||
errors=[ | ||
PIE805(lineno=2, col_offset=1, suggestion="use `for data in foo.values()`") | ||
], | ||
), | ||
ex( | ||
code=""" | ||
for key, value in foo.items(): | ||
... | ||
for idx, foo in enumerate(bar): | ||
... | ||
[k for k, v in users.items() if v == "guest"] | ||
""", | ||
errors=[], | ||
), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("code,errors", EXAMPLES) | ||
def test_examples(code: str, errors: list[Error]) -> None: | ||
expr = ast.parse(code) | ||
assert to_errors(Flake8PieCheck(expr, filename="foo.py").run()) == errors |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "flake8-pie" | ||
version = "0.13.0" | ||
version = "0.14.0" | ||
description = "A flake8 extension that implements misc. lints" | ||
repository = "https://github.com/sbdchd/flake8-pie" | ||
authors = ["Steve Dignam <[email protected]>"] | ||
|
@@ -29,9 +29,10 @@ twine = "^1.12" | |
pytest-watch = "^4.2" | ||
pytest = "^4.0" | ||
ipython = "^7.2" | ||
mypy = "^0.812.0" | ||
mypy = "^0.900" | ||
astpretty = "^2.1" | ||
isort = "^4.3" | ||
typing_extensions = "^3.10.0.0" | ||
|
||
[tool.poetry.plugins] | ||
[tool.poetry.plugins."flake8.extension"] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unused?