Skip to content

Add return annotation to __annotations__ last #6071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
18 changes: 9 additions & 9 deletions compiler/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2254,15 +2254,6 @@ impl Compiler {
) -> CompileResult<u32> {
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(&parameters.posonlyargs)
Expand All @@ -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)
}

Expand Down
10 changes: 10 additions & 0 deletions extra_tests/snippets/syntax_annotations.py
Original file line number Diff line number Diff line change
@@ -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)]
Loading