Canonicalize f-string field spacing in annotations (#9077)

Ignore incidental formatted-value whitespace while retaining brace-leading safeguards and literal/debug text. Include compile-only annotation-constant coverage.

Assisted-by: OpenAI Codex:model identifier unavailable
This commit is contained in:
youknowdot authored and GitHub committed 2026-10-12 13:38:23 +09:00
1 parent 896e07d9fa
commit 9ea138c889
2 files changed
+14 -13

No files matched your search

+1 -13
View File
@@ -628,19 +628,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
// put a space to avoid escaping the bracket // put a space to avoid escaping the bracket
"{ " "{ "
} else { } else {
// Preserve leading whitespace between '{' and the expression "{"
let source_text = self.source.source_text();
let start = val.range().start().to_usize();
if start > 0
&& source_text
.as_bytes()
.get(start - 1)
.is_some_and(|b| b.is_ascii_whitespace())
{
"{ "
} else {
"{"
}
}; };
self.p(brace)?; self.p(brace)?;
self.p(&buffered)?; self.p(&buffered)?;
@@ -1,5 +1,18 @@
from typing import get_type_hints from typing import get_type_hints
# Future f-string annotations canonicalize fields, not their literal padding.
for formatted, expected in (
("f'{ value }'", "f'{value}'"),
("f'{ value = }'", "f' value = {value!r}'"),
("f'{ value : >8}'", "f'{value: >8}'"),
("f'{ {1, 2} }'", "f'{ {1, 2}}'"),
):
formatted_code = compile(
"from __future__ import annotations\nx: " + formatted, "<test>", "exec"
)
assert expected in formatted_code.co_consts
def func(s: str) -> int: def func(s: str) -> int:
return int(s) return int(s)