Skip to content
Closed
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
8 changes: 8 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF027_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,11 @@ def fuzz_bug():
def backslash_test():
x = "test"
print("Hello {'\\n'}{x}") # Should not trigger RUF027 for Python < 3.12

# Test case for comment handling in f-string interpolations
# Should not trigger RUF027 for Python < 3.12 due to comments in interpolations
# https://github.com/astral-sh/ruff/issues/23460
def comment_test():
x = "!"
print("""{x # }
}""")
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,13 @@ fn should_be_fstring(
for f_string in value.f_strings() {
let mut has_name = false;
for element in f_string.elements.interpolations() {
// Check if the interpolation expression contains backslashes
// F-strings with backslashes in interpolations are only valid in Python 3.12+
// F-strings with backslashes or comments in interpolations are only
// valid in Python 3.12+ (PEP 701)
let interpolation_text = &fstring_expr[element.range()];
if target_version < PythonVersion::PY312 && interpolation_text.contains('\\') {
if target_version < PythonVersion::PY312
&& (interpolation_text.contains('\\')
|| interpolation_text.contains('#'))
{
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,35 @@ RUF027 [*] Possible f-string without an `f` prefix
91 | x = "test"
92 | print("Hello {'\\n'}{x}") # Should not trigger RUF027 for Python < 3.12
| ^^^^^^^^^^^^^^^^^^
93 |
94 | # Test case for comment handling in f-string interpolations
|
help: Add `f` prefix
89 | # Should not trigger RUF027 for Python < 3.12 due to backslashes in interpolations
90 | def backslash_test():
91 | x = "test"
- print("Hello {'\\n'}{x}") # Should not trigger RUF027 for Python < 3.12
92 + print(f"Hello {'\\n'}{x}") # Should not trigger RUF027 for Python < 3.12
93 |
94 | # Test case for comment handling in f-string interpolations
95 | # Should not trigger RUF027 for Python < 3.12 due to comments in interpolations
note: This is an unsafe fix and may change runtime behavior

RUF027 [*] Possible f-string without an `f` prefix
--> RUF027_0.py:99:11
|
97 | def comment_test():
98 | x = "!"
99 | print("""{x # }
| ___________^
100 | | }""")
| |____^
|
help: Add `f` prefix
96 | # https://github.com/astral-sh/ruff/issues/23460
97 | def comment_test():
98 | x = "!"
- print("""{x # }
99 + print(f"""{x # }
100 | }""")
note: This is an unsafe fix and may change runtime behavior
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,33 @@ source: crates/ruff_linter/src/rules/ruff/mod.rs
+linter.unresolved_target_version = 3.11

--- Summary ---
Removed: 2
Removed: 4
Added: 0

--- Removed ---
RUF027 [*] Possible f-string without an `f` prefix
--> RUF027_0.py:41:22
|
39 | single_line = """ {a} """ # RUF027
40 | # RUF027
41 | multi_line = a = """b { # comment
| ______________________^
42 | | c} d
43 | | """
| |_______^
|
help: Add `f` prefix
38 | c = a
39 | single_line = """ {a} """ # RUF027
40 | # RUF027
- multi_line = a = """b { # comment
41 + multi_line = a = f"""b { # comment
42 | c} d
43 | """
44 |
note: This is an unsafe fix and may change runtime behavior


RUF027 [*] Possible f-string without an `f` prefix
--> RUF027_0.py:49:9
|
Expand Down Expand Up @@ -40,11 +63,36 @@ RUF027 [*] Possible f-string without an `f` prefix
91 | x = "test"
92 | print("Hello {'\\n'}{x}") # Should not trigger RUF027 for Python < 3.12
| ^^^^^^^^^^^^^^^^^^
93 |
94 | # Test case for comment handling in f-string interpolations
|
help: Add `f` prefix
89 | # Should not trigger RUF027 for Python < 3.12 due to backslashes in interpolations
90 | def backslash_test():
91 | x = "test"
- print("Hello {'\\n'}{x}") # Should not trigger RUF027 for Python < 3.12
92 + print(f"Hello {'\\n'}{x}") # Should not trigger RUF027 for Python < 3.12
93 |
94 | # Test case for comment handling in f-string interpolations
95 | # Should not trigger RUF027 for Python < 3.12 due to comments in interpolations
note: This is an unsafe fix and may change runtime behavior


RUF027 [*] Possible f-string without an `f` prefix
--> RUF027_0.py:99:11
|
97 | def comment_test():
98 | x = "!"
99 | print("""{x # }
| ___________^
100 | | }""")
| |____^
|
help: Add `f` prefix
96 | # https://github.com/astral-sh/ruff/issues/23460
97 | def comment_test():
98 | x = "!"
- print("""{x # }
99 + print(f"""{x # }
100 | }""")
note: This is an unsafe fix and may change runtime behavior