diff --git a/src/compile.rs b/src/compile.rs index 41710c8..66d3081 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -931,7 +931,7 @@ impl Compiler { num_annotations += 1; } - for arg in args.args.iter() { + let mut visit_arg_annotation = |arg: &ast::Parameter| -> CompileResult<()> { if let Some(annotation) = &arg.annotation { self.emit(Instruction::LoadConst { value: bytecode::Constant::String { @@ -941,6 +941,19 @@ impl Compiler { self.compile_expression(&annotation)?; num_annotations += 1; } + Ok(()) + }; + + for arg in args.args.iter().chain(args.kwonlyargs.iter()) { + visit_arg_annotation(arg)?; + } + + if let ast::Varargs::Named(arg) = &args.vararg { + visit_arg_annotation(arg)?; + } + + if let ast::Varargs::Named(arg) = &args.kwarg { + visit_arg_annotation(arg)?; } if num_annotations > 0 { @@ -1385,6 +1398,11 @@ impl Compiler { self.compile_store(target)?; } + // Annotations are only evaluated in a module or class. + if self.ctx.in_func() { + return Ok(()); + } + // Compile annotation: self.compile_expression(annotation)?;