Skip to content

Commit 2c83511

Browse files
committed
handle type annotations in nested functions correctly
For example in the following code: def foo(x: int, y: float): def bar(q: int): return q pass Make sure that `foo` type annotations are correctly propogated to it's `__annotate__` and `__annotations__` attributes. With this chage, we'll get: >>>>> foo.__annotations__ {'x': <class 'int'>, 'y': <class 'float'>} Previously annotations where 'lost', and we would get: >>>>> foo.__annotations__ {}
1 parent d79b41b commit 2c83511

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

Lib/test/test_typing.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6707,7 +6707,6 @@ def test_get_type_hints_modules_forwardref(self):
67076707
'default_b': Optional[mod_generics_cache.B]}
67086708
self.assertEqual(gth(mod_generics_cache), mgc_hints)
67096709

6710-
@unittest.expectedFailure # TODO: RUSTPYTHON; + {'x': <class 'int'>}
67116710
def test_get_type_hints_classes(self):
67126711
self.assertEqual(gth(ann_module.C), # gth will find the right globalns
67136712
{'y': Optional[ann_module.C]})

crates/codegen/src/symboltable.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,10 @@ impl SymbolTableBuilder {
11361136
let parent_scope_typ = self.tables.last().map(|t| t.typ);
11371137
let should_save_annotation_block = matches!(
11381138
parent_scope_typ,
1139-
Some(CompilerScope::Class) | Some(CompilerScope::Module)
1139+
Some(CompilerScope::Class)
1140+
| Some(CompilerScope::Module)
1141+
| Some(CompilerScope::Function)
1142+
| Some(CompilerScope::AsyncFunction)
11401143
);
11411144
let saved_annotation_block = if should_save_annotation_block {
11421145
self.tables.last_mut().unwrap().annotation_block.take()

0 commit comments

Comments
 (0)