mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
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:
1 parent
896e07d9fa
commit
9ea138c889
2 files changed
+14
-13
No files matched your search
@@ -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)?;
|
||||
|
||||
@@ -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, "<test>", "exec"
|
||||
)
|
||||
assert expected in formatted_code.co_consts
|
||||
|
||||
|
||||
def func(s: str) -> int:
|
||||
return int(s)
|
||||
|
||||
|
||||
Reference in new issue
Block a user