diff --git a/compiler/codegen/src/compile.rs b/compiler/codegen/src/compile.rs index bd66c0c3d6..c481d44908 100644 --- a/compiler/codegen/src/compile.rs +++ b/compiler/codegen/src/compile.rs @@ -2254,15 +2254,6 @@ impl Compiler { ) -> CompileResult { let mut num_annotations = 0; - // Handle return annotation first - if let Some(annotation) = returns { - self.emit_load_const(ConstantData::Str { - value: "return".into(), - }); - self.compile_annotation(annotation)?; - num_annotations += 1; - } - // Handle parameter annotations let parameters_iter = std::iter::empty() .chain(¶meters.posonlyargs) @@ -2282,6 +2273,15 @@ impl Compiler { } } + // Handle return annotation last + if let Some(annotation) = returns { + self.emit_load_const(ConstantData::Str { + value: "return".into(), + }); + self.compile_annotation(annotation)?; + num_annotations += 1; + } + Ok(num_annotations) } diff --git a/extra_tests/snippets/syntax_annotations.py b/extra_tests/snippets/syntax_annotations.py new file mode 100644 index 0000000000..0f78bdc2be --- /dev/null +++ b/extra_tests/snippets/syntax_annotations.py @@ -0,0 +1,10 @@ +from typing import get_type_hints + +def func(s: str) -> int: + return int(s) + +hints = get_type_hints(func) + +# The order of type hints matters for certain functions +# e.g. functools.singledispatch +assert list(hints.items()) == [('s', str), ('return', int)]