Skip to content

Commit 99fce6d

Browse files
committed
Fix some errors in deparsing Python 3 annotations
1 parent 7b8c5e0 commit 99fce6d

File tree

5 files changed

+53
-24
lines changed

5 files changed

+53
-24
lines changed

uncompyle6/parsers/parse3.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,27 @@ def add_custom_rules(self, tokens, customize):
636636
rule = ('mkfunc ::= kwargs %sexpr %s' %
637637
('pos_arg ' * args_pos, opname))
638638
self.add_unique_rule(rule, opname, token.attr, customize)
639+
if opname.startswith('MAKE_FUNCTION_A'):
640+
# rule = ('mkfunc2 ::= %s%sEXTENDED_ARG %s' %
641+
# ('pos_arg ' * (args_pos), 'kwargs ' * (annotate_args-1), opname))
642+
self.add_unique_rule(rule, opname, token.attr, customize)
643+
if self.version >= 3.3:
644+
rule = ('mkfunc_annotate ::= %s%sannotate_tuple LOAD_CONST LOAD_CONST EXTENDED_ARG %s' %
645+
(('pos_arg ' * (args_pos)),
646+
('call_function ' * (annotate_args-1)), opname))
647+
self.add_unique_rule(rule, opname, token.attr, customize)
648+
rule = ('mkfunc_annotate ::= %s%sannotate_tuple LOAD_CONST LOAD_CONST EXTENDED_ARG %s' %
649+
(('pos_arg ' * (args_pos)),
650+
('annotate_arg ' * (annotate_args-1)), opname))
651+
else:
652+
rule = ('mkfunc_annotate ::= %s%sannotate_tuple LOAD_CONST EXTENDED_ARG %s' %
653+
(('pos_arg ' * (args_pos)),
654+
('annotate_arg ' * (annotate_args-1)), opname))
655+
self.add_unique_rule(rule, opname, token.attr, customize)
656+
rule = ('mkfunc_annotate ::= %s%sannotate_tuple LOAD_CONST EXTENDED_ARG %s' %
657+
(('pos_arg ' * (args_pos)),
658+
('call_function ' * (annotate_args-1)), opname))
659+
self.add_unique_rule(rule, opname, token.attr, customize)
639660
elif opname_base == 'CALL_METHOD':
640661
# PyPy only - DRY with parse2
641662

@@ -696,6 +717,8 @@ def add_custom_rules(self, tokens, customize):
696717
self.check_reduce['augassign1'] = 'AST'
697718
self.check_reduce['augassign2'] = 'AST'
698719
self.check_reduce['while1stmt'] = 'noAST'
720+
self.check_reduce['annotate_tuple'] = 'noAST'
721+
self.check_reduce['kwarg'] = 'noAST'
699722
# FIXME: remove parser errors caused by the below
700723
# self.check_reduce['while1elsestmt'] = 'noAST'
701724
return
@@ -704,6 +727,10 @@ def reduce_is_invalid(self, rule, ast, tokens, first, last):
704727
lhs = rule[0]
705728
if lhs in ('augassign1', 'augassign2') and ast[0][0] == 'and':
706729
return True
730+
elif lhs == 'annotate_tuple':
731+
return not isinstance(tokens[first].attr, tuple)
732+
elif lhs == 'kwarg':
733+
return not isinstance(tokens[first].attr, str)
707734
elif lhs == 'while1elsestmt':
708735
# if SETUP_LOOP target spans the else part, then this is
709736
# not while1else. Also do for whileTrue?

uncompyle6/parsers/parse31.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,8 @@ def p_31(self, args):
3636

3737
def add_custom_rules(self, tokens, customize):
3838
super(Python31Parser, self).add_custom_rules(tokens, customize)
39-
for i, token in enumerate(tokens):
40-
opname = token.type
41-
if opname.startswith('MAKE_FUNCTION_A'):
42-
args_pos, args_kw, annotate_args = token.attr
43-
# Check that there are 2 annotated params?
44-
# rule = ('mkfunc2 ::= %s%sEXTENDED_ARG %s' %
45-
# ('pos_arg ' * (args_pos), 'kwargs ' * (annotate_args-1), opname))
46-
rule = ('mkfunc_annotate ::= %s%sannotate_tuple LOAD_CONST EXTENDED_ARG %s' %
47-
(('pos_arg ' * (args_pos)),
48-
('annotate_arg ' * (annotate_args-1)), opname))
49-
self.add_unique_rule(rule, opname, token.attr, customize)
39+
return
40+
pass
41+
5042
class Python31ParserSingle(Python31Parser, PythonParserSingle):
5143
pass

uncompyle6/parsers/parse32.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ def add_custom_rules(self, tokens, customize):
4646
(('pos_arg ' * (args_pos)),
4747
('annotate_arg ' * (annotate_args-1)), opname))
4848
self.add_unique_rule(rule, opname, token.attr, customize)
49+
pass
50+
return
51+
pass
4952

5053

5154
class Python32ParserSingle(Python32Parser, PythonParserSingle):

uncompyle6/semantics/make_function.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,15 @@ def build_param(ast, name, default):
148148
suffix = ''
149149
for param in paramnames[:i]:
150150
self.write(suffix, param)
151-
if param in annotate_args:
152-
value, string = annotate_args[param]
153-
if string:
154-
self.write(': "%s"' % value)
155-
else:
156-
self.write(': %s' % value)
151+
if param in annotate_tuple[0].attr:
152+
p = annotate_tuple[0].attr.index(param)
153+
self.write(': ')
154+
self.preorder(node[p])
155+
# value, string = annotate_args[param]
156+
# if string:
157+
# self.write(': "%s"' % value)
158+
# else:
159+
# self.write(': %s' % value)
157160
suffix = ', '
158161

159162
suffix = ', ' if i > 0 else ''
@@ -210,12 +213,14 @@ def build_param(ast, name, default):
210213
self.write(": ")
211214
else:
212215
self.write(')')
213-
if 'return' in annotate_args:
214-
value, string = annotate_args['return']
215-
if string:
216-
self.write(' -> "%s"' % value)
217-
else:
218-
self.write(' -> %s' % value)
216+
if 'return' in annotate_tuple[0].attr:
217+
self.write(' -> ')
218+
# value, string = annotate_args['return']
219+
# if string:
220+
# self.write(' -> "%s"' % value)
221+
# else:
222+
# self.write(' -> %s' % value)
223+
self.preorder(node[annotate_last-1])
219224

220225
self.println(":")
221226

uncompyle6/semantics/pysource.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,9 @@ def n_mkfunc_annotate(node):
624624
code = node[-3]
625625

626626
self.indentMore()
627-
annotate_last = -4 if self.version == 3.1 else -5
627+
for annotate_last in range(len(node)-1, -1, -1):
628+
if node[annotate_last] == 'annotate_tuple':
629+
break
628630

629631
# FIXME: handle and pass full annotate args
630632
make_function3_annotate(self, node, isLambda=False,

0 commit comments

Comments
 (0)