Skip to content

Commit 82faa9b

Browse files
authored
Add tests demonstrating f-strings with debug expressions in replacements that contain escaped characters (#14929)
1 parent 2eac00c commit 82faa9b

File tree

4 files changed

+96
-4
lines changed

4 files changed

+96
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{
3+
"preview": "enabled"
4+
}
5+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# This test is in its own file because it results in AST changes on stable.
2+
# We don't plan to fix it on stable because we plan on promoting f-string formatting soon.
3+
4+
# Regression test for https://github.com/astral-sh/ruff/issues/14766
5+
# Don't change the casing of the escaped characters in the f-string because it would be observable in the debug expression.
6+
# >>> f"{r"\xFF"=}"
7+
# 'r"ÿ"=\'\\\\xFF\''
8+
# >>> f"{r"\xff"=}"
9+
# 'r"ÿ"=\'\\\\xff\''
10+
f"{r"\xFF"=}"
11+
f"{r"\xff"=}"
12+
13+
14+
# Test for https://github.com/astral-sh/ruff/issues/14926
15+
# We could change the casing of the escaped characters in non-raw f-string because Python interprets the inner string
16+
# before printing it as a debug expression. For now, we decided not to change the casing because it's fairly complicated
17+
# to implement and it seems uncommon (debug expressions are uncommon, escapes are uncommon).
18+
# These two strings could be formatted to the same output but we currently don't do that.
19+
f"{"\xFF\N{space}"=}"
20+
f"{"\xff\N{SPACE}"=}"

crates/ruff_python_formatter/tests/fixtures.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,9 @@ fn format() {
177177
let test_file = |input_path: &Path| {
178178
let content = fs::read_to_string(input_path).unwrap();
179179

180-
let options = PyFormatOptions::from_extension(input_path);
181-
let formatted_code = format_file(&content, &options, input_path);
182-
183180
let mut snapshot = format!("## Input\n{}", CodeFrame::new("python", &content));
184-
185181
let options_path = input_path.with_extension("options.json");
182+
186183
if let Ok(options_file) = fs::File::open(options_path) {
187184
let reader = BufReader::new(options_file);
188185
let options: Vec<PyFormatOptions> =
@@ -228,6 +225,9 @@ fn format() {
228225
}
229226
} else {
230227
// We want to capture the differences in the preview style in our fixtures
228+
let options = PyFormatOptions::from_extension(input_path);
229+
let formatted_code = format_file(&content, &options, input_path);
230+
231231
let options_preview = options.with_preview(PreviewMode::Enabled);
232232
let formatted_preview = format_file(&content, &options_preview, input_path);
233233

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
source: crates/ruff_python_formatter/tests/fixtures.rs
3+
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/fstring_preview.py
4+
snapshot_kind: text
5+
---
6+
## Input
7+
```python
8+
# This test is in its own file because it results in AST changes on stable.
9+
# We don't plan to fix it on stable because we plan on promoting f-string formatting soon.
10+
11+
# Regression test for https://github.com/astral-sh/ruff/issues/14766
12+
# Don't change the casing of the escaped characters in the f-string because it would be observable in the debug expression.
13+
# >>> f"{r"\xFF"=}"
14+
# 'r"ÿ"=\'\\\\xFF\''
15+
# >>> f"{r"\xff"=}"
16+
# 'r"ÿ"=\'\\\\xff\''
17+
f"{r"\xFF"=}"
18+
f"{r"\xff"=}"
19+
20+
21+
# Test for https://github.com/astral-sh/ruff/issues/14926
22+
# We could change the casing of the escaped characters in non-raw f-string because Python interprets the inner string
23+
# before printing it as a debug expression. For now, we decided not to change the casing because it's fairly complicated
24+
# to implement and it seems uncommon (debug expressions are uncommon, escapes are uncommon).
25+
# These two strings could be formatted to the same output but we currently don't do that.
26+
f"{"\xFF\N{space}"=}"
27+
f"{"\xff\N{SPACE}"=}"
28+
```
29+
30+
## Outputs
31+
### Output 1
32+
```
33+
indent-style = space
34+
line-width = 88
35+
indent-width = 4
36+
quote-style = Double
37+
line-ending = LineFeed
38+
magic-trailing-comma = Respect
39+
docstring-code = Disabled
40+
docstring-code-line-width = "dynamic"
41+
preview = Enabled
42+
target_version = Py39
43+
source_type = Python
44+
```
45+
46+
```python
47+
# This test is in its own file because it results in AST changes on stable.
48+
# We don't plan to fix it on stable because we plan on promoting f-string formatting soon.
49+
50+
# Regression test for https://github.com/astral-sh/ruff/issues/14766
51+
# Don't change the casing of the escaped characters in the f-string because it would be observable in the debug expression.
52+
# >>> f"{r"\xFF"=}"
53+
# 'r"ÿ"=\'\\\\xFF\''
54+
# >>> f"{r"\xff"=}"
55+
# 'r"ÿ"=\'\\\\xff\''
56+
f"{r"\xFF"=}"
57+
f"{r"\xff"=}"
58+
59+
60+
# Test for https://github.com/astral-sh/ruff/issues/14926
61+
# We could change the casing of the escaped characters in non-raw f-string because Python interprets the inner string
62+
# before printing it as a debug expression. For now, we decided not to change the casing because it's fairly complicated
63+
# to implement and it seems uncommon (debug expressions are uncommon, escapes are uncommon).
64+
# These two strings could be formatted to the same output but we currently don't do that.
65+
f"{"\xFF\N{space}"=}"
66+
f"{"\xff\N{SPACE}"=}"
67+
```

0 commit comments

Comments
 (0)