Skip to content

Commit

Permalink
Fix newlines being printed in stringified strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dosisod committed Feb 29, 2024
1 parent 900f6b4 commit 60bb2f6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions refurb/checks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def _stringify(node: Node) -> str:
return str(value)

case StrExpr(value=value):
value = value.replace('"', r"\"")
value = repr(value)[1:-1].replace('"', r"\"")

return f'"{value}"'

Expand Down Expand Up @@ -402,7 +402,7 @@ def _stringify(node: Node) -> str:
if not is_format_arg:
assert isinstance(arg, StrExpr)

output += arg.value
output += _stringify(arg)[1:-1]

elif fmt:
output += f"{{{_stringify(arg)}:{fmt}}}"
Expand Down
3 changes: 2 additions & 1 deletion test/data/stringify.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
_ = str(f"x{123}y")
_ = str(f"x{123}y{456}z")
_ = str(f"{'abc'}") # noqa: FURB183
_ = str(f"{123}\n")

# wont trigger string formatting
# wont trigger fstring stringify code
_ = str("".join([""]))
_ = str("".join(["", 1])) # type: ignore
5 changes: 3 additions & 2 deletions test/data/stringify.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ test/data/stringify.py:24:5 [FURB123]: Replace `str(f"{123:x}")` with `f"{123:x}
test/data/stringify.py:25:5 [FURB123]: Replace `str(f"x{123}y")` with `f"x{123}y"`
test/data/stringify.py:26:5 [FURB123]: Replace `str(f"x{123}y{456}z")` with `f"x{123}y{456}z"`
test/data/stringify.py:27:5 [FURB123]: Replace `str(f"{"abc"}")` with `f"{"abc"}"`
test/data/stringify.py:30:5 [FURB123]: Replace `str("".join([""]))` with `"".join([""])`
test/data/stringify.py:31:5 [FURB123]: Replace `str("".join(["", 1]))` with `"".join(["", 1])`
test/data/stringify.py:28:5 [FURB123]: Replace `str(f"{123}\n")` with `f"{123}\n"`
test/data/stringify.py:31:5 [FURB123]: Replace `str("".join([""]))` with `"".join([""])`
test/data/stringify.py:32:5 [FURB123]: Replace `str("".join(["", 1]))` with `"".join(["", 1])`

0 comments on commit 60bb2f6

Please sign in to comment.