Skip to content

Commit 433aee9

Browse files
committed
Fix annotationlib parsing
1 parent 144dc7e commit 433aee9

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

crates/codegen/src/compile.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use malachite_bigint::BigInt;
2222
use num_complex::Complex;
2323
use num_traits::{Num, ToPrimitive};
2424
use ruff_python_ast as ast;
25-
use ruff_text_size::{Ranged, TextRange};
25+
use ruff_text_size::{Ranged, TextRange, TextSize};
2626
use std::collections::HashSet;
2727

2828
use rustpython_compiler_core::{
@@ -8366,9 +8366,11 @@ impl Compiler {
83668366
// Compile the interpolation value
83678367
self.compile_expression(&interp.expression)?;
83688368

8369-
// Load the expression source string
8370-
let expr_range = interp.expression.range();
8371-
let expr_source = self.source_file.slice(expr_range);
8369+
// Load the expression source string, including any
8370+
// whitespace between '{' and the expression start
8371+
let after_brace = interp.range.start() + TextSize::new(1);
8372+
let expr_end = interp.expression.range().end();
8373+
let expr_source = self.source_file.slice(TextRange::new(after_brace, expr_end));
83728374
self.emit_load_const(ConstantData::Str {
83738375
value: expr_source.to_string().into(),
83748376
});

crates/codegen/src/unparse.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,19 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
561561
// put a space to avoid escaping the bracket
562562
"{ "
563563
} else {
564-
"{"
564+
// Preserve leading whitespace between '{' and the expression
565+
let source_text = self.source.source_text();
566+
let start = val.range().start().to_usize();
567+
if start > 0
568+
&& source_text
569+
.as_bytes()
570+
.get(start - 1)
571+
.is_some_and(|b| b.is_ascii_whitespace())
572+
{
573+
"{ "
574+
} else {
575+
"{"
576+
}
565577
};
566578
self.p(brace)?;
567579
self.p(&buffered)?;

0 commit comments

Comments
 (0)