More annotation processing in to make_function

Move return-value annotation determination from n_mkfunc_annotate to
make_function_annotate which is where other kinds of annotation handling
will also need to be done.
This commit is contained in:
rocky
2016-10-29 16:03:02 -04:00
parent 2328ca7a55
commit 1574bf4e1e
2 changed files with 15 additions and 13 deletions

View File

@@ -47,7 +47,7 @@ def find_none(node):
# FIXME: DRY the below code... # FIXME: DRY the below code...
def make_function3_annotate(self, node, isLambda, nested=1, def make_function3_annotate(self, node, isLambda, nested=1,
codeNode=None, annotate=None): codeNode=None, annotate_last=-1):
""" """
Dump function defintion, doc string, and function Dump function defintion, doc string, and function
body. This code is specialized for Python 3""" body. This code is specialized for Python 3"""
@@ -70,6 +70,17 @@ def make_function3_annotate(self, node, isLambda, nested=1,
# MAKE_FUNCTION_... or MAKE_CLOSURE_... # MAKE_FUNCTION_... or MAKE_CLOSURE_...
assert node[-1].type.startswith('MAKE_') assert node[-1].type.startswith('MAKE_')
annotate_return = None
annotate_arg = node[annotate_last]
if (annotate_arg == 'annotate_arg'
and annotate_arg[0] == 'LOAD_CONST'
and isinstance(annotate_arg[0].attr, tuple)):
annotate_tup = annotate_arg[0].attr
if annotate_tup[-1] == 'return':
annotate_return = node[annotate_last-1][0].attr
pass
args_node = node[-1] args_node = node[-1]
if isinstance(args_node.attr, tuple): if isinstance(args_node.attr, tuple):
# positional args are before kwargs # positional args are before kwargs
@@ -177,8 +188,8 @@ def make_function3_annotate(self, node, isLambda, nested=1,
self.write(": ") self.write(": ")
else: else:
self.write(')') self.write(')')
if annotate: if annotate_return:
self.write(' -> "%s"' % annotate) self.write(' -> "%s"' % annotate_return)
self.println(":") self.println(":")
if (len(code.co_consts) > 0 and if (len(code.co_consts) > 0 and

View File

@@ -597,20 +597,11 @@ class SourceWalker(GenericASTTraversal, object):
code = node[-3] code = node[-3]
self.indentMore() self.indentMore()
annotate_return = None
annotate_last = -4 if self.version == 3.1 else -5 annotate_last = -4 if self.version == 3.1 else -5
annotate_arg = node[annotate_last]
if (annotate_arg == 'annotate_arg'
and annotate_arg[0] == 'LOAD_CONST'
and isinstance(annotate_arg[0].attr, tuple)):
annotate_tup = annotate_arg[0].attr
if annotate_tup[-1] == 'return':
annotate_return = node[annotate_last-1][0].attr
pass
# FIXME: handle and pass full annotate args # FIXME: handle and pass full annotate args
make_function3_annotate(self, node, isLambda=False, make_function3_annotate(self, node, isLambda=False,
codeNode=code, annotate=annotate_return) codeNode=code, annotate_last=annotate_last)
if len(self.param_stack) > 1: if len(self.param_stack) > 1:
self.write('\n\n') self.write('\n\n')