Debian Codegraph @

tests/data/cases/function_trailing_comma.py

raw
1 def f(a,):
2 d = {'key': 'value',}
3 tup = (1,)
4
5 def f2(a,b,):
6 d = {'key': 'value', 'key2': 'value2',}
7 tup = (1,2,)
8
9 def f(a:int=1,):
10 call(arg={'explode': 'this',})
11 call2(arg=[1,2,3],)
12 x = {
13 "a": 1,
14 "b": 2,
15 }["a"]
16 if a == {"a": 1,"b": 2,"c": 3,"d": 4,"e": 5,"f": 6,"g": 7,"h": 8,}["a"]:
17 pass
18
19 def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
20 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
21 ]:
22 json = {"k": {"k2": {"k3": [1,]}}}
23
24
25
26 # The type annotation shouldn't get a trailing comma since that would change its type.
27 # Relevant bug report: https://github.com/psf/black/issues/2381.
28 def some_function_with_a_really_long_name() -> (
29 returning_a_deeply_nested_import_of_a_type_i_suppose
30 ):
31 pass
32
33
34 def some_method_with_a_really_long_name(very_long_parameter_so_yeah: str, another_long_parameter: int) -> (
35 another_case_of_returning_a_deeply_nested_import_of_a_type_i_suppose_cause_why_not
36 ):
37 pass
38
39
40 def func() -> (
41 also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(this_shouldn_t_get_a_trailing_comma_too)
42 ):
43 pass
44
45
46 def func() -> ((also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
47 this_shouldn_t_get_a_trailing_comma_too
48 ))
49 ):
50 pass
51
52
53 # Make sure inner one-element tuple won't explode
54 some_module.some_function(
55 argument1, (one_element_tuple,), argument4, argument5, argument6
56 )
57
58 # Inner trailing comma causes outer to explode
59 some_module.some_function(
60 argument1, (one, two,), argument4, argument5, argument6
61 )
62
63 def foo() -> (
64 # comment inside parenthesised return type
65 int
66 ):
67 ...
68
69 def foo() -> (
70 # comment inside parenthesised return type
71 # more
72 int
73 # another
74 ):
75 ...
76
77 def foo() -> (
78 # comment inside parenthesised new union return type
79 int | str | bytes
80 ):
81 ...
82
83 def foo() -> (
84 # comment inside plain tuple
85 ):
86 pass
87
88 def foo(arg: (# comment with non-return annotation
89 int
90 # comment with non-return annotation
91 )):
92 pass
93
94 def foo(arg: (# comment with non-return annotation
95 int | range | memoryview
96 # comment with non-return annotation
97 )):
98 pass
99
100 def foo(arg: (# only before
101 int
102 )):
103 pass
104
105 def foo(arg: (
106 int
107 # only after
108 )):
109 pass
110
111 variable: ( # annotation
112 because
113 # why not
114 )
115
116 variable: (
117 because
118 # why not
119 )
120
121 # output
122
123 def f(
124 a,
125 ):
126 d = {
127 "key": "value",
128 }
129 tup = (1,)
130
131
132 def f2(
133 a,
134 b,
135 ):
136 d = {
137 "key": "value",
138 "key2": "value2",
139 }
140 tup = (
141 1,
142 2,
143 )
144
145
146 def f(
147 a: int = 1,
148 ):
149 call(
150 arg={
151 "explode": "this",
152 }
153 )
154 call2(
155 arg=[1, 2, 3],
156 )
157 x = {
158 "a": 1,
159 "b": 2,
160 }["a"]
161 if (
162 a
163 == {
164 "a": 1,
165 "b": 2,
166 "c": 3,
167 "d": 4,
168 "e": 5,
169 "f": 6,
170 "g": 7,
171 "h": 8,
172 }["a"]
173 ):
174 pass
175
176
177 def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
178 Set["xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"]
179 ):
180 json = {
181 "k": {
182 "k2": {
183 "k3": [
184 1,
185 ]
186 }
187 }
188 }
189
190
191 # The type annotation shouldn't get a trailing comma since that would change its type.
192 # Relevant bug report: https://github.com/psf/black/issues/2381.
193 def some_function_with_a_really_long_name() -> (
194 returning_a_deeply_nested_import_of_a_type_i_suppose
195 ):
196 pass
197
198
199 def some_method_with_a_really_long_name(
200 very_long_parameter_so_yeah: str, another_long_parameter: int
201 ) -> another_case_of_returning_a_deeply_nested_import_of_a_type_i_suppose_cause_why_not:
202 pass
203
204
205 def func() -> (
206 also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
207 this_shouldn_t_get_a_trailing_comma_too
208 )
209 ):
210 pass
211
212
213 def func() -> (
214 also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
215 this_shouldn_t_get_a_trailing_comma_too
216 )
217 ):
218 pass
219
220
221 # Make sure inner one-element tuple won't explode
222 some_module.some_function(
223 argument1, (one_element_tuple,), argument4, argument5, argument6
224 )
225
226 # Inner trailing comma causes outer to explode
227 some_module.some_function(
228 argument1,
229 (
230 one,
231 two,
232 ),
233 argument4,
234 argument5,
235 argument6,
236 )
237
238
239 def foo() -> (
240 # comment inside parenthesised return type
241 int
242 ): ...
243
244
245 def foo() -> (
246 # comment inside parenthesised return type
247 # more
248 int
249 # another
250 ): ...
251
252
253 def foo() -> (
254 # comment inside parenthesised new union return type
255 int
256 | str
257 | bytes
258 ): ...
259
260
261 def foo() -> (
262 # comment inside plain tuple
263 ):
264 pass
265
266
267 def foo(
268 arg: ( # comment with non-return annotation
269 int
270 # comment with non-return annotation
271 ),
272 ):
273 pass
274
275
276 def foo(
277 arg: ( # comment with non-return annotation
278 int
279 | range
280 | memoryview
281 # comment with non-return annotation
282 ),
283 ):
284 pass
285
286
287 def foo(arg: int): # only before
288 pass
289
290
291 def foo(
292 arg: (
293 int
294 # only after
295 ),
296 ):
297 pass
298
299
300 variable: ( # annotation
301 because
302 # why not
303 )
304
305 variable: (
306 because
307 # why not
308 )