From 08ba7aefead6429d46b9c664742f1995add630b5 Mon Sep 17 00:00:00 2001 From: dot Date: Sat, 10 Oct 2026 23:39:21 +0900 Subject: [PATCH] Canonicalize f-string field spacing in annotations 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 --- crates/codegen/src/unparse.rs | 14 +------------- extra_tests/snippets/syntax_annotations.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/crates/codegen/src/unparse.rs b/crates/codegen/src/unparse.rs index 868f1b1bab1..7c139e77a8e 100644 --- a/crates/codegen/src/unparse.rs +++ b/crates/codegen/src/unparse.rs @@ -628,19 +628,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { // put a space to avoid escaping the bracket "{ " } 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(&buffered)?; diff --git a/extra_tests/snippets/syntax_annotations.py b/extra_tests/snippets/syntax_annotations.py index 9b7d0c0ce89..b6859a89b3f 100644 --- a/extra_tests/snippets/syntax_annotations.py +++ b/extra_tests/snippets/syntax_annotations.py @@ -1,5 +1,18 @@ 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, "", "exec" + ) + assert expected in formatted_code.co_consts + + def func(s: str) -> int: return int(s)