mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 18:58:04 +00:00
Consider quotes inside format-specs when choosing the quotes for an f-string (#14493)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
This commit is contained in:
parent
2917534279
commit
b80de52592
6 changed files with 325 additions and 80 deletions
|
@ -22,6 +22,34 @@ f'{"""other " """ + "more"}'
|
|||
f'{b"""other " """}'
|
||||
f'{f"""other " """}'
|
||||
|
||||
|
||||
# Regression tests for https://github.com/astral-sh/ruff/issues/13935
|
||||
f'{1: hy "user"}'
|
||||
f'{1:hy "user"}'
|
||||
f'{1: abcd "{1}" }'
|
||||
f'{1: abcd "{'aa'}" }'
|
||||
f'{1=: "abcd {'aa'}}'
|
||||
f'{x:a{z:hy "user"}} \'\'\''
|
||||
|
||||
# Changing the outer quotes is fine because the format-spec is in a nested expression.
|
||||
f'{f'{z=:hy "user"}'} \'\'\''
|
||||
|
||||
|
||||
# We have to be careful about changing the quotes if the f-string has a debug expression because it is inserted verbatim.
|
||||
f'{1=: "abcd \'\'}' # Don't change the outer quotes, or it results in a syntax error
|
||||
f'{1=: abcd \'\'}' # Changing the quotes here is fine because the inner quotes aren't the opposite quotes
|
||||
f'{1=: abcd \"\"}' # Changing the quotes here is fine because the inner quotes are escaped
|
||||
# Don't change the quotes in the following cases:
|
||||
f'{x=:hy "user"} \'\'\''
|
||||
f'{x=:a{y:hy "user"}} \'\'\''
|
||||
f'{x=:a{y:{z:hy "user"}}} \'\'\''
|
||||
f'{x:a{y=:{z:hy "user"}}} \'\'\''
|
||||
|
||||
# This is fine because the debug expression and format spec are in a nested expression
|
||||
|
||||
f"""{1=: "this" is fine}"""
|
||||
f'''{1=: "this" is fine}''' # Change quotes to double quotes because they're preferred
|
||||
f'{1=: {'ab"cd"'}}' # It's okay if the quotes are in an expression part.
|
||||
```
|
||||
|
||||
## Outputs
|
||||
|
@ -57,6 +85,35 @@ f'{"""other " """}'
|
|||
f'{"""other " """ + "more"}'
|
||||
f'{b"""other " """}'
|
||||
f'{f"""other " """}'
|
||||
|
||||
|
||||
# Regression tests for https://github.com/astral-sh/ruff/issues/13935
|
||||
f'{1: hy "user"}'
|
||||
f'{1:hy "user"}'
|
||||
f'{1: abcd "{1}" }'
|
||||
f'{1: abcd "{'aa'}" }'
|
||||
f'{1=: "abcd {'aa'}}'
|
||||
f'{x:a{z:hy "user"}} \'\'\''
|
||||
|
||||
# Changing the outer quotes is fine because the format-spec is in a nested expression.
|
||||
f'{f'{z=:hy "user"}'} \'\'\''
|
||||
|
||||
|
||||
# We have to be careful about changing the quotes if the f-string has a debug expression because it is inserted verbatim.
|
||||
f'{1=: "abcd \'\'}' # Don't change the outer quotes, or it results in a syntax error
|
||||
f'{1=: abcd \'\'}' # Changing the quotes here is fine because the inner quotes aren't the opposite quotes
|
||||
f'{1=: abcd \"\"}' # Changing the quotes here is fine because the inner quotes are escaped
|
||||
# Don't change the quotes in the following cases:
|
||||
f'{x=:hy "user"} \'\'\''
|
||||
f'{x=:a{y:hy "user"}} \'\'\''
|
||||
f'{x=:a{y:{z:hy "user"}}} \'\'\''
|
||||
f'{x:a{y=:{z:hy "user"}}} \'\'\''
|
||||
|
||||
# This is fine because the debug expression and format spec are in a nested expression
|
||||
|
||||
f"""{1=: "this" is fine}"""
|
||||
f"""{1=: "this" is fine}""" # Change quotes to double quotes because they're preferred
|
||||
f'{1=: {'ab"cd"'}}' # It's okay if the quotes are in an expression part.
|
||||
```
|
||||
|
||||
|
||||
|
@ -64,7 +121,7 @@ f'{f"""other " """}'
|
|||
```diff
|
||||
--- Stable
|
||||
+++ Preview
|
||||
@@ -6,11 +6,11 @@
|
||||
@@ -6,32 +6,32 @@
|
||||
f"{'a'}"
|
||||
|
||||
# 312+, it's okay to change the outer quotes even when there's a debug expression using the same quotes
|
||||
|
@ -82,4 +139,36 @@ f'{f"""other " """}'
|
|||
+f"{'''other " ''' + 'more'}"
|
||||
+f"{b'''other " '''}"
|
||||
+f"{f'''other " '''}"
|
||||
|
||||
|
||||
# Regression tests for https://github.com/astral-sh/ruff/issues/13935
|
||||
f'{1: hy "user"}'
|
||||
f'{1:hy "user"}'
|
||||
f'{1: abcd "{1}" }'
|
||||
-f'{1: abcd "{'aa'}" }'
|
||||
+f'{1: abcd "{"aa"}" }'
|
||||
f'{1=: "abcd {'aa'}}'
|
||||
-f'{x:a{z:hy "user"}} \'\'\''
|
||||
+f"{x:a{z:hy \"user\"}} '''"
|
||||
|
||||
# Changing the outer quotes is fine because the format-spec is in a nested expression.
|
||||
-f'{f'{z=:hy "user"}'} \'\'\''
|
||||
+f"{f'{z=:hy "user"}'} '''"
|
||||
|
||||
|
||||
# We have to be careful about changing the quotes if the f-string has a debug expression because it is inserted verbatim.
|
||||
f'{1=: "abcd \'\'}' # Don't change the outer quotes, or it results in a syntax error
|
||||
-f'{1=: abcd \'\'}' # Changing the quotes here is fine because the inner quotes aren't the opposite quotes
|
||||
-f'{1=: abcd \"\"}' # Changing the quotes here is fine because the inner quotes are escaped
|
||||
+f"{1=: abcd \'\'}" # Changing the quotes here is fine because the inner quotes aren't the opposite quotes
|
||||
+f"{1=: abcd \"\"}" # Changing the quotes here is fine because the inner quotes are escaped
|
||||
# Don't change the quotes in the following cases:
|
||||
f'{x=:hy "user"} \'\'\''
|
||||
f'{x=:a{y:hy "user"}} \'\'\''
|
||||
@@ -42,4 +42,4 @@
|
||||
|
||||
f"""{1=: "this" is fine}"""
|
||||
f"""{1=: "this" is fine}""" # Change quotes to double quotes because they're preferred
|
||||
-f'{1=: {'ab"cd"'}}' # It's okay if the quotes are in an expression part.
|
||||
+f"{1=: {'ab"cd"'}}" # It's okay if the quotes are in an expression part.
|
||||
```
|
||||
|
|
|
@ -326,6 +326,13 @@ assert False, "Implicit concatenated stringuses {} layout on {} format"[
|
|||
assert False, +"Implicit concatenated string" "uses {} layout on {} format".format(
|
||||
"Multiline", "first"
|
||||
)
|
||||
|
||||
|
||||
# Regression tests for https://github.com/astral-sh/ruff/issues/13935
|
||||
|
||||
"a" f'{1=: "abcd \'\'}'
|
||||
f'{1=: "abcd \'\'}' "a"
|
||||
f'{1=: "abcd \'\'}' f"{1=: 'abcd \"\"}"
|
||||
```
|
||||
|
||||
## Outputs
|
||||
|
@ -733,4 +740,11 @@ assert False, "Implicit concatenated stringuses {} layout on {} format"[
|
|||
assert False, +"Implicit concatenated stringuses {} layout on {} format".format(
|
||||
"Multiline", "first"
|
||||
)
|
||||
|
||||
|
||||
# Regression tests for https://github.com/astral-sh/ruff/issues/13935
|
||||
|
||||
f'a{1=: "abcd \'\'}'
|
||||
f'{1=: "abcd \'\'}a'
|
||||
f'{1=: "abcd \'\'}' f"{1=: 'abcd \"\"}"
|
||||
```
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue