Debian Codegraph @

tests/data/cases/fmtonoff.py

raw
1 #!/usr/bin/env python3
2 import asyncio
3 import sys
4
5 from third_party import X, Y, Z
6
7 from library import some_connection, \
8 some_decorator
9 # fmt: off
10 from third_party import (X,
11 Y, Z)
12 # fmt: on
13 f'trigger 3.6 mode'
14 # Comment 1
15
16 # Comment 2
17
18 # fmt: off
19 def func_no_args():
20 a; b; c
21 if True: raise RuntimeError
22 if False: ...
23 for i in range(10):
24 print(i)
25 continue
26 exec('new-style exec', {}, {})
27 return None
28 async def coroutine(arg, exec=False):
29 'Single-line docstring. Multiline is harder to reformat.'
30 async with some_connection() as conn:
31 await conn.do_what_i_mean('SELECT bobby, tables FROM xkcd', timeout=2)
32 await asyncio.sleep(1)
33 @asyncio.coroutine
34 @some_decorator(
35 with_args=True,
36 many_args=[1,2,3]
37 )
38 def function_signature_stress_test(number:int,no_annotation=None,text:str='default',* ,debug:bool=False,**kwargs) -> str:
39 return text[number:-1]
40 # fmt: on
41 def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r''):
42 offset = attr.ib(default=attr.Factory( lambda: _r.uniform(1, 2)))
43 assert task._cancel_stack[:len(old_stack)] == old_stack
44 def spaces_types(a: int = 1, b: tuple = (), c: list = [], d: dict = {}, e: bool = True, f: int = -1, g: int = 1 if False else 2, h: str = "", i: str = r''): ...
45 def spaces2(result= _core.Value(None)):
46 ...
47 something = {
48 # fmt: off
49 key: 'value',
50 }
51
52 def subscriptlist():
53 atom[
54 # fmt: off
55 'some big and',
56 'complex subscript',
57 # fmt: on
58 goes + here, andhere,
59 ]
60
61 def import_as_names():
62 # fmt: off
63 from hello import a, b
64 'unformatted'
65 # fmt: on
66
67 def testlist_star_expr():
68 # fmt: off
69 a , b = *hello
70 'unformatted'
71 # fmt: on
72
73 def yield_expr():
74 # fmt: off
75 yield hello
76 'unformatted'
77 # fmt: on
78 'formatted'
79 # fmt: off
80 ( yield hello )
81 'unformatted'
82 # fmt: on
83
84 def example(session):
85 # fmt: off
86 result = session\
87 .query(models.Customer.id)\
88 .filter(models.Customer.account_id == account_id,
89 models.Customer.email == email_address)\
90 .order_by(models.Customer.id.asc())\
91 .all()
92 # fmt: on
93 def off_and_on_without_data():
94 """Test that comment-only fmt:off/on blocks preserve formatting."""
95 # fmt: off
96 #should not be formatted
97 # fmt: on
98 # fmt: off
99
100 #should not be formatted
101
102 # fmt: on
103 # fmt: off
104 #should not be formatted
105 #should not be formatted #also should not be formatted
106 # fmt: on
107 pass
108 def on_and_off_with_comment_only_blocks():
109 """Test that fmt:off/on works with multiple directives and comment-only blocks."""
110 # fmt: on
111 # fmt: off
112 this=should.not_be.formatted()
113 and_=indeed . it is not formatted
114 because . the . handling . inside . generate_ignored_nodes()
115 now . considers . multiple . fmt . directives . within . one . prefix
116 # fmt: on
117 # fmt: off
118 #should not be formatted
119 # fmt: on
120 # fmt: off
121
122 #should not be formatted
123
124 # fmt: on
125 # fmt: off
126 #should not be formatted
127 #should not be formatted #also should not be formatted
128 # fmt: on
129 def long_lines():
130 if True:
131 typedargslist.extend(
132 gen_annotated_params(ast_args.kwonlyargs, ast_args.kw_defaults, parameters, implicit_default=True)
133 )
134 # fmt: off
135 a = (
136 unnecessary_bracket()
137 )
138 # fmt: on
139 _type_comment_re = re.compile(
140 r"""
141 ^
142 [\t ]*
143 \#[ ]type:[ ]*
144 (?P<type>
145 [^#\t\n]+?
146 )
147 (?<!ignore) # note: this will force the non-greedy + in <type> to match
148 # a trailing space which is why we need the silliness below
149 (?<!ignore[ ]{1})(?<!ignore[ ]{2})(?<!ignore[ ]{3})(?<!ignore[ ]{4})
150 (?<!ignore[ ]{5})(?<!ignore[ ]{6})(?<!ignore[ ]{7})(?<!ignore[ ]{8})
151 (?<!ignore[ ]{9})(?<!ignore[ ]{10})
152 [\t ]*
153 (?P<nl>
154 (?:\#[^\n]*)?
155 \n?
156 )
157 $
158 """,
159 # fmt: off
160 re.MULTILINE|re.VERBOSE
161 # fmt: on
162 )
163 def single_literal_yapf_disable():
164 """Black does not support this."""
165 BAZ = {
166 (1, 2, 3, 4),
167 (5, 6, 7, 8),
168 (9, 10, 11, 12)
169 } # yapf: disable
170 cfg.rule(
171 "Default", "address",
172 xxxx_xxxx=["xxx-xxxxxx-xxxxxxxxxx"],
173 xxxxxx="xx_xxxxx", xxxxxxx="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
174 xxxxxxxxx_xxxx=True, xxxxxxxx_xxxxxxxxxx=False,
175 xxxxxx_xxxxxx=2, xxxxxx_xxxxx_xxxxxxxx=70, xxxxxx_xxxxxx_xxxxx=True,
176 # fmt: off
177 xxxxxxx_xxxxxxxxxxxx={
178 "xxxxxxxx": {
179 "xxxxxx": False,
180 "xxxxxxx": False,
181 "xxxx_xxxxxx": "xxxxx",
182 },
183 "xxxxxxxx-xxxxx": {
184 "xxxxxx": False,
185 "xxxxxxx": True,
186 "xxxx_xxxxxx": "xxxxxx",
187 },
188 },
189 # fmt: on
190 xxxxxxxxxx_xxxxxxxxxxx_xxxxxxx_xxxxxxxxx=5
191 )
192
193 # Test comment-only blocks at file level with various spacing patterns
194 # fmt: off
195 #nospace
196 # twospaces
197 # fmt: on
198
199
200 # fmt: off
201 #nospaceatall
202 #extraspaces
203 #evenmorespaces
204 # fmt: on
205
206
207 # fmt: off
208 # fmt: on
209
210
211 # fmt: off
212 #SBATCH --job-name=test
213 #SBATCH --output=test.out
214 # fmt: on
215
216
217 # fmt: off
218 #first
219
220 #second
221 # fmt: on
222
223
224 # fmt: off
225 #!@#$%^&*()
226 #<=>+-*/
227 # fmt: on
228
229
230 # fmt: off
231 #x=1+2
232 #y = 3
233 #z = 4
234 # fmt: on
235
236 # fmt: off
237 yield 'hello'
238 # No formatting to the end of the file
239 l=[1,2,3]
240 d={'a':1,
241 'b':2}
242
243 # output
244
245
246 #!/usr/bin/env python3
247 import asyncio
248 import sys
249
250 from third_party import X, Y, Z
251
252 from library import some_connection, some_decorator
253 # fmt: off
254 from third_party import (X,
255 Y, Z)
256 # fmt: on
257 f"trigger 3.6 mode"
258 # Comment 1
259
260 # Comment 2
261
262
263 # fmt: off
264 def func_no_args():
265 a; b; c
266 if True: raise RuntimeError
267 if False: ...
268 for i in range(10):
269 print(i)
270 continue
271 exec('new-style exec', {}, {})
272 return None
273 async def coroutine(arg, exec=False):
274 'Single-line docstring. Multiline is harder to reformat.'
275 async with some_connection() as conn:
276 await conn.do_what_i_mean('SELECT bobby, tables FROM xkcd', timeout=2)
277 await asyncio.sleep(1)
278 @asyncio.coroutine
279 @some_decorator(
280 with_args=True,
281 many_args=[1,2,3]
282 )
283 def function_signature_stress_test(number:int,no_annotation=None,text:str='default',* ,debug:bool=False,**kwargs) -> str:
284 return text[number:-1]
285 # fmt: on
286 def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r""):
287 offset = attr.ib(default=attr.Factory(lambda: _r.uniform(1, 2)))
288 assert task._cancel_stack[: len(old_stack)] == old_stack
289
290
291 def spaces_types(
292 a: int = 1,
293 b: tuple = (),
294 c: list = [],
295 d: dict = {},
296 e: bool = True,
297 f: int = -1,
298 g: int = 1 if False else 2,
299 h: str = "",
300 i: str = r"",
301 ): ...
302 def spaces2(result=_core.Value(None)): ...
303
304
305 something = {
306 # fmt: off
307 key: 'value',
308 }
309
310
311 def subscriptlist():
312 atom[
313 # fmt: off
314 'some big and',
315 'complex subscript',
316 # fmt: on
317 goes + here,
318 andhere,
319 ]
320
321
322 def import_as_names():
323 # fmt: off
324 from hello import a, b
325 'unformatted'
326 # fmt: on
327
328
329 def testlist_star_expr():
330 # fmt: off
331 a , b = *hello
332 'unformatted'
333 # fmt: on
334
335
336 def yield_expr():
337 # fmt: off
338 yield hello
339 'unformatted'
340 # fmt: on
341 "formatted"
342 # fmt: off
343 ( yield hello )
344 'unformatted'
345 # fmt: on
346
347
348 def example(session):
349 # fmt: off
350 result = session\
351 .query(models.Customer.id)\
352 .filter(models.Customer.account_id == account_id,
353 models.Customer.email == email_address)\
354 .order_by(models.Customer.id.asc())\
355 .all()
356 # fmt: on
357
358
359 def off_and_on_without_data():
360 """Test that comment-only fmt:off/on blocks preserve formatting."""
361 # fmt: off
362 #should not be formatted
363 # fmt: on
364 # fmt: off
365
366 #should not be formatted
367
368 # fmt: on
369 # fmt: off
370 #should not be formatted
371 #should not be formatted #also should not be formatted
372 # fmt: on
373 pass
374
375
376 def on_and_off_with_comment_only_blocks():
377 """Test that fmt:off/on works with multiple directives and comment-only blocks."""
378 # fmt: on
379 # fmt: off
380 this=should.not_be.formatted()
381 and_=indeed . it is not formatted
382 because . the . handling . inside . generate_ignored_nodes()
383 now . considers . multiple . fmt . directives . within . one . prefix
384
385 # fmt: off
386 #should not be formatted
387 # fmt: on
388 # fmt: off
389
390 #should not be formatted
391
392 # fmt: on
393 # fmt: off
394 #should not be formatted
395 #should not be formatted #also should not be formatted
396 # fmt: on
397
398
399 def long_lines():
400 if True:
401 typedargslist.extend(
402 gen_annotated_params(
403 ast_args.kwonlyargs,
404 ast_args.kw_defaults,
405 parameters,
406 implicit_default=True,
407 )
408 )
409 # fmt: off
410 a = (
411 unnecessary_bracket()
412 )
413 # fmt: on
414 _type_comment_re = re.compile(
415 r"""
416 ^
417 [\t ]*
418 \#[ ]type:[ ]*
419 (?P<type>
420 [^#\t\n]+?
421 )
422 (?<!ignore) # note: this will force the non-greedy + in <type> to match
423 # a trailing space which is why we need the silliness below
424 (?<!ignore[ ]{1})(?<!ignore[ ]{2})(?<!ignore[ ]{3})(?<!ignore[ ]{4})
425 (?<!ignore[ ]{5})(?<!ignore[ ]{6})(?<!ignore[ ]{7})(?<!ignore[ ]{8})
426 (?<!ignore[ ]{9})(?<!ignore[ ]{10})
427 [\t ]*
428 (?P<nl>
429 (?:\#[^\n]*)?
430 \n?
431 )
432 $
433 """,
434 # fmt: off
435 re.MULTILINE|re.VERBOSE
436 # fmt: on
437 )
438
439
440 def single_literal_yapf_disable():
441 """Black does not support this."""
442 BAZ = {(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12)} # yapf: disable
443
444
445 cfg.rule(
446 "Default",
447 "address",
448 xxxx_xxxx=["xxx-xxxxxx-xxxxxxxxxx"],
449 xxxxxx="xx_xxxxx",
450 xxxxxxx="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
451 xxxxxxxxx_xxxx=True,
452 xxxxxxxx_xxxxxxxxxx=False,
453 xxxxxx_xxxxxx=2,
454 xxxxxx_xxxxx_xxxxxxxx=70,
455 xxxxxx_xxxxxx_xxxxx=True,
456 # fmt: off
457 xxxxxxx_xxxxxxxxxxxx={
458 "xxxxxxxx": {
459 "xxxxxx": False,
460 "xxxxxxx": False,
461 "xxxx_xxxxxx": "xxxxx",
462 },
463 "xxxxxxxx-xxxxx": {
464 "xxxxxx": False,
465 "xxxxxxx": True,
466 "xxxx_xxxxxx": "xxxxxx",
467 },
468 },
469 # fmt: on
470 xxxxxxxxxx_xxxxxxxxxxx_xxxxxxx_xxxxxxxxx=5,
471 )
472
473 # Test comment-only blocks at file level with various spacing patterns
474 # fmt: off
475 #nospace
476 # twospaces
477 # fmt: on
478
479
480 # fmt: off
481 #nospaceatall
482 #extraspaces
483 #evenmorespaces
484 # fmt: on
485
486
487 # fmt: off
488 # fmt: on
489
490
491 # fmt: off
492 #SBATCH --job-name=test
493 #SBATCH --output=test.out
494 # fmt: on
495
496
497 # fmt: off
498 #first
499
500 #second
501 # fmt: on
502
503
504 # fmt: off
505 #!@#$%^&*()
506 #<=>+-*/
507 # fmt: on
508
509
510 # fmt: off
511 #x=1+2
512 #y = 3
513 #z = 4
514 # fmt: on
515
516 # fmt: off
517 yield 'hello'
518 # No formatting to the end of the file
519 l=[1,2,3]
520 d={'a':1,
521 'b':2}