Skip to content

Commit

Permalink
Fix FURB183 false positive when using custom formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
dosisod committed Dec 18, 2023
1 parent 435963c commit 4994129
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 22 additions & 1 deletion refurb/checks/readability/use_str_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from refurb.checks.common import stringify
from refurb.checks.string.use_fstring_fmt import CONVERSIONS as FURB_119_FUNCS
from refurb.error import Error
from refurb.visitor import TraverserVisitor


@dataclass
Expand Down Expand Up @@ -36,6 +37,14 @@ class ErrorInfo(Error):
ignore = set[int]()


# TODO: add support for returning False from check to indicate it shouldnt prapogate
class NestedFstringIgnorer(TraverserVisitor):
def visit_call_expr(self, o: CallExpr) -> None:
ignore.add(id(o))

super().visit_call_expr(o)


def check(node: CallExpr, errors: list[Error]) -> None:
if id(node) in ignore:
return
Expand All @@ -48,7 +57,10 @@ def check(node: CallExpr, errors: list[Error]) -> None:
),
args=[ListExpr(items=items)],
):
ignore.update(id(item) for item in items)
visitor = NestedFstringIgnorer()

for item in items:
visitor.accept(item)

case CallExpr(
callee=MemberExpr(
Expand All @@ -66,3 +78,12 @@ def check(node: CallExpr, errors: list[Error]) -> None:
msg = f'Replace `f"{{{x}}}"` with `str({x})`'

errors.append(ErrorInfo.from_node(node, msg))

case CallExpr(
callee=MemberExpr(
expr=StrExpr(value="{:{}}"),
name="format",
),
args=[_, arg],
):
NestedFstringIgnorer().accept(arg)
2 changes: 2 additions & 0 deletions test/data/err_183.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@

f"hello{x}world"
f"{x} {x}"

f"{x:{x}}"

0 comments on commit 4994129

Please sign in to comment.