Skip to content

Commit

Permalink
Add set exprs to FURB171, improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dosisod committed Jan 16, 2024
1 parent 5dc8f20 commit 3bf4058
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
9 changes: 9 additions & 0 deletions refurb/checks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ def _stringify(node: Node) -> str:
case TupleExpr(items=items):
inner = ", ".join(stringify(x) for x in items)

if len(items) == 1:
# single element tuples need a trailing comma
inner += ","

return f"({inner})"

case CallExpr():
Expand Down Expand Up @@ -387,6 +391,11 @@ def _stringify(node: Node) -> str:

return f"[{inner}]"

case SetExpr(items=items):
inner = ", ".join(stringify(x) for x in items)

return f"{{{inner}}}"

raise ValueError


Expand Down
12 changes: 6 additions & 6 deletions refurb/checks/iterable/no_single_item_in.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from dataclasses import dataclass

from mypy.nodes import ComparisonExpr, ListExpr, TupleExpr
from mypy.nodes import ComparisonExpr, ListExpr, SetExpr, TupleExpr

from refurb.checks.common import stringify
from refurb.error import Error


Expand Down Expand Up @@ -34,13 +35,12 @@ def check(node: ComparisonExpr, errors: list[Error]) -> None:
match node:
case ComparisonExpr(
operators=["in" | "not in" as oper],
operands=[_, ListExpr() | TupleExpr() as expr],
operands=[lhs, ListExpr() | TupleExpr() | SetExpr() as expr],
) if len(expr.items) == 1:
new_oper = "==" if oper == "in" else "!="

if isinstance(expr, ListExpr):
msg = f"Replace `x {oper} [y]` with `x {new_oper} y`"
else:
msg = f"Replace `x {oper} (y,)` with `x {new_oper} y`"
new = f"{stringify(lhs)} {new_oper} {stringify(expr.items[0])}"

msg = f"Replace `{stringify(node)}` with `{new}`"

errors.append(ErrorInfo.from_node(node, msg))
2 changes: 2 additions & 0 deletions test/data/err_171.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

_ = 1 in (1,)
_ = 1 in [1] # noqa: FURB109
_ = 1 in {1}
_ = 1 not in (1,)
_ = 1 not in [1] # noqa: FURB109
_ = 1 not in {1} # noqa: FURB109


# these should not
Expand Down
10 changes: 6 additions & 4 deletions test/data/err_171.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
test/data/err_171.py:3:5 [FURB171]: Replace `x in (y,)` with `x == y`
test/data/err_171.py:4:5 [FURB171]: Replace `x in [y]` with `x == y`
test/data/err_171.py:5:5 [FURB171]: Replace `x not in (y,)` with `x != y`
test/data/err_171.py:6:5 [FURB171]: Replace `x not in [y]` with `x != y`
test/data/err_171.py:3:5 [FURB171]: Replace `1 in (1,)` with `1 == 1`
test/data/err_171.py:4:5 [FURB171]: Replace `1 in [1]` with `1 == 1`
test/data/err_171.py:5:5 [FURB171]: Replace `1 in {1}` with `1 == 1`
test/data/err_171.py:6:5 [FURB171]: Replace `1 not in (1,)` with `1 != 1`
test/data/err_171.py:7:5 [FURB171]: Replace `1 not in [1]` with `1 != 1`
test/data/err_171.py:8:5 [FURB171]: Replace `1 not in {1}` with `1 != 1`

0 comments on commit 3bf4058

Please sign in to comment.