1 | n/a | """Fixer for function definitions with tuple parameters. |
---|
2 | n/a | |
---|
3 | n/a | def func(((a, b), c), d): |
---|
4 | n/a | ... |
---|
5 | n/a | |
---|
6 | n/a | -> |
---|
7 | n/a | |
---|
8 | n/a | def func(x, d): |
---|
9 | n/a | ((a, b), c) = x |
---|
10 | n/a | ... |
---|
11 | n/a | |
---|
12 | n/a | It will also support lambdas: |
---|
13 | n/a | |
---|
14 | n/a | lambda (x, y): x + y -> lambda t: t[0] + t[1] |
---|
15 | n/a | |
---|
16 | n/a | # The parens are a syntax error in Python 3 |
---|
17 | n/a | lambda (x): x + y -> lambda x: x + y |
---|
18 | n/a | """ |
---|
19 | n/a | # Author: Collin Winter |
---|
20 | n/a | |
---|
21 | n/a | # Local imports |
---|
22 | n/a | from .. import pytree |
---|
23 | n/a | from ..pgen2 import token |
---|
24 | n/a | from .. import fixer_base |
---|
25 | n/a | from ..fixer_util import Assign, Name, Newline, Number, Subscript, syms |
---|
26 | n/a | |
---|
27 | n/a | def is_docstring(stmt): |
---|
28 | n/a | return isinstance(stmt, pytree.Node) and \ |
---|
29 | n/a | stmt.children[0].type == token.STRING |
---|
30 | n/a | |
---|
31 | n/a | class FixTupleParams(fixer_base.BaseFix): |
---|
32 | n/a | run_order = 4 #use a lower order since lambda is part of other |
---|
33 | n/a | #patterns |
---|
34 | n/a | BM_compatible = True |
---|
35 | n/a | |
---|
36 | n/a | PATTERN = """ |
---|
37 | n/a | funcdef< 'def' any parameters< '(' args=any ')' > |
---|
38 | n/a | ['->' any] ':' suite=any+ > |
---|
39 | n/a | | |
---|
40 | n/a | lambda= |
---|
41 | n/a | lambdef< 'lambda' args=vfpdef< '(' inner=any ')' > |
---|
42 | n/a | ':' body=any |
---|
43 | n/a | > |
---|
44 | n/a | """ |
---|
45 | n/a | |
---|
46 | n/a | def transform(self, node, results): |
---|
47 | n/a | if "lambda" in results: |
---|
48 | n/a | return self.transform_lambda(node, results) |
---|
49 | n/a | |
---|
50 | n/a | new_lines = [] |
---|
51 | n/a | suite = results["suite"] |
---|
52 | n/a | args = results["args"] |
---|
53 | n/a | # This crap is so "def foo(...): x = 5; y = 7" is handled correctly. |
---|
54 | n/a | # TODO(cwinter): suite-cleanup |
---|
55 | n/a | if suite[0].children[1].type == token.INDENT: |
---|
56 | n/a | start = 2 |
---|
57 | n/a | indent = suite[0].children[1].value |
---|
58 | n/a | end = Newline() |
---|
59 | n/a | else: |
---|
60 | n/a | start = 0 |
---|
61 | n/a | indent = "; " |
---|
62 | n/a | end = pytree.Leaf(token.INDENT, "") |
---|
63 | n/a | |
---|
64 | n/a | # We need access to self for new_name(), and making this a method |
---|
65 | n/a | # doesn't feel right. Closing over self and new_lines makes the |
---|
66 | n/a | # code below cleaner. |
---|
67 | n/a | def handle_tuple(tuple_arg, add_prefix=False): |
---|
68 | n/a | n = Name(self.new_name()) |
---|
69 | n/a | arg = tuple_arg.clone() |
---|
70 | n/a | arg.prefix = "" |
---|
71 | n/a | stmt = Assign(arg, n.clone()) |
---|
72 | n/a | if add_prefix: |
---|
73 | n/a | n.prefix = " " |
---|
74 | n/a | tuple_arg.replace(n) |
---|
75 | n/a | new_lines.append(pytree.Node(syms.simple_stmt, |
---|
76 | n/a | [stmt, end.clone()])) |
---|
77 | n/a | |
---|
78 | n/a | if args.type == syms.tfpdef: |
---|
79 | n/a | handle_tuple(args) |
---|
80 | n/a | elif args.type == syms.typedargslist: |
---|
81 | n/a | for i, arg in enumerate(args.children): |
---|
82 | n/a | if arg.type == syms.tfpdef: |
---|
83 | n/a | # Without add_prefix, the emitted code is correct, |
---|
84 | n/a | # just ugly. |
---|
85 | n/a | handle_tuple(arg, add_prefix=(i > 0)) |
---|
86 | n/a | |
---|
87 | n/a | if not new_lines: |
---|
88 | n/a | return |
---|
89 | n/a | |
---|
90 | n/a | # This isn't strictly necessary, but it plays nicely with other fixers. |
---|
91 | n/a | # TODO(cwinter) get rid of this when children becomes a smart list |
---|
92 | n/a | for line in new_lines: |
---|
93 | n/a | line.parent = suite[0] |
---|
94 | n/a | |
---|
95 | n/a | # TODO(cwinter) suite-cleanup |
---|
96 | n/a | after = start |
---|
97 | n/a | if start == 0: |
---|
98 | n/a | new_lines[0].prefix = " " |
---|
99 | n/a | elif is_docstring(suite[0].children[start]): |
---|
100 | n/a | new_lines[0].prefix = indent |
---|
101 | n/a | after = start + 1 |
---|
102 | n/a | |
---|
103 | n/a | for line in new_lines: |
---|
104 | n/a | line.parent = suite[0] |
---|
105 | n/a | suite[0].children[after:after] = new_lines |
---|
106 | n/a | for i in range(after+1, after+len(new_lines)+1): |
---|
107 | n/a | suite[0].children[i].prefix = indent |
---|
108 | n/a | suite[0].changed() |
---|
109 | n/a | |
---|
110 | n/a | def transform_lambda(self, node, results): |
---|
111 | n/a | args = results["args"] |
---|
112 | n/a | body = results["body"] |
---|
113 | n/a | inner = simplify_args(results["inner"]) |
---|
114 | n/a | |
---|
115 | n/a | # Replace lambda ((((x)))): x with lambda x: x |
---|
116 | n/a | if inner.type == token.NAME: |
---|
117 | n/a | inner = inner.clone() |
---|
118 | n/a | inner.prefix = " " |
---|
119 | n/a | args.replace(inner) |
---|
120 | n/a | return |
---|
121 | n/a | |
---|
122 | n/a | params = find_params(args) |
---|
123 | n/a | to_index = map_to_index(params) |
---|
124 | n/a | tup_name = self.new_name(tuple_name(params)) |
---|
125 | n/a | |
---|
126 | n/a | new_param = Name(tup_name, prefix=" ") |
---|
127 | n/a | args.replace(new_param.clone()) |
---|
128 | n/a | for n in body.post_order(): |
---|
129 | n/a | if n.type == token.NAME and n.value in to_index: |
---|
130 | n/a | subscripts = [c.clone() for c in to_index[n.value]] |
---|
131 | n/a | new = pytree.Node(syms.power, |
---|
132 | n/a | [new_param.clone()] + subscripts) |
---|
133 | n/a | new.prefix = n.prefix |
---|
134 | n/a | n.replace(new) |
---|
135 | n/a | |
---|
136 | n/a | |
---|
137 | n/a | ### Helper functions for transform_lambda() |
---|
138 | n/a | |
---|
139 | n/a | def simplify_args(node): |
---|
140 | n/a | if node.type in (syms.vfplist, token.NAME): |
---|
141 | n/a | return node |
---|
142 | n/a | elif node.type == syms.vfpdef: |
---|
143 | n/a | # These look like vfpdef< '(' x ')' > where x is NAME |
---|
144 | n/a | # or another vfpdef instance (leading to recursion). |
---|
145 | n/a | while node.type == syms.vfpdef: |
---|
146 | n/a | node = node.children[1] |
---|
147 | n/a | return node |
---|
148 | n/a | raise RuntimeError("Received unexpected node %s" % node) |
---|
149 | n/a | |
---|
150 | n/a | def find_params(node): |
---|
151 | n/a | if node.type == syms.vfpdef: |
---|
152 | n/a | return find_params(node.children[1]) |
---|
153 | n/a | elif node.type == token.NAME: |
---|
154 | n/a | return node.value |
---|
155 | n/a | return [find_params(c) for c in node.children if c.type != token.COMMA] |
---|
156 | n/a | |
---|
157 | n/a | def map_to_index(param_list, prefix=[], d=None): |
---|
158 | n/a | if d is None: |
---|
159 | n/a | d = {} |
---|
160 | n/a | for i, obj in enumerate(param_list): |
---|
161 | n/a | trailer = [Subscript(Number(str(i)))] |
---|
162 | n/a | if isinstance(obj, list): |
---|
163 | n/a | map_to_index(obj, trailer, d=d) |
---|
164 | n/a | else: |
---|
165 | n/a | d[obj] = prefix + trailer |
---|
166 | n/a | return d |
---|
167 | n/a | |
---|
168 | n/a | def tuple_name(param_list): |
---|
169 | n/a | l = [] |
---|
170 | n/a | for obj in param_list: |
---|
171 | n/a | if isinstance(obj, list): |
---|
172 | n/a | l.append(tuple_name(obj)) |
---|
173 | n/a | else: |
---|
174 | n/a | l.append(obj) |
---|
175 | n/a | return "_".join(l) |
---|