From 60bb2f68acc8b6c89a4a7e5758aa20eb61a2c4b2 Mon Sep 17 00:00:00 2001 From: dosisod <39638017+dosisod@users.noreply.github.com> Date: Wed, 28 Feb 2024 23:21:36 -0800 Subject: [PATCH] Fix newlines being printed in stringified strings --- refurb/checks/common.py | 4 ++-- test/data/stringify.py | 3 ++- test/data/stringify.txt | 5 +++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/refurb/checks/common.py b/refurb/checks/common.py index e1894d6..a297ddc 100644 --- a/refurb/checks/common.py +++ b/refurb/checks/common.py @@ -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}"' @@ -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}}}" diff --git a/test/data/stringify.py b/test/data/stringify.py index fd678ed..6e39e64 100644 --- a/test/data/stringify.py +++ b/test/data/stringify.py @@ -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 diff --git a/test/data/stringify.txt b/test/data/stringify.txt index 8014e8a..c8c20b2 100644 --- a/test/data/stringify.txt +++ b/test/data/stringify.txt @@ -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])`