-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ruff] Suppress strings with backslashes in interpolations before Python 3.12 (RUF027)
#21069
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
Open
danparizher
wants to merge
3
commits into
astral-sh:main
Choose a base branch
from
danparizher:fix-21033
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
300 changes: 300 additions & 0 deletions
300
...ff/snapshots/ruff_linter__rules__ruff__tests__missing_fstring_syntax_backslash_py311.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,300 @@ | ||
| --- | ||
| source: crates/ruff_linter/src/rules/ruff/mod.rs | ||
| --- | ||
| RUF027 [*] Possible f-string without an `f` prefix | ||
| --> RUF027_0.py:5:7 | ||
| | | ||
| 3 | "always ignore this: {val}" | ||
| 4 | | ||
| 5 | print("but don't ignore this: {val}") # RUF027 | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: Add `f` prefix | ||
| 2 | | ||
| 3 | "always ignore this: {val}" | ||
| 4 | | ||
| - print("but don't ignore this: {val}") # RUF027 | ||
| 5 + print(f"but don't ignore this: {val}") # RUF027 | ||
| 6 | | ||
| 7 | | ||
| 8 | def simple_cases(): | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| RUF027 [*] Possible f-string without an `f` prefix | ||
| --> RUF027_0.py:10:9 | ||
| | | ||
| 8 | def simple_cases(): | ||
| 9 | a = 4 | ||
| 10 | b = "{a}" # RUF027 | ||
| | ^^^^^ | ||
| 11 | c = "{a} {b} f'{val}' " # RUF027 | ||
| | | ||
| help: Add `f` prefix | ||
| 7 | | ||
| 8 | def simple_cases(): | ||
| 9 | a = 4 | ||
| - b = "{a}" # RUF027 | ||
| 10 + b = f"{a}" # RUF027 | ||
| 11 | c = "{a} {b} f'{val}' " # RUF027 | ||
| 12 | | ||
| 13 | | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| RUF027 [*] Possible f-string without an `f` prefix | ||
| --> RUF027_0.py:11:9 | ||
| | | ||
| 9 | a = 4 | ||
| 10 | b = "{a}" # RUF027 | ||
| 11 | c = "{a} {b} f'{val}' " # RUF027 | ||
| | ^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: Add `f` prefix | ||
| 8 | def simple_cases(): | ||
| 9 | a = 4 | ||
| 10 | b = "{a}" # RUF027 | ||
| - c = "{a} {b} f'{val}' " # RUF027 | ||
| 11 + c = f"{a} {b} f'{val}' " # RUF027 | ||
| 12 | | ||
| 13 | | ||
| 14 | def escaped_string(): | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| RUF027 [*] Possible f-string without an `f` prefix | ||
| --> RUF027_0.py:21:9 | ||
| | | ||
| 19 | def raw_string(): | ||
| 20 | a = 4 | ||
| 21 | b = r"raw string with formatting: {a}" # RUF027 | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 22 | c = r"raw string with \backslashes\ and \"escaped quotes\": {a}" # RUF027 | ||
| | | ||
| help: Add `f` prefix | ||
| 18 | | ||
| 19 | def raw_string(): | ||
| 20 | a = 4 | ||
| - b = r"raw string with formatting: {a}" # RUF027 | ||
| 21 + b = fr"raw string with formatting: {a}" # RUF027 | ||
| 22 | c = r"raw string with \backslashes\ and \"escaped quotes\": {a}" # RUF027 | ||
| 23 | | ||
| 24 | | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| RUF027 [*] Possible f-string without an `f` prefix | ||
| --> RUF027_0.py:22:9 | ||
| | | ||
| 20 | a = 4 | ||
| 21 | b = r"raw string with formatting: {a}" # RUF027 | ||
| 22 | c = r"raw string with \backslashes\ and \"escaped quotes\": {a}" # RUF027 | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: Add `f` prefix | ||
| 19 | def raw_string(): | ||
| 20 | a = 4 | ||
| 21 | b = r"raw string with formatting: {a}" # RUF027 | ||
| - c = r"raw string with \backslashes\ and \"escaped quotes\": {a}" # RUF027 | ||
| 22 + c = fr"raw string with \backslashes\ and \"escaped quotes\": {a}" # RUF027 | ||
| 23 | | ||
| 24 | | ||
| 25 | def print_name(name: str): | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| RUF027 [*] Possible f-string without an `f` prefix | ||
| --> RUF027_0.py:27:11 | ||
| | | ||
| 25 | def print_name(name: str): | ||
| 26 | a = 4 | ||
| 27 | print("Hello, {name}!") # RUF027 | ||
| | ^^^^^^^^^^^^^^^^ | ||
| 28 | print("The test value we're using today is {a}") # RUF027 | ||
| | | ||
| help: Add `f` prefix | ||
| 24 | | ||
| 25 | def print_name(name: str): | ||
| 26 | a = 4 | ||
| - print("Hello, {name}!") # RUF027 | ||
| 27 + print(f"Hello, {name}!") # RUF027 | ||
| 28 | print("The test value we're using today is {a}") # RUF027 | ||
| 29 | | ||
| 30 | | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| RUF027 [*] Possible f-string without an `f` prefix | ||
| --> RUF027_0.py:28:11 | ||
| | | ||
| 26 | a = 4 | ||
| 27 | print("Hello, {name}!") # RUF027 | ||
| 28 | print("The test value we're using today is {a}") # RUF027 | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: Add `f` prefix | ||
| 25 | def print_name(name: str): | ||
| 26 | a = 4 | ||
| 27 | print("Hello, {name}!") # RUF027 | ||
| - print("The test value we're using today is {a}") # RUF027 | ||
| 28 + print(f"The test value we're using today is {a}") # RUF027 | ||
| 29 | | ||
| 30 | | ||
| 31 | def nested_funcs(): | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| RUF027 [*] Possible f-string without an `f` prefix | ||
| --> RUF027_0.py:33:33 | ||
| | | ||
| 31 | def nested_funcs(): | ||
| 32 | a = 4 | ||
| 33 | print(do_nothing(do_nothing("{a}"))) # RUF027 | ||
| | ^^^^^ | ||
| | | ||
| help: Add `f` prefix | ||
| 30 | | ||
| 31 | def nested_funcs(): | ||
| 32 | a = 4 | ||
| - print(do_nothing(do_nothing("{a}"))) # RUF027 | ||
| 33 + print(do_nothing(do_nothing(f"{a}"))) # RUF027 | ||
| 34 | | ||
| 35 | | ||
| 36 | def tripled_quoted(): | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| RUF027 [*] Possible f-string without an `f` prefix | ||
| --> RUF027_0.py:39:19 | ||
| | | ||
| 37 | a = 4 | ||
| 38 | c = a | ||
| 39 | single_line = """ {a} """ # RUF027 | ||
| | ^^^^^^^^^^^ | ||
| 40 | # RUF027 | ||
| 41 | multi_line = a = """b { # comment | ||
| | | ||
| help: Add `f` prefix | ||
| 36 | def tripled_quoted(): | ||
| 37 | a = 4 | ||
| 38 | c = a | ||
| - single_line = """ {a} """ # RUF027 | ||
| 39 + single_line = f""" {a} """ # RUF027 | ||
| 40 | # RUF027 | ||
| 41 | multi_line = a = """b { # comment | ||
| 42 | c} d | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| 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:56:9 | ||
| | | ||
| 54 | def implicit_concat(): | ||
| 55 | a = 4 | ||
| 56 | b = "{a}" "+" "{b}" r" \\ " # RUF027 for the first part only | ||
| | ^^^^^ | ||
| 57 | print(f"{a}" "{a}" f"{b}") # RUF027 | ||
| | | ||
| help: Add `f` prefix | ||
| 53 | | ||
| 54 | def implicit_concat(): | ||
| 55 | a = 4 | ||
| - b = "{a}" "+" "{b}" r" \\ " # RUF027 for the first part only | ||
| 56 + b = f"{a}" "+" "{b}" r" \\ " # RUF027 for the first part only | ||
| 57 | print(f"{a}" "{a}" f"{b}") # RUF027 | ||
| 58 | | ||
| 59 | | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| RUF027 [*] Possible f-string without an `f` prefix | ||
| --> RUF027_0.py:57:18 | ||
| | | ||
| 55 | a = 4 | ||
| 56 | b = "{a}" "+" "{b}" r" \\ " # RUF027 for the first part only | ||
| 57 | print(f"{a}" "{a}" f"{b}") # RUF027 | ||
| | ^^^^^ | ||
| | | ||
| help: Add `f` prefix | ||
| 54 | def implicit_concat(): | ||
| 55 | a = 4 | ||
| 56 | b = "{a}" "+" "{b}" r" \\ " # RUF027 for the first part only | ||
| - print(f"{a}" "{a}" f"{b}") # RUF027 | ||
| 57 + print(f"{a}" f"{a}" f"{b}") # RUF027 | ||
| 58 | | ||
| 59 | | ||
| 60 | def escaped_chars(): | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| RUF027 [*] Possible f-string without an `f` prefix | ||
| --> RUF027_0.py:62:9 | ||
| | | ||
| 60 | def escaped_chars(): | ||
| 61 | a = 4 | ||
| 62 | b = "\"not escaped:\" '{a}' \"escaped:\": '{{c}}'" # RUF027 | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: Add `f` prefix | ||
| 59 | | ||
| 60 | def escaped_chars(): | ||
| 61 | a = 4 | ||
| - b = "\"not escaped:\" '{a}' \"escaped:\": '{{c}}'" # RUF027 | ||
| 62 + b = f"\"not escaped:\" '{a}' \"escaped:\": '{{c}}'" # RUF027 | ||
| 63 | | ||
| 64 | | ||
| 65 | def method_calls(): | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| RUF027 [*] Possible f-string without an `f` prefix | ||
| --> RUF027_0.py:70:18 | ||
| | | ||
| 68 | first = "Wendy" | ||
| 69 | last = "Appleseed" | ||
| 70 | value.method("{first} {last}") # RUF027 | ||
| | ^^^^^^^^^^^^^^^^ | ||
| 71 | | ||
| 72 | def format_specifiers(): | ||
| | | ||
| help: Add `f` prefix | ||
| 67 | value.method = print_name | ||
| 68 | first = "Wendy" | ||
| 69 | last = "Appleseed" | ||
| - value.method("{first} {last}") # RUF027 | ||
| 70 + value.method(f"{first} {last}") # RUF027 | ||
| 71 | | ||
| 72 | def format_specifiers(): | ||
| 73 | a = 4 | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| RUF027 [*] Possible f-string without an `f` prefix | ||
| --> RUF027_0.py:74:9 | ||
| | | ||
| 72 | def format_specifiers(): | ||
| 73 | a = 4 | ||
| 74 | b = "{a:b} {a:^5}" | ||
| | ^^^^^^^^^^^^^^ | ||
| 75 | | ||
| 76 | # fstrings are never correct as type definitions | ||
| | | ||
| help: Add `f` prefix | ||
| 71 | | ||
| 72 | def format_specifiers(): | ||
| 73 | a = 4 | ||
| - b = "{a:b} {a:^5}" | ||
| 74 + b = f"{a:b} {a:^5}" | ||
| 75 | | ||
| 76 | # fstrings are never correct as type definitions | ||
| 77 | # so we should always skip those | ||
| note: This is an unsafe fix and may change runtime behavior |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a variant of this test where we use a Python version before 3.12. This is still showing a diagnostic being emitted.