Skip to content

Commit 2f0b026

Browse files
committed
Clean up handling of function return type annotation.
1 parent 76f06de commit 2f0b026

3 files changed

Lines changed: 7 additions & 3 deletions

File tree

py/compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2764,7 +2764,7 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
27642764
apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
27652765
}
27662766

2767-
assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // 2 is something...
2767+
// pns->nodes[2] is return/whole function annotation
27682768

27692769
compile_node(comp, pns->nodes[3]); // 3 is function body
27702770
// emit return if it wasn't the last opcode

py/grammar.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ DEF_RULE(decorator, nc, and(4), tok(DEL_AT), rule(dotted_name), opt_rule(trailer
3333
DEF_RULE(decorators, nc, one_or_more, rule(decorator))
3434
DEF_RULE(decorated, c(decorated), and(2), rule(decorators), rule(decorated_body))
3535
DEF_RULE(decorated_body, nc, or(2), rule(classdef), rule(funcdef))
36-
DEF_RULE(funcdef, c(funcdef), and(8), tok(KW_DEF), tok(NAME), tok(DEL_PAREN_OPEN), opt_rule(typedargslist), tok(DEL_PAREN_CLOSE), opt_rule(funcdef_2), tok(DEL_COLON), rule(suite))
37-
DEF_RULE(funcdef_2, nc, and(2), tok(DEL_MINUS_MORE), rule(test))
36+
DEF_RULE(funcdef, c(funcdef), and(8), tok(KW_DEF), tok(NAME), tok(DEL_PAREN_OPEN), opt_rule(typedargslist), tok(DEL_PAREN_CLOSE), opt_rule(funcdefrettype), tok(DEL_COLON), rule(suite))
37+
DEF_RULE(funcdefrettype, nc, and(2), tok(DEL_MINUS_MORE), rule(test))
3838
// TODO typedargslist lets through more than is allowed
3939
DEF_RULE(typedargslist, nc, list_with_end, rule(typedargslist_item), tok(DEL_COMMA))
4040
DEF_RULE(typedargslist_item, nc, or(3), rule(typedargslist_name), rule(typedargslist_star), rule(typedargslist_dbl_star))

tests/basics/fun-annotations.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def foo(x: int, y: list) -> dict:
2+
return {x: y}
3+
4+
print(foo(1, [2, 3]))

0 commit comments

Comments
 (0)