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

Fix FURB183 false positive when using custom formatter #315

Merged
merged 1 commit into from
Dec 18, 2023
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
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}}"