mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Fix future annotation block alignment (#8506)
This commit is contained in:
1 parent
1178841eab
commit
d5919ce2f4
1 file changed
+27
@@ -5083,6 +5083,14 @@ impl<'warnings> Compiler<'warnings> {
|
||||
func_range: TextRange,
|
||||
) -> CompileResult<bool> {
|
||||
if !self.next_function_annotation_symbol_table_uses_annotations() {
|
||||
// CPython creates a hidden AnnotationBlock for every function
|
||||
// signature under `from __future__ import annotations`, including
|
||||
// an unannotated one. It still belongs to this function: consume
|
||||
// it so the next function sees its own block rather than remaining
|
||||
// pinned to this unused entry.
|
||||
if self.push_annotation_symbol_table() {
|
||||
self.pop_annotation_symbol_table();
|
||||
}
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
@@ -31247,6 +31255,25 @@ def f(x: T): pass
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn future_unannotated_function_does_not_hide_next_annotation_block() {
|
||||
let code = compile_exec(
|
||||
"\
|
||||
from __future__ import annotations
|
||||
def plain(x): pass
|
||||
def annotated(x: int): pass
|
||||
",
|
||||
);
|
||||
let annotate = find_direct_child_code(&code, "__annotate__")
|
||||
.expect("second function must retain its annotation closure");
|
||||
assert!(
|
||||
annotate.constants.iter().any(
|
||||
|constant| matches!(constant, ConstantData::Str { value } if value.as_str() == Ok("int"))
|
||||
),
|
||||
"annotation closure must belong to the annotated function"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deferred_annotation_format_name_does_not_capture_helper_parameter() {
|
||||
let code = compile_exec(
|
||||
|
||||
Reference in new issue
Block a user