-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add tests demonstrating f-strings with debug expressions in replacements that contain escaped characters #14929
Merged
Merged
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions
5
...uff_python_formatter/resources/test/fixtures/ruff/expression/fstring_preview.options.json
This file contains 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,5 @@ | ||
[ | ||
{ | ||
"preview": "enabled" | ||
} | ||
] |
20 changes: 20 additions & 0 deletions
20
crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/fstring_preview.py
This file contains 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,20 @@ | ||
# This test is in its own file because it results in AST changes on stable. | ||
# We don't plan to fix it on stable because we plan on promoting f-string formatting soon. | ||
|
||
# Regression test for https://github.com/astral-sh/ruff/issues/14766 | ||
# Don't change the casing of the escaped characters in the f-string because it would be observable in the debug expression. | ||
# >>> f"{r"\xFF"=}" | ||
# 'r"ÿ"=\'\\\\xFF\'' | ||
# >>> f"{r"\xff"=}" | ||
# 'r"ÿ"=\'\\\\xff\'' | ||
f"{r"\xFF"=}" | ||
f"{r"\xff"=}" | ||
|
||
|
||
# Test for https://github.com/astral-sh/ruff/issues/14926 | ||
# We could change the casing of the escaped characters in non-raw f-string because Python interprets the inner string | ||
# before printing it as a debug expression. For now, we decided not to change the casing because it's fairly complicated | ||
# to implement and it seems uncommon (debug expressions are uncommon, escapes are uncommon). | ||
# These two strings could be formatted to the same output but we currently don't do that. | ||
f"{"\xFF\N{space}"=}" | ||
f"{"\xff\N{SPACE}"=}" |
This file contains 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
67 changes: 67 additions & 0 deletions
67
crates/ruff_python_formatter/tests/snapshots/format@expression__fstring_preview.py.snap
This file contains 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,67 @@ | ||
--- | ||
source: crates/ruff_python_formatter/tests/fixtures.rs | ||
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/fstring_preview.py | ||
snapshot_kind: text | ||
--- | ||
## Input | ||
```python | ||
# This test is in its own file because it results in AST changes on stable. | ||
# We don't plan to fix it on stable because we plan on promoting f-string formatting soon. | ||
|
||
# Regression test for https://github.com/astral-sh/ruff/issues/14766 | ||
# Don't change the casing of the escaped characters in the f-string because it would be observable in the debug expression. | ||
# >>> f"{r"\xFF"=}" | ||
# 'r"ÿ"=\'\\\\xFF\'' | ||
# >>> f"{r"\xff"=}" | ||
# 'r"ÿ"=\'\\\\xff\'' | ||
f"{r"\xFF"=}" | ||
f"{r"\xff"=}" | ||
|
||
|
||
# Test for https://github.com/astral-sh/ruff/issues/14926 | ||
# We could change the casing of the escaped characters in non-raw f-string because Python interprets the inner string | ||
# before printing it as a debug expression. For now, we decided not to change the casing because it's fairly complicated | ||
# to implement and it seems uncommon (debug expressions are uncommon, escapes are uncommon). | ||
# These two strings could be formatted to the same output but we currently don't do that. | ||
f"{"\xFF\N{space}"=}" | ||
f"{"\xff\N{SPACE}"=}" | ||
``` | ||
|
||
## Outputs | ||
### Output 1 | ||
``` | ||
indent-style = space | ||
line-width = 88 | ||
indent-width = 4 | ||
quote-style = Double | ||
line-ending = LineFeed | ||
magic-trailing-comma = Respect | ||
docstring-code = Disabled | ||
docstring-code-line-width = "dynamic" | ||
preview = Enabled | ||
target_version = Py39 | ||
source_type = Python | ||
``` | ||
|
||
```python | ||
# This test is in its own file because it results in AST changes on stable. | ||
# We don't plan to fix it on stable because we plan on promoting f-string formatting soon. | ||
|
||
# Regression test for https://github.com/astral-sh/ruff/issues/14766 | ||
# Don't change the casing of the escaped characters in the f-string because it would be observable in the debug expression. | ||
# >>> f"{r"\xFF"=}" | ||
# 'r"ÿ"=\'\\\\xFF\'' | ||
# >>> f"{r"\xff"=}" | ||
# 'r"ÿ"=\'\\\\xff\'' | ||
f"{r"\xFF"=}" | ||
f"{r"\xff"=}" | ||
|
||
|
||
# Test for https://github.com/astral-sh/ruff/issues/14926 | ||
# We could change the casing of the escaped characters in non-raw f-string because Python interprets the inner string | ||
# before printing it as a debug expression. For now, we decided not to change the casing because it's fairly complicated | ||
# to implement and it seems uncommon (debug expressions are uncommon, escapes are uncommon). | ||
# These two strings could be formatted to the same output but we currently don't do that. | ||
f"{"\xFF\N{space}"=}" | ||
f"{"\xff\N{SPACE}"=}" | ||
``` |
Oops, something went wrong.
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.
Ha, that took me a minute to figure out. We always formatted the code once with the default options even if there's an options file. We don't want that, so I moved it into the
else
branch`