Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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__
    {}
  • Loading branch information
elmjag committed Feb 10, 2026
commit 2c83511bd68579e41f42c197b2105898aa7ab334
1 change: 0 additions & 1 deletion Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6707,7 +6707,6 @@ def test_get_type_hints_modules_forwardref(self):
'default_b': Optional[mod_generics_cache.B]}
self.assertEqual(gth(mod_generics_cache), mgc_hints)

@unittest.expectedFailure # TODO: RUSTPYTHON; + {'x': <class 'int'>}
def test_get_type_hints_classes(self):
self.assertEqual(gth(ann_module.C), # gth will find the right globalns
{'y': Optional[ann_module.C]})
Expand Down
5 changes: 4 additions & 1 deletion crates/codegen/src/symboltable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,10 @@ impl SymbolTableBuilder {
let parent_scope_typ = self.tables.last().map(|t| t.typ);
let should_save_annotation_block = matches!(
parent_scope_typ,
Some(CompilerScope::Class) | Some(CompilerScope::Module)
Some(CompilerScope::Class)
| Some(CompilerScope::Module)
| Some(CompilerScope::Function)
| Some(CompilerScope::AsyncFunction)
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
let saved_annotation_block = if should_save_annotation_block {
self.tables.last_mut().unwrap().annotation_block.take()
Expand Down
Loading