diff --git a/compiler/src/compile.rs b/compiler/src/compile.rs index a13d3617e83..74deb2d2a96 100644 --- a/compiler/src/compile.rs +++ b/compiler/src/compile.rs @@ -1877,7 +1877,9 @@ impl Compiler { fn lookup_name(&self, name: &str) -> &Symbol { // println!("Looking up {:?}", name); let scope = self.scope_stack.last().unwrap(); - scope.lookup(name).unwrap() + scope.lookup(name).expect( + "The symbol must be present in the symbol table, even when it is undefined in python.", + ) } // Low level helper functions: diff --git a/compiler/src/symboltable.rs b/compiler/src/symboltable.rs index c1db24bf325..6dc9c4c7fde 100644 --- a/compiler/src/symboltable.rs +++ b/compiler/src/symboltable.rs @@ -244,13 +244,11 @@ impl SymbolTableBuilder { } => { self.scan_expressions(decorator_list)?; self.register_name(name, SymbolRole::Assigned)?; - - self.enter_function(args)?; - - self.scan_statements(body)?; if let Some(expression) = returns { self.scan_expression(expression)?; } + self.enter_function(args)?; + self.scan_statements(body)?; self.leave_scope(); } ClassDef { diff --git a/tests/snippets/function.py b/tests/snippets/function.py index b499c6776b9..59fd028eace 100644 --- a/tests/snippets/function.py +++ b/tests/snippets/function.py @@ -1,4 +1,3 @@ - __name__ = "function" @@ -72,3 +71,20 @@ def nested(): f6() + + +def f7(): + try: + def t() -> void: # noqa: F821 + pass + except NameError: + return True + return False + +assert f7() + + +def f8() -> int: + return 10 + +assert f8() == 10