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

Cleanup the "in tuple" check #49

Merged
merged 1 commit into from
Oct 5, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions refurb/checks/iterable/in_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
@dataclass
class ErrorInfo(Error):
"""
Since tuples cannot change value over time, it is more performant to use
them in `for` loops, generators, etc.:
Since tuple, list, and set literals can be used with the `in` operator, it
is best to pick one and stick with it.

Bad:

Expand All @@ -31,23 +31,28 @@ class ErrorInfo(Error):
"""

code = 109
msg: str = "Use `in (x, y, z)` instead of `in [x, y, z]`"


def error_msg(oper: str) -> str:
return f"Replace `{oper} [x, y, z]` with `{oper} (x, y, z)`"


def check(
node: ComparisonExpr | ForStmt | GeneratorExpr, errors: list[Error]
) -> None:
match node:
case (
ComparisonExpr(
operators=["in"],
operands=[_, ListExpr() as expr],
)
| ForStmt(expr=ListExpr() as expr)
case ComparisonExpr(
operators=["in" | "not in" as oper],
operands=[_, ListExpr() as expr],
):
errors.append(ErrorInfo(expr.line, expr.column))
errors.append(ErrorInfo(expr.line, expr.column, error_msg(oper)))

case ForStmt(expr=ListExpr() as expr):
errors.append(ErrorInfo(expr.line, expr.column, error_msg("in")))

case GeneratorExpr():
for expr in node.sequences: # type: ignore
if isinstance(expr, ListExpr):
errors.append(ErrorInfo(expr.line, expr.column))
errors.append(
ErrorInfo(expr.line, expr.column, error_msg("in"))
)
3 changes: 3 additions & 0 deletions test/data/err_109.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
if 1 in [1, 2, 3]:
pass

if 1 not in [1, 2, 3]:
pass


# these will not

Expand Down
13 changes: 7 additions & 6 deletions test/data/err_109.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
test/data/err_109.py:3:10 [FURB109]: Use `in (x, y, z)` instead of `in [x, y, z]`
test/data/err_109.py:6:13 [FURB109]: Use `in (x, y, z)` instead of `in [x, y, z]`
test/data/err_109.py:8:13 [FURB109]: Use `in (x, y, z)` instead of `in [x, y, z]`
test/data/err_109.py:11:22 [FURB109]: Use `in (x, y, z)` instead of `in [x, y, z]`
test/data/err_109.py:12:14 [FURB109]: Use `in (x, y, z)` instead of `in [x, y, z]`
test/data/err_109.py:15:9 [FURB109]: Use `in (x, y, z)` instead of `in [x, y, z]`
test/data/err_109.py:3:10 [FURB109]: Replace `in [x, y, z]` with `in (x, y, z)`
test/data/err_109.py:6:13 [FURB109]: Replace `in [x, y, z]` with `in (x, y, z)`
test/data/err_109.py:8:13 [FURB109]: Replace `in [x, y, z]` with `in (x, y, z)`
test/data/err_109.py:11:22 [FURB109]: Replace `in [x, y, z]` with `in (x, y, z)`
test/data/err_109.py:12:14 [FURB109]: Replace `in [x, y, z]` with `in (x, y, z)`
test/data/err_109.py:15:9 [FURB109]: Replace `in [x, y, z]` with `in (x, y, z)`
test/data/err_109.py:18:13 [FURB109]: Replace `not in [x, y, z]` with `not in (x, y, z)`