Skip to content

Commit

Permalink
Further improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dosisod committed Jan 11, 2024
1 parent dbad1ea commit 585c8a2
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 29 deletions.
15 changes: 14 additions & 1 deletion refurb/checks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,16 @@ def _stringify(node: Node) -> str:

return f"{name}({args})"

case IndexExpr(base=base, index=index):
return f"{stringify(base)}[{stringify(index)}]"

case SliceExpr(begin_index=begin_index, end_index=end_index, stride=stride):
begin = stringify(begin_index) if begin_index else ""
end = stringify(end_index) if end_index else ""
stride = f":{stringify(stride)}" if stride else "" # type: ignore[assignment]

return f"{begin}:{end}{stride}"

case OpExpr(left=left, op=op, right=right):
return f"{_stringify(left)} {op} {_stringify(right)}"

Expand All @@ -352,7 +362,10 @@ def _stringify(node: Node) -> str:
return " ".join(parts)

case UnaryExpr(op=op, expr=expr):
return f"{op} {_stringify(expr)}"
if op not in "+-~":
op += " "

return f"{op}{_stringify(expr)}"

case LambdaExpr(
arg_names=arg_names,
Expand Down
13 changes: 6 additions & 7 deletions refurb/checks/pathlib/open.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from mypy.nodes import CallExpr, NameExpr, StrExpr

from refurb.checks.common import stringify
from refurb.checks.pathlib.util import is_pathlike
from refurb.error import Error

Expand Down Expand Up @@ -56,11 +57,9 @@ def check(node: CallExpr, errors: list[Error]) -> None:
mode = f'"{value}"'
args = f", {mode}"

expr = "x" if arg == node.args[0] else "str(x)"
inner = stringify(arg)
expr = inner if arg == node.args[0] else f"str({inner})"

errors.append(
ErrorInfo.from_node(
open_node,
f"Replace `open({expr}{args})` with `x.open({mode})`",
)
)
msg = f"Replace `open({expr}{args})` with `{inner}.open({mode})`"

errors.append(ErrorInfo.from_node(open_node, msg))
11 changes: 8 additions & 3 deletions refurb/checks/pathlib/with_suffix.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from mypy.nodes import CallExpr, IndexExpr, NameExpr, OpExpr, SliceExpr, StrExpr

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

from .util import is_pathlike
Expand Down Expand Up @@ -29,7 +30,6 @@ class ErrorInfo(Error):

name = "use-pathlib-with-suffix"
code = 100
msg: str = "Use `Path(x).with_suffix(y)` instead of slice and concat"
categories = ("pathlib",)


Expand All @@ -44,6 +44,11 @@ def check(node: OpExpr, errors: list[Error]) -> None:
),
index=SliceExpr(begin_index=None),
),
right=StrExpr(),
right=StrExpr() as suffix,
) if is_pathlike(arg):
errors.append(ErrorInfo.from_node(arg))
old = stringify(node)
new = f"Path({stringify(arg)}).with_suffix({suffix})"

msg = f"Replace `{old}` with `{new}`"

errors.append(ErrorInfo.from_node(arg, msg))
6 changes: 3 additions & 3 deletions test/data/err_100.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
test/data/err_100.py:6:9 [FURB100]: Use `Path(x).with_suffix(y)` instead of slice and concat
test/data/err_100.py:9:9 [FURB100]: Use `Path(x).with_suffix(y)` instead of slice and concat
test/data/err_100.py:11:9 [FURB100]: Use `Path(x).with_suffix(y)` instead of slice and concat
test/data/err_100.py:6:9 [FURB100]: Replace `str(Path("file.txt"))[:4] + ".pdf"` with `Path(Path("file.txt")).with_suffix(StrExpr(.pdf))`
test/data/err_100.py:9:9 [FURB100]: Replace `str(p)[:4] + ".pdf"` with `Path(p).with_suffix(StrExpr(.pdf))`
test/data/err_100.py:11:9 [FURB100]: Replace `str(pathlib.Path("file.txt"))[:4] + ".pdf"` with `Path(pathlib.Path("file.txt")).with_suffix(StrExpr(.pdf))`
14 changes: 7 additions & 7 deletions test/data/err_117.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
test/data/err_117.py:7:6 [FURB117]: Replace `open(str(x))` with `x.open()`
test/data/err_117.py:10:6 [FURB117]: Replace `open(str(x))` with `x.open()`
test/data/err_117.py:13:6 [FURB117]: Replace `open(x)` with `x.open()`
test/data/err_117.py:16:6 [FURB117]: Replace `open(x)` with `x.open()`
test/data/err_117.py:19:6 [FURB117]: Replace `open(str(x), "rb")` with `x.open("rb")`
test/data/err_117.py:22:6 [FURB117]: Replace `open(x, "rb")` with `x.open("rb")`
test/data/err_117.py:25:5 [FURB117]: Replace `open(str(x))` with `x.open()`
test/data/err_117.py:7:6 [FURB117]: Replace `open(str(path))` with `path.open()`
test/data/err_117.py:10:6 [FURB117]: Replace `open(str(Path("filename")))` with `Path("filename").open()`
test/data/err_117.py:13:6 [FURB117]: Replace `open(Path("filename"))` with `Path("filename").open()`
test/data/err_117.py:16:6 [FURB117]: Replace `open(path)` with `path.open()`
test/data/err_117.py:19:6 [FURB117]: Replace `open(str(path), "rb")` with `path.open("rb")`
test/data/err_117.py:22:6 [FURB117]: Replace `open(path, "rb")` with `path.open("rb")`
test/data/err_117.py:25:5 [FURB117]: Replace `open(str(path))` with `path.open()`
6 changes: 3 additions & 3 deletions test/data/err_118.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
lambda x, y: x >= y
lambda x, y: x > y

lambda x: ~ x
lambda x: - x
lambda x: ~x
lambda x: -x
lambda x: not x
lambda x: + x
lambda x: +x

def f(x, y):
return x + y
Expand Down
6 changes: 3 additions & 3 deletions test/data/err_118.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ test/data/err_118.py:21:1 [FURB118]: Replace `lambda x, y: x == y` with `operato
test/data/err_118.py:22:1 [FURB118]: Replace `lambda x, y: x != y` with `operator.ne`
test/data/err_118.py:23:1 [FURB118]: Replace `lambda x, y: x >= y` with `operator.ge`
test/data/err_118.py:24:1 [FURB118]: Replace `lambda x, y: x > y` with `operator.gt`
test/data/err_118.py:26:1 [FURB118]: Replace `lambda x: ~ x` with `operator.invert`
test/data/err_118.py:27:1 [FURB118]: Replace `lambda x: - x` with `operator.neg`
test/data/err_118.py:26:1 [FURB118]: Replace `lambda x: ~x` with `operator.invert`
test/data/err_118.py:27:1 [FURB118]: Replace `lambda x: -x` with `operator.neg`
test/data/err_118.py:28:1 [FURB118]: Replace `lambda x: not x` with `operator.not_`
test/data/err_118.py:29:1 [FURB118]: Replace `lambda x: + x` with `operator.pos`
test/data/err_118.py:29:1 [FURB118]: Replace `lambda x: +x` with `operator.pos`
test/data/err_118.py:31:1 [FURB118]: Replace function with `operator.add`
test/data/err_118.py:34:1 [FURB118]: Replace function with `operator.neg`
4 changes: 2 additions & 2 deletions test/data/pathlib.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
test/data/pathlib.py:7:6 [FURB117]: Replace `open(x)` with `x.open()`
test/data/pathlib.py:10:6 [FURB117]: Replace `open(x)` with `x.open()`
test/data/pathlib.py:7:6 [FURB117]: Replace `open(folder / "file.txt")` with `folder / "file.txt".open()`
test/data/pathlib.py:10:6 [FURB117]: Replace `open(folder / "another_folder" / "file.txt")` with `folder / "another_folder" / "file.txt".open()`
7 changes: 7 additions & 0 deletions test/data/stringify.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# test double star in dict expr
_ = dict({**{}})

# test different slice exprs
_ = list([""[0]])
_ = list([""[1:]])
_ = list([""[1:2]])
_ = list([""[1:2:-1]])
_ = list([""[::-1]])
5 changes: 5 additions & 0 deletions test/data/stringify.txt
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
test/data/stringify.py:2:5 [FURB123]: Replace `dict({**{}})` with `{**{}}.copy()`
test/data/stringify.py:5:5 [FURB123]: Replace `list([""[0]])` with `[""[0]].copy()`
test/data/stringify.py:6:5 [FURB123]: Replace `list([""[1:]])` with `[""[1:]].copy()`
test/data/stringify.py:7:5 [FURB123]: Replace `list([""[1:2]])` with `[""[1:2]].copy()`
test/data/stringify.py:8:5 [FURB123]: Replace `list([""[1:2:-1]])` with `[""[1:2:-1]].copy()`
test/data/stringify.py:9:5 [FURB123]: Replace `list([""[::-1]])` with `[""[::-1]].copy()`

0 comments on commit 585c8a2

Please sign in to comment.