src/black/nodes.py
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | """
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | blib2to3 Node/Leaf transformation-related utility functions.
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | """
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import sys
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from typing import Final, Generic, Iterator, List, Optional, Set, Tuple, TypeVar, Union
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | if sys.version_info >= (3, 10):
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from typing import TypeGuard
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | else:
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from typing_extensions import TypeGuard
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from mypy_extensions import mypyc_attr
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.cache import CACHE_DIR
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.mode import Mode, Preview
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.strings import get_string_prefix, has_triple_quotes
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from blib2to3 import pygram
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from blib2to3.pgen2 import token
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from blib2to3.pytree import NL, Leaf, Node, type_repr
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | pygram.initialize(CACHE_DIR)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms: Final = pygram.python_symbols
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # types
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | T = TypeVar("T")
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | LN = Union[Leaf, Node]
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | LeafID = int
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | NodeType = int
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | WHITESPACE: Final = {token.DEDENT, token.INDENT, token.NEWLINE}
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | STATEMENT: Final = {
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.if_stmt,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.while_stmt,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.for_stmt,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.try_stmt,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.except_clause,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.with_stmt,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.funcdef,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.classdef,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.match_stmt,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.case_block,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | }
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | STANDALONE_COMMENT: Final = 153
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.tok_name[STANDALONE_COMMENT] = "STANDALONE_COMMENT"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | LOGIC_OPERATORS: Final = {"and", "or"}
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | COMPARATORS: Final = {
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.LESS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.GREATER,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.EQEQUAL,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.NOTEQUAL,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.LESSEQUAL,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.GREATEREQUAL,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | }
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | MATH_OPERATORS: Final = {
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.VBAR,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.CIRCUMFLEX,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.AMPER,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.LEFTSHIFT,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.RIGHTSHIFT,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.PLUS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.MINUS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.STAR,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.SLASH,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.DOUBLESLASH,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.PERCENT,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.AT,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.TILDE,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.DOUBLESTAR,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | }
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | STARS: Final = {token.STAR, token.DOUBLESTAR}
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | VARARGS_SPECIALS: Final = STARS | {token.SLASH}
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | VARARGS_PARENTS: Final = {
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.arglist,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.argument, # double star in arglist
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.trailer, # single argument to call
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.typedargslist,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.varargslist, # lambdas
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | }
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | UNPACKING_PARENTS: Final = {
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.atom, # single element of a list or set literal
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.dictsetmaker,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.listmaker,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.testlist_gexp,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.testlist_star_expr,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.subject_expr,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.pattern,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | }
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | TEST_DESCENDANTS: Final = {
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.test,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.lambdef,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.or_test,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.and_test,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.not_test,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.comparison,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.star_expr,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.expr,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.xor_expr,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.and_expr,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.shift_expr,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.arith_expr,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.trailer,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.term,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms.power,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | }
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | TYPED_NAMES: Final = {syms.tname, syms.tname_star}
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ASSIGNMENTS: Final = {
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "=",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "+=",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "-=",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "*=",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "@=",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "/=",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "%=",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "&=",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "|=",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "^=",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "<<=",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ">>=",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "**=",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "//=",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | }
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | IMPLICIT_TUPLE: Final = {syms.testlist, syms.testlist_star_expr, syms.exprlist}
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | BRACKET: Final = {
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.LPAR: token.RPAR,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.LSQB: token.RSQB,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.LBRACE: token.RBRACE,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | }
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | OPENING_BRACKETS: Final = set(BRACKET.keys())
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | CLOSING_BRACKETS: Final = set(BRACKET.values())
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | BRACKETS: Final = OPENING_BRACKETS | CLOSING_BRACKETS
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ALWAYS_NO_SPACE: Final = CLOSING_BRACKETS | {token.COMMA, STANDALONE_COMMENT}
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | RARROW = 55
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @mypyc_attr(allow_interpreted_subclasses=True)
---- ---- ---- ---- ---- ---- ---- 04 -- --- --- --- --- --- --- ------- ------- ------- | class Visitor(Generic[T]):
---- ---- ---- ---- ---- ---- ---- 04 -- --- --- --- --- --- --- ------- ------- ------- | """Basic lib2to3 visitor that yields things of type `T` on `visit()`."""
---- ---- ---- ---- ---- ---- ---- 04 -- --- --- --- --- --- --- ------- ------- ------- |
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def visit(self, node: LN) -> Iterator[T]:
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Main method to visit `node` and its children.
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | It tries to find a `visit_*()` method for the given `node.type`, like
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | `visit_simple_stmt` for Node objects or `visit_INDENT` for Leaf objects.
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | If no dedicated `visit_*()` method is found, chooses `visit_default()`
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | instead.
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | Then yields objects of type `T` from the selected visitor.
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | if node.type < 256:
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | name = token.tok_name[node.type]
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | else:
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | name = str(type_repr(node.type))
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # We explicitly branch on whether a visitor exists (instead of
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # using self.visit_default as the default arg to getattr) in order
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # to save needing to create a bound method object and so mypyc can
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # generate a native call to visit_default.
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | visitf = getattr(self, f"visit_{name}", None)
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | if visitf:
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | yield from visitf(node)
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | else:
0023 0011 0010 0004 0007 0002 0004 04 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | yield from self.visit_default(node)
---- ---- ---- ---- ---- ---- ---- 04 -- --- --- --- --- --- --- ------- ------- ------- |
0005 0005 0004 0000 0000 0000 0001 04 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def visit_default(self, node: LN) -> Iterator[T]:
0005 0005 0004 0000 0000 0000 0001 04 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """Default `visit_*()` implementation. Recurses to children of `node`."""
0005 0005 0004 0000 0000 0000 0001 04 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | if isinstance(node, Node):
0005 0005 0004 0000 0000 0000 0001 04 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | for child in node.children:
0005 0005 0004 0000 0000 0000 0001 04 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | yield from self.visit(child)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | def whitespace(leaf: Leaf, *, complex_subscript: bool, mode: Mode) -> str: # noqa: C901
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | """Return whitespace prefix if needed for the given `leaf`.
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | `complex_subscript` signals whether the given leaf is part of a subscription
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | which has non-trivial arguments, like arithmetic expressions or function calls.
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | """
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | NO: Final[str] = ""
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | SPACE: Final[str] = " "
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | DOUBLESPACE: Final[str] = " "
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | t = leaf.type
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | p = leaf.parent
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | v = leaf.value
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if t in ALWAYS_NO_SPACE:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if t == token.COMMENT:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return DOUBLESPACE
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if t == token.COLON and p.type not in {
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | syms.subscript,
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | syms.subscriptlist,
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | syms.sliceop,
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | }:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | prev = leaf.prev_sibling
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if not prev:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | prevp = preceding_leaf(p)
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if not prevp or prevp.type in OPENING_BRACKETS:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if t == token.COLON:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if prevp.type == token.COLON:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif prevp.type != token.COMMA and not complex_subscript:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return SPACE
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if prevp.type == token.EQUAL:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if prevp.parent:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if prevp.parent.type in {
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | syms.arglist,
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | syms.argument,
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | syms.parameters,
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | syms.varargslist,
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | }:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif prevp.parent.type == syms.typedargslist:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # A bit hacky: if the equal sign has whitespace, it means we
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # previously found it's a typed argument. So, we're using
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # that, too.
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return prevp.prefix
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif (
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | prevp.type == token.STAR
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | and parent_type(prevp) == syms.star_expr
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | and parent_type(prevp.parent) == syms.subscriptlist
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | ):
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # No space between typevar tuples.
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif prevp.type in VARARGS_SPECIALS:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if is_vararg(prevp, within=VARARGS_PARENTS | UNPACKING_PARENTS):
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif prevp.type == token.COLON:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if prevp.parent and prevp.parent.type in {syms.subscript, syms.sliceop}:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return SPACE if complex_subscript else NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif (
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | prevp.parent
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | and prevp.parent.type == syms.factor
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | and prevp.type in MATH_OPERATORS
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | ):
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif prevp.type == token.AT and p.parent and p.parent.type == syms.decorator:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # no space in decorators
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif prev.type in OPENING_BRACKETS:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if p.type in {syms.parameters, syms.arglist}:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # untyped function signatures or calls
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if not prev or prev.type != token.COMMA:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif p.type == syms.varargslist:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # lambdas
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if prev and prev.type != token.COMMA:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif p.type == syms.typedargslist:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # typed function signatures
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if not prev:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if t == token.EQUAL:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if prev.type not in TYPED_NAMES:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif prev.type == token.EQUAL:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # A bit hacky: if the equal sign has whitespace, it means we
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # previously found it's a typed argument. So, we're using that, too.
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return prev.prefix
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif prev.type != token.COMMA:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif p.type in TYPED_NAMES:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # type names
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if not prev:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | prevp = preceding_leaf(p)
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if not prevp or prevp.type != token.COMMA:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif p.type == syms.trailer:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # attributes and calls
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if t == token.LPAR or t == token.RPAR:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if not prev:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if t == token.DOT or t == token.LSQB:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif prev.type != token.COMMA:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif p.type == syms.argument:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # single argument
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if t == token.EQUAL:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if not prev:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | prevp = preceding_leaf(p)
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if not prevp or prevp.type == token.LPAR:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif prev.type in {token.EQUAL} | VARARGS_SPECIALS:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif p.type == syms.decorator:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # decorators
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif p.type == syms.dotted_name:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if prev:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | prevp = preceding_leaf(p)
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if not prevp or prevp.type == token.AT or prevp.type == token.DOT:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif p.type == syms.classdef:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if t == token.LPAR:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if prev and prev.type == token.LPAR:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif p.type in {syms.subscript, syms.sliceop}:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # indexing
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if not prev:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | assert p.parent is not None, "subscripts are always parented"
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if p.parent.type == syms.subscriptlist:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return SPACE
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif Preview.walrus_subscript in mode and (
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | t == token.COLONEQUAL or prev.type == token.COLONEQUAL
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | ):
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return SPACE
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif not complex_subscript:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif p.type == syms.atom:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if prev and t == token.DOT:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # dots, but not the first one.
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif p.type == syms.dictsetmaker:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # dict unpacking
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if prev and prev.type == token.DOUBLESTAR:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif p.type in {syms.factor, syms.star_expr}:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | # unary ops
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if not prev:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | prevp = preceding_leaf(p)
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if not prevp or prevp.type in OPENING_BRACKETS:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | prevp_parent = prevp.parent
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | assert prevp_parent is not None
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if prevp.type == token.COLON and prevp_parent.type in {
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | syms.subscript,
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | syms.sliceop,
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | }:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif prevp.type == token.EQUAL and prevp_parent.type == syms.argument:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif t in {token.NAME, token.NUMBER, token.STRING}:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif p.type == syms.import_from:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if t == token.DOT:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if prev and prev.type == token.DOT:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif t == token.NAME:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if v == "import":
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return SPACE
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if prev and prev.type == token.DOT:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif p.type == syms.sliceop:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | elif p.type == syms.except_clause:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | if t == token.STAR:
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return NO
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 |
0233 0144 0162 0019 0004 0049 0018 -- 104 009 103 119 227 112 346 2355.34 23359.07 0009.92 | return SPACE
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0005 0005 0004 0000 0000 0000 0001 -- 02 003 007 005 010 010 015 0049.83 0106.78 0002.14 | def make_simple_prefix(nl_count: int, form_feed: bool, empty_line: str = "\n") -> str:
0005 0005 0004 0000 0000 0000 0001 -- 02 003 007 005 010 010 015 0049.83 0106.78 0002.14 | """Generate a normalized prefix string."""
0005 0005 0004 0000 0000 0000 0001 -- 02 003 007 005 010 010 015 0049.83 0106.78 0002.14 | if form_feed:
0005 0005 0004 0000 0000 0000 0001 -- 02 003 007 005 010 010 015 0049.83 0106.78 0002.14 | return (empty_line * (nl_count - 1)) + "\f" + empty_line
0005 0005 0004 0000 0000 0000 0001 -- 02 003 007 005 010 010 015 0049.83 0106.78 0002.14 | return empty_line * nl_count
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0016 0013 0012 0000 0000 0003 0001 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | def preceding_leaf(node: Optional[LN]) -> Optional[Leaf]:
0016 0013 0012 0000 0000 0003 0001 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | """Return the first leaf that precedes `node`, if any."""
0016 0013 0012 0000 0000 0003 0001 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | while node:
0016 0013 0012 0000 0000 0003 0001 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | res = node.prev_sibling
0016 0013 0012 0000 0000 0003 0001 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | if res:
0016 0013 0012 0000 0000 0003 0001 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | if isinstance(res, Leaf):
0016 0013 0012 0000 0000 0003 0001 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | return res
0016 0013 0012 0000 0000 0003 0001 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 |
0016 0013 0012 0000 0000 0003 0001 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | try:
0016 0013 0012 0000 0000 0003 0001 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | return list(res.leaves())[-1]
0016 0013 0012 0000 0000 0003 0001 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 |
0016 0013 0012 0000 0000 0003 0001 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | except IndexError:
0016 0013 0012 0000 0000 0003 0001 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | return None
0016 0013 0012 0000 0000 0003 0001 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 |
0016 0013 0012 0000 0000 0003 0001 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | node = node.parent
0016 0013 0012 0000 0000 0003 0001 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | return None
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0014 0012 0010 0000 0004 0000 0000 -- 05 004 007 008 011 011 019 0065.73 0206.58 0003.14 | def prev_siblings_are(node: Optional[LN], tokens: List[Optional[NodeType]]) -> bool:
0014 0012 0010 0000 0004 0000 0000 -- 05 004 007 008 011 011 019 0065.73 0206.58 0003.14 | """Return if the `node` and its previous siblings match types against the provided
0014 0012 0010 0000 0004 0000 0000 -- 05 004 007 008 011 011 019 0065.73 0206.58 0003.14 | list of tokens; the provided `node`has its type matched against the last element in
0014 0012 0010 0000 0004 0000 0000 -- 05 004 007 008 011 011 019 0065.73 0206.58 0003.14 | the list. `None` can be used as the first element to declare that the start of the
0014 0012 0010 0000 0004 0000 0000 -- 05 004 007 008 011 011 019 0065.73 0206.58 0003.14 | list is anchored at the start of its parent's children."""
0014 0012 0010 0000 0004 0000 0000 -- 05 004 007 008 011 011 019 0065.73 0206.58 0003.14 | if not tokens:
0014 0012 0010 0000 0004 0000 0000 -- 05 004 007 008 011 011 019 0065.73 0206.58 0003.14 | return True
0014 0012 0010 0000 0004 0000 0000 -- 05 004 007 008 011 011 019 0065.73 0206.58 0003.14 | if tokens[-1] is None:
0014 0012 0010 0000 0004 0000 0000 -- 05 004 007 008 011 011 019 0065.73 0206.58 0003.14 | return node is None
0014 0012 0010 0000 0004 0000 0000 -- 05 004 007 008 011 011 019 0065.73 0206.58 0003.14 | if not node:
0014 0012 0010 0000 0004 0000 0000 -- 05 004 007 008 011 011 019 0065.73 0206.58 0003.14 | return False
0014 0012 0010 0000 0004 0000 0000 -- 05 004 007 008 011 011 019 0065.73 0206.58 0003.14 | if node.type != tokens[-1]:
0014 0012 0010 0000 0004 0000 0000 -- 05 004 007 008 011 011 019 0065.73 0206.58 0003.14 | return False
0014 0012 0010 0000 0004 0000 0000 -- 05 004 007 008 011 011 019 0065.73 0206.58 0003.14 | return prev_siblings_are(node.prev_sibling, tokens[:-1])
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0011 0005 0004 0000 0006 0001 0000 -- 03 002 005 003 006 007 009 0025.27 0030.32 0001.20 | def parent_type(node: Optional[LN]) -> Optional[NodeType]:
0011 0005 0004 0000 0006 0001 0000 -- 03 002 005 003 006 007 009 0025.27 0030.32 0001.20 | """
0011 0005 0004 0000 0006 0001 0000 -- 03 002 005 003 006 007 009 0025.27 0030.32 0001.20 | Returns:
0011 0005 0004 0000 0006 0001 0000 -- 03 002 005 003 006 007 009 0025.27 0030.32 0001.20 | @node.parent.type, if @node is not None and has a parent.
0011 0005 0004 0000 0006 0001 0000 -- 03 002 005 003 006 007 009 0025.27 0030.32 0001.20 | OR
0011 0005 0004 0000 0006 0001 0000 -- 03 002 005 003 006 007 009 0025.27 0030.32 0001.20 | None, otherwise.
0011 0005 0004 0000 0006 0001 0000 -- 03 002 005 003 006 007 009 0025.27 0030.32 0001.20 | """
0011 0005 0004 0000 0006 0001 0000 -- 03 002 005 003 006 007 009 0025.27 0030.32 0001.20 | if node is None or node.parent is None:
0011 0005 0004 0000 0006 0001 0000 -- 03 002 005 003 006 007 009 0025.27 0030.32 0001.20 | return None
0011 0005 0004 0000 0006 0001 0000 -- 03 002 005 003 006 007 009 0025.27 0030.32 0001.20 |
0011 0005 0004 0000 0006 0001 0000 -- 03 002 005 003 006 007 009 0025.27 0030.32 0001.20 | return node.parent.type
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0006 0007 0005 0000 0000 0000 0001 -- 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | def child_towards(ancestor: Node, descendant: LN) -> Optional[LN]:
0006 0007 0005 0000 0000 0000 0001 -- 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | """Return the child of `ancestor` that contains `descendant`."""
0006 0007 0005 0000 0000 0000 0001 -- 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | node: Optional[LN] = descendant
0006 0007 0005 0000 0000 0000 0001 -- 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | while node and node.parent != ancestor:
0006 0007 0005 0000 0000 0000 0001 -- 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | node = node.parent
0006 0007 0005 0000 0000 0000 0001 -- 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | return node
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0015 0008 0007 0000 0007 0001 0000 -- 03 002 003 002 003 005 005 0011.61 0011.61 0001.00 | def replace_child(old_child: LN, new_child: LN) -> None:
0015 0008 0007 0000 0007 0001 0000 -- 03 002 003 002 003 005 005 0011.61 0011.61 0001.00 | """
0015 0008 0007 0000 0007 0001 0000 -- 03 002 003 002 003 005 005 0011.61 0011.61 0001.00 | Side Effects:
0015 0008 0007 0000 0007 0001 0000 -- 03 002 003 002 003 005 005 0011.61 0011.61 0001.00 | * If @old_child.parent is set, replace @old_child with @new_child in
0015 0008 0007 0000 0007 0001 0000 -- 03 002 003 002 003 005 005 0011.61 0011.61 0001.00 | @old_child's underlying Node structure.
0015 0008 0007 0000 0007 0001 0000 -- 03 002 003 002 003 005 005 0011.61 0011.61 0001.00 | OR
0015 0008 0007 0000 0007 0001 0000 -- 03 002 003 002 003 005 005 0011.61 0011.61 0001.00 | * Otherwise, this function does nothing.
0015 0008 0007 0000 0007 0001 0000 -- 03 002 003 002 003 005 005 0011.61 0011.61 0001.00 | """
0015 0008 0007 0000 0007 0001 0000 -- 03 002 003 002 003 005 005 0011.61 0011.61 0001.00 | parent = old_child.parent
0015 0008 0007 0000 0007 0001 0000 -- 03 002 003 002 003 005 005 0011.61 0011.61 0001.00 | if not parent:
0015 0008 0007 0000 0007 0001 0000 -- 03 002 003 002 003 005 005 0011.61 0011.61 0001.00 | return
0015 0008 0007 0000 0007 0001 0000 -- 03 002 003 002 003 005 005 0011.61 0011.61 0001.00 |
0015 0008 0007 0000 0007 0001 0000 -- 03 002 003 002 003 005 005 0011.61 0011.61 0001.00 | child_idx = old_child.remove()
0015 0008 0007 0000 0007 0001 0000 -- 03 002 003 002 003 005 005 0011.61 0011.61 0001.00 | if child_idx is not None:
0015 0008 0007 0000 0007 0001 0000 -- 03 002 003 002 003 005 005 0011.61 0011.61 0001.00 | parent.insert_child(child_idx, new_child)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | def container_of(leaf: Leaf) -> LN:
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | """Return `leaf` or one of its ancestors that is the topmost container of it.
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 |
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | By "container" we mean a node where `leaf` is the very first child.
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | """
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | same_prefix = leaf.prefix
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | container: LN = leaf
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | while container:
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | parent = container.parent
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | if parent is None:
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | break
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 |
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | if parent.children[0].prefix != same_prefix:
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | break
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 |
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | if parent.type == syms.file_input:
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | break
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 |
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | if parent.prev_sibling is not None and parent.prev_sibling.type in BRACKETS:
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | break
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 |
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | container = parent
0023 0017 0015 0000 0003 0005 0000 -- 07 006 010 006 012 016 018 0072.00 0259.20 0003.60 | return container
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0008 0008 0007 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def first_leaf_of(node: LN) -> Optional[Leaf]:
0008 0008 0007 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """Returns the first leaf of the node tree."""
0008 0008 0007 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | if isinstance(node, Leaf):
0008 0008 0007 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return node
0008 0008 0007 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | if node.children:
0008 0008 0007 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return first_leaf_of(node.children[0])
0008 0008 0007 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | else:
0008 0008 0007 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return None
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0008 0003 0007 0000 0000 0000 0001 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def is_arith_like(node: LN) -> bool:
0008 0003 0007 0000 0000 0000 0001 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Whether node is an arithmetic or a binary arithmetic expression"""
0008 0003 0007 0000 0000 0000 0001 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return node.type in {
0008 0003 0007 0000 0000 0000 0001 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | syms.arith_expr,
0008 0003 0007 0000 0000 0000 0001 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | syms.shift_expr,
0008 0003 0007 0000 0000 0000 0001 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | syms.xor_expr,
0008 0003 0007 0000 0000 0000 0001 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | syms.and_expr,
0008 0003 0007 0000 0000 0000 0001 -- -- 001 002 001 002 003 003 0004.75 0002.38 0000.50 | }
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | def is_docstring(leaf: Leaf, mode: Mode) -> bool:
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | if leaf.type != token.STRING:
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | return False
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 |
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | prefix = get_string_prefix(leaf.value)
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | if set(prefix).intersection("bBfF"):
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | return False
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 |
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | if (
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | Preview.unify_docstring_detection in mode
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | and leaf.parent
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | and leaf.parent.type == syms.simple_stmt
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | and not leaf.parent.prev_sibling
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | and leaf.parent.parent
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | and leaf.parent.parent.type == syms.file_input
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | ):
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | return True
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 |
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | if prev_siblings_are(
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | leaf.parent, [None, token.NEWLINE, token.INDENT, syms.simple_stmt]
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | ):
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | return True
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 |
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | # Multiline docstring on the same line as the `def`.
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | if prev_siblings_are(leaf.parent, [syms.parameters, token.COLON, syms.simple_stmt]):
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | # `syms.parameters` is only used in funcdefs and async_funcdefs in the Python
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | # grammar. We're safe to return True without further checks.
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | return True
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 |
0030 0013 0022 0003 0000 0005 0003 -- 11 005 012 006 015 017 021 0085.84 0268.24 0003.12 | return False
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0008 0003 0007 0000 0000 0000 0001 -- 04 002 010 005 012 012 017 0060.94 0073.13 0001.20 | def is_empty_tuple(node: LN) -> bool:
0008 0003 0007 0000 0000 0000 0001 -- 04 002 010 005 012 012 017 0060.94 0073.13 0001.20 | """Return True if `node` holds an empty tuple."""
0008 0003 0007 0000 0000 0000 0001 -- 04 002 010 005 012 012 017 0060.94 0073.13 0001.20 | return (
0008 0003 0007 0000 0000 0000 0001 -- 04 002 010 005 012 012 017 0060.94 0073.13 0001.20 | node.type == syms.atom
0008 0003 0007 0000 0000 0000 0001 -- 04 002 010 005 012 012 017 0060.94 0073.13 0001.20 | and len(node.children) == 2
0008 0003 0007 0000 0000 0000 0001 -- 04 002 010 005 012 012 017 0060.94 0073.13 0001.20 | and node.children[0].type == token.LPAR
0008 0003 0007 0000 0000 0000 0001 -- 04 002 010 005 012 012 017 0060.94 0073.13 0001.20 | and node.children[1].type == token.RPAR
0008 0003 0007 0000 0000 0000 0001 -- -- 002 010 005 012 012 017 0060.94 0073.13 0001.20 | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0014 0008 0011 0000 0000 0002 0001 -- 07 006 017 011 023 023 034 0153.80 0624.25 0004.06 | def is_one_tuple(node: LN) -> bool:
0014 0008 0011 0000 0000 0002 0001 -- 07 006 017 011 023 023 034 0153.80 0624.25 0004.06 | """Return True if `node` holds a tuple with one element, with or without parens."""
0014 0008 0011 0000 0000 0002 0001 -- 07 006 017 011 023 023 034 0153.80 0624.25 0004.06 | if node.type == syms.atom:
0014 0008 0011 0000 0000 0002 0001 -- 07 006 017 011 023 023 034 0153.80 0624.25 0004.06 | gexp = unwrap_singleton_parenthesis(node)
0014 0008 0011 0000 0000 0002 0001 -- 07 006 017 011 023 023 034 0153.80 0624.25 0004.06 | if gexp is None or gexp.type != syms.testlist_gexp:
0014 0008 0011 0000 0000 0002 0001 -- 07 006 017 011 023 023 034 0153.80 0624.25 0004.06 | return False
0014 0008 0011 0000 0000 0002 0001 -- 07 006 017 011 023 023 034 0153.80 0624.25 0004.06 |
0014 0008 0011 0000 0000 0002 0001 -- 07 006 017 011 023 023 034 0153.80 0624.25 0004.06 | return len(gexp.children) == 2 and gexp.children[1].type == token.COMMA
0014 0008 0011 0000 0000 0002 0001 -- 07 006 017 011 023 023 034 0153.80 0624.25 0004.06 |
0014 0008 0011 0000 0000 0002 0001 -- 07 006 017 011 023 023 034 0153.80 0624.25 0004.06 | return (
0014 0008 0011 0000 0000 0002 0001 -- 07 006 017 011 023 023 034 0153.80 0624.25 0004.06 | node.type in IMPLICIT_TUPLE
0014 0008 0011 0000 0000 0002 0001 -- 07 006 017 011 023 023 034 0153.80 0624.25 0004.06 | and len(node.children) == 2
0014 0008 0011 0000 0000 0002 0001 -- 07 006 017 011 023 023 034 0153.80 0624.25 0004.06 | and node.children[1].type == token.COMMA
0014 0008 0011 0000 0000 0002 0001 -- -- 006 017 011 023 023 034 0153.80 0624.25 0004.06 | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0009 0008 0007 0000 0000 0001 0001 -- 05 004 008 005 010 012 015 0053.77 0134.44 0002.50 | def is_tuple_containing_walrus(node: LN) -> bool:
0009 0008 0007 0000 0000 0001 0001 -- 05 004 008 005 010 012 015 0053.77 0134.44 0002.50 | """Return True if `node` holds a tuple that contains a walrus operator."""
0009 0008 0007 0000 0000 0001 0001 -- 05 004 008 005 010 012 015 0053.77 0134.44 0002.50 | if node.type != syms.atom:
0009 0008 0007 0000 0000 0001 0001 -- 05 004 008 005 010 012 015 0053.77 0134.44 0002.50 | return False
0009 0008 0007 0000 0000 0001 0001 -- 05 004 008 005 010 012 015 0053.77 0134.44 0002.50 | gexp = unwrap_singleton_parenthesis(node)
0009 0008 0007 0000 0000 0001 0001 -- 05 004 008 005 010 012 015 0053.77 0134.44 0002.50 | if gexp is None or gexp.type != syms.testlist_gexp:
0009 0008 0007 0000 0000 0001 0001 -- 05 004 008 005 010 012 015 0053.77 0134.44 0002.50 | return False
0009 0008 0007 0000 0000 0001 0001 -- 05 004 008 005 010 012 015 0053.77 0134.44 0002.50 |
0009 0008 0007 0000 0000 0001 0001 -- 05 004 008 005 010 012 015 0053.77 0134.44 0002.50 | return any(child.type == syms.namedexpr_test for child in gexp.children)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | def is_one_sequence_between(
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | opening: Leaf,
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | closing: Leaf,
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | leaves: List[Leaf],
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | brackets: Tuple[int, int] = (token.LPAR, token.RPAR),
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | ) -> bool:
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | """Return True if content between `opening` and `closing` is a one-sequence."""
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | if (opening.type, closing.type) != brackets:
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | return False
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 |
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | depth = closing.bracket_depth + 1
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | for _opening_index, leaf in enumerate(leaves):
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | if leaf is opening:
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | break
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 |
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | else:
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | raise LookupError("Opening paren not found in `leaves`")
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 |
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | commas = 0
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | _opening_index += 1
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | for leaf in leaves[_opening_index:]:
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | if leaf is closing:
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | break
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 |
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | bracket_depth = leaf.bracket_depth
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | if bracket_depth == depth and leaf.type == token.COMMA:
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | commas += 1
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | if leaf.parent and leaf.parent.type in {
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | syms.arglist,
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | syms.typedargslist,
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | }:
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | commas += 1
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | break
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 |
0035 0022 0029 0000 0000 0005 0001 -- 11 007 018 013 026 025 039 0181.11 0915.61 0005.06 | return commas < 2
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0004 0004 0003 0000 0000 0000 0001 -- 02 003 006 003 006 009 009 0028.53 0042.79 0001.50 | def is_walrus_assignment(node: LN) -> bool:
0004 0004 0003 0000 0000 0000 0001 -- 02 003 006 003 006 009 009 0028.53 0042.79 0001.50 | """Return True iff `node` is of the shape ( test := test )"""
0004 0004 0003 0000 0000 0000 0001 -- 02 003 006 003 006 009 009 0028.53 0042.79 0001.50 | inner = unwrap_singleton_parenthesis(node)
0004 0004 0003 0000 0000 0000 0001 -- 02 003 006 003 006 009 009 0028.53 0042.79 0001.50 | return inner is not None and inner.type == syms.namedexpr_test
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | def is_simple_decorator_trailer(node: LN, last: bool = False) -> bool:
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | """Return True iff `node` is a trailer valid in a simple decorator"""
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | return node.type == syms.trailer and (
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | (
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | len(node.children) == 2
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | and node.children[0].type == token.DOT
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | and node.children[1].type == token.NAME
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | )
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | # last trailer can be an argument-less parentheses pair
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | or (
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | last
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | and len(node.children) == 2
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | and node.children[0].type == token.LPAR
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | and node.children[1].type == token.RPAR
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | )
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | # last trailer can be arguments
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | or (
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | last
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | and len(node.children) == 3
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | and node.children[0].type == token.LPAR
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | # and node.children[1].type == syms.argument
0024 0003 0023 0003 0000 0000 0001 -- 12 003 026 015 036 029 051 0247.76 0514.57 0002.08 | and node.children[2].type == token.RPAR
0024 0003 0023 0003 0000 0000 0001 -- -- 003 026 015 036 029 051 0247.76 0514.57 0002.08 | )
0024 0003 0023 0003 0000 0000 0001 -- -- 003 026 015 036 029 051 0247.76 0514.57 0002.08 | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | def is_simple_decorator_expression(node: LN) -> bool:
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | """Return True iff `node` could be a 'dotted name' decorator
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 |
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | This function takes the node of the 'namedexpr_test' of the new decorator
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | grammar and test if it would be valid under the old decorator grammar.
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 |
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | The old grammar was: decorator: @ dotted_name [arguments] NEWLINE
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | The new grammar is : decorator: @ namedexpr_test NEWLINE
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | """
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | if node.type == token.NAME:
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | return True
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | if node.type == syms.power:
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | if node.children:
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | return (
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | node.children[0].type == token.NAME
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | and all(map(is_simple_decorator_trailer, node.children[1:-1]))
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | and (
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | len(node.children) < 2
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | or is_simple_decorator_trailer(node.children[-1], last=True)
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | )
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | )
0022 0009 0014 0000 0006 0002 0000 -- 07 005 011 008 015 016 023 0092.00 0313.64 0003.41 | return False
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 | def is_yield(node: LN) -> bool:
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 | """Return True if `node` holds a `yield` or `yield from` expression."""
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 | if node.type == syms.yield_expr:
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 | return True
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 |
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 | if is_name_token(node) and node.value == "yield":
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 | return True
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 |
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 | if node.type != syms.atom:
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 | return False
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 |
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 | if len(node.children) != 3:
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 | return False
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 |
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 | lpar, expr, rpar = node.children
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 | if lpar.type == token.LPAR and rpar.type == token.RPAR:
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 | return is_yield(expr)
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 |
0019 0014 0013 0000 0000 0005 0001 -- 08 003 013 008 016 016 024 0096.00 0177.23 0001.85 | return False
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 | def is_vararg(leaf: Leaf, within: Set[NodeType]) -> bool:
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 | """Return True if `leaf` is a star or double star in a vararg or kwarg.
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 |
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 | If `within` includes VARARGS_PARENTS, this applies to function signatures.
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 | If `within` includes UNPACKING_PARENTS, it applies to right hand-side
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 | extended iterable unpacking (PEP 3132) and additional unpacking
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 | generalizations (PEP 448).
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 | """
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 | if leaf.type not in VARARGS_SPECIALS or not leaf.parent:
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 | return False
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 |
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 | p = leaf.parent
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 | if p.type == syms.star_expr:
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 | # Star expressions are also used as assignment targets in extended
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 | # iterable unpacking (PEP 3132). See what its parent is instead.
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 | if not p.parent:
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 | return False
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 |
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 | p = p.parent
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 |
0021 0010 0009 0002 0006 0004 0002 -- 05 005 007 006 010 012 016 0057.36 0204.86 0003.57 | return p.type in within
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0003 0003 0002 0000 0000 0000 0001 -- 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | def is_multiline_string(leaf: Leaf) -> bool:
0003 0003 0002 0000 0000 0000 0001 -- 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | """Return True if `leaf` is a multiline string that actually spans many lines."""
0003 0003 0002 0000 0000 0000 0001 -- 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | return has_triple_quotes(leaf.value) and "\n" in leaf.value
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0005 0004 0004 0001 0000 0000 0001 -- 01 002 005 003 006 007 009 0025.27 0030.32 0001.20 | def is_parent_function_or_class(node: Node) -> bool:
0005 0004 0004 0001 0000 0000 0001 -- 01 002 005 003 006 007 009 0025.27 0030.32 0001.20 | assert node.type in {syms.suite, syms.simple_stmt}
0005 0004 0004 0001 0000 0000 0001 -- 01 002 005 003 006 007 009 0025.27 0030.32 0001.20 | assert node.parent is not None
0005 0004 0004 0001 0000 0000 0001 -- 01 002 005 003 006 007 009 0025.27 0030.32 0001.20 | # Note this works for suites / simple_stmts in async def as well
0005 0004 0004 0001 0000 0000 0001 -- 01 002 005 003 006 007 009 0025.27 0030.32 0001.20 | return node.parent.type in {syms.funcdef, syms.classdef}
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0002 0002 0002 0000 0000 0000 0000 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def is_function_or_class(node: Node) -> bool:
0002 0002 0002 0000 0000 0000 0000 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return node.type in {syms.funcdef, syms.classdef, syms.async_funcdef}
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | def is_stub_suite(node: Node, mode: Mode) -> bool:
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | """Return True if `node` is a suite with a stub body."""
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | if (
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | node.parent is not None
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | and Preview.dummy_implementations in mode
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | and not is_parent_function_or_class(node)
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | ):
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | return False
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 |
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | # If there is a comment, we want to keep it.
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | if node.prefix.strip():
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | return False
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 |
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | if (
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | len(node.children) != 4
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | or node.children[0].type != token.NEWLINE
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | or node.children[1].type != token.INDENT
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | or node.children[3].type != token.DEDENT
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | ):
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | return False
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 |
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | if node.children[3].prefix.strip():
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | return False
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 |
0025 0011 0019 0001 0000 0004 0002 -- 10 006 018 009 020 024 029 0132.96 0443.21 0003.33 | return is_stub_body(node.children[2])
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0015 0008 0012 0000 0000 0002 0001 -- 08 005 017 009 018 022 027 0120.40 0318.72 0002.65 | def is_stub_body(node: LN) -> bool:
0015 0008 0012 0000 0000 0002 0001 -- 08 005 017 009 018 022 027 0120.40 0318.72 0002.65 | """Return True if `node` is a simple statement containing an ellipsis."""
0015 0008 0012 0000 0000 0002 0001 -- 08 005 017 009 018 022 027 0120.40 0318.72 0002.65 | if not isinstance(node, Node) or node.type != syms.simple_stmt:
0015 0008 0012 0000 0000 0002 0001 -- 08 005 017 009 018 022 027 0120.40 0318.72 0002.65 | return False
0015 0008 0012 0000 0000 0002 0001 -- 08 005 017 009 018 022 027 0120.40 0318.72 0002.65 |
0015 0008 0012 0000 0000 0002 0001 -- 08 005 017 009 018 022 027 0120.40 0318.72 0002.65 | if len(node.children) != 2:
0015 0008 0012 0000 0000 0002 0001 -- 08 005 017 009 018 022 027 0120.40 0318.72 0002.65 | return False
0015 0008 0012 0000 0000 0002 0001 -- 08 005 017 009 018 022 027 0120.40 0318.72 0002.65 |
0015 0008 0012 0000 0000 0002 0001 -- 08 005 017 009 018 022 027 0120.40 0318.72 0002.65 | child = node.children[0]
0015 0008 0012 0000 0000 0002 0001 -- 08 005 017 009 018 022 027 0120.40 0318.72 0002.65 | return (
0015 0008 0012 0000 0000 0002 0001 -- 08 005 017 009 018 022 027 0120.40 0318.72 0002.65 | not child.prefix.strip()
0015 0008 0012 0000 0000 0002 0001 -- 08 005 017 009 018 022 027 0120.40 0318.72 0002.65 | and child.type == syms.atom
0015 0008 0012 0000 0000 0002 0001 -- 08 005 017 009 018 022 027 0120.40 0318.72 0002.65 | and len(child.children) == 3
0015 0008 0012 0000 0000 0002 0001 -- 08 005 017 009 018 022 027 0120.40 0318.72 0002.65 | and all(leaf == Leaf(token.DOT, ".") for leaf in child.children)
0015 0008 0012 0000 0000 0002 0001 -- -- 005 017 009 018 022 027 0120.40 0318.72 0002.65 | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0016 0006 0012 0000 0003 0001 0000 -- 08 005 015 008 019 020 027 0116.69 0369.52 0003.17 | def is_atom_with_invisible_parens(node: LN) -> bool:
0016 0006 0012 0000 0003 0001 0000 -- 08 005 015 008 019 020 027 0116.69 0369.52 0003.17 | """Given a `LN`, determines whether it's an atom `node` with invisible
0016 0006 0012 0000 0003 0001 0000 -- 08 005 015 008 019 020 027 0116.69 0369.52 0003.17 | parens. Useful in dedupe-ing and normalizing parens.
0016 0006 0012 0000 0003 0001 0000 -- 08 005 015 008 019 020 027 0116.69 0369.52 0003.17 | """
0016 0006 0012 0000 0003 0001 0000 -- 08 005 015 008 019 020 027 0116.69 0369.52 0003.17 | if isinstance(node, Leaf) or node.type != syms.atom:
0016 0006 0012 0000 0003 0001 0000 -- 08 005 015 008 019 020 027 0116.69 0369.52 0003.17 | return False
0016 0006 0012 0000 0003 0001 0000 -- 08 005 015 008 019 020 027 0116.69 0369.52 0003.17 |
0016 0006 0012 0000 0003 0001 0000 -- 08 005 015 008 019 020 027 0116.69 0369.52 0003.17 | first, last = node.children[0], node.children[-1]
0016 0006 0012 0000 0003 0001 0000 -- 08 005 015 008 019 020 027 0116.69 0369.52 0003.17 | return (
0016 0006 0012 0000 0003 0001 0000 -- 08 005 015 008 019 020 027 0116.69 0369.52 0003.17 | isinstance(first, Leaf)
0016 0006 0012 0000 0003 0001 0000 -- 08 005 015 008 019 020 027 0116.69 0369.52 0003.17 | and first.type == token.LPAR
0016 0006 0012 0000 0003 0001 0000 -- 08 005 015 008 019 020 027 0116.69 0369.52 0003.17 | and first.value == ""
0016 0006 0012 0000 0003 0001 0000 -- 08 005 015 008 019 020 027 0116.69 0369.52 0003.17 | and isinstance(last, Leaf)
0016 0006 0012 0000 0003 0001 0000 -- 08 005 015 008 019 020 027 0116.69 0369.52 0003.17 | and last.type == token.RPAR
0016 0006 0012 0000 0003 0001 0000 -- 08 005 015 008 019 020 027 0116.69 0369.52 0003.17 | and last.value == ""
0016 0006 0012 0000 0003 0001 0000 -- -- 005 015 008 019 020 027 0116.69 0369.52 0003.17 | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0002 0002 0002 0000 0000 0000 0000 -- 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def is_empty_par(leaf: Leaf) -> bool:
0002 0002 0002 0000 0000 0000 0000 -- 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return is_empty_lpar(leaf) or is_empty_rpar(leaf)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0002 0002 0002 0000 0000 0000 0000 -- 02 002 006 003 006 008 009 0027.00 0027.00 0001.00 | def is_empty_lpar(leaf: Leaf) -> bool:
0002 0002 0002 0000 0000 0000 0000 -- 02 002 006 003 006 008 009 0027.00 0027.00 0001.00 | return leaf.type == token.LPAR and leaf.value == ""
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0002 0002 0002 0000 0000 0000 0000 -- 02 002 006 003 006 008 009 0027.00 0027.00 0001.00 | def is_empty_rpar(leaf: Leaf) -> bool:
0002 0002 0002 0000 0000 0000 0000 -- 02 002 006 003 006 008 009 0027.00 0027.00 0001.00 | return leaf.type == token.RPAR and leaf.value == ""
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0012 0006 0011 0000 0000 0000 0001 -- 07 003 017 009 020 020 029 0125.34 0221.18 0001.76 | def is_import(leaf: Leaf) -> bool:
0012 0006 0011 0000 0000 0000 0001 -- 07 003 017 009 020 020 029 0125.34 0221.18 0001.76 | """Return True if the given leaf starts an import statement."""
0012 0006 0011 0000 0000 0000 0001 -- 07 003 017 009 020 020 029 0125.34 0221.18 0001.76 | p = leaf.parent
0012 0006 0011 0000 0000 0000 0001 -- 07 003 017 009 020 020 029 0125.34 0221.18 0001.76 | t = leaf.type
0012 0006 0011 0000 0000 0000 0001 -- 07 003 017 009 020 020 029 0125.34 0221.18 0001.76 | v = leaf.value
0012 0006 0011 0000 0000 0000 0001 -- 07 003 017 009 020 020 029 0125.34 0221.18 0001.76 | return bool(
0012 0006 0011 0000 0000 0000 0001 -- 07 003 017 009 020 020 029 0125.34 0221.18 0001.76 | t == token.NAME
0012 0006 0011 0000 0000 0000 0001 -- 07 003 017 009 020 020 029 0125.34 0221.18 0001.76 | and (
0012 0006 0011 0000 0000 0000 0001 -- 07 003 017 009 020 020 029 0125.34 0221.18 0001.76 | (v == "import" and p and p.type == syms.import_name)
0012 0006 0011 0000 0000 0000 0001 -- 07 003 017 009 020 020 029 0125.34 0221.18 0001.76 | or (v == "from" and p and p.type == syms.import_from)
0012 0006 0011 0000 0000 0000 0001 -- -- 003 017 009 020 020 029 0125.34 0221.18 0001.76 | )
0012 0006 0011 0000 0000 0000 0001 -- -- 003 017 009 020 020 029 0125.34 0221.18 0001.76 | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0012 0003 0011 0000 0000 0000 0001 -- 07 003 015 008 019 018 027 0112.59 0213.92 0001.90 | def is_with_or_async_with_stmt(leaf: Leaf) -> bool:
0012 0003 0011 0000 0000 0000 0001 -- 07 003 015 008 019 018 027 0112.59 0213.92 0001.90 | """Return True if the given leaf starts a with or async with statement."""
0012 0003 0011 0000 0000 0000 0001 -- 07 003 015 008 019 018 027 0112.59 0213.92 0001.90 | return bool(
0012 0003 0011 0000 0000 0000 0001 -- 07 003 015 008 019 018 027 0112.59 0213.92 0001.90 | leaf.type == token.NAME
0012 0003 0011 0000 0000 0000 0001 -- 07 003 015 008 019 018 027 0112.59 0213.92 0001.90 | and leaf.value == "with"
0012 0003 0011 0000 0000 0000 0001 -- 07 003 015 008 019 018 027 0112.59 0213.92 0001.90 | and leaf.parent
0012 0003 0011 0000 0000 0000 0001 -- 07 003 015 008 019 018 027 0112.59 0213.92 0001.90 | and leaf.parent.type == syms.with_stmt
0012 0003 0011 0000 0000 0000 0001 -- 07 003 015 008 019 018 027 0112.59 0213.92 0001.90 | ) or bool(
0012 0003 0011 0000 0000 0000 0001 -- 07 003 015 008 019 018 027 0112.59 0213.92 0001.90 | leaf.type == token.ASYNC
0012 0003 0011 0000 0000 0000 0001 -- 07 003 015 008 019 018 027 0112.59 0213.92 0001.90 | and leaf.next_sibling
0012 0003 0011 0000 0000 0000 0001 -- 07 003 015 008 019 018 027 0112.59 0213.92 0001.90 | and leaf.next_sibling.type == syms.with_stmt
0012 0003 0011 0000 0000 0000 0001 -- -- 003 015 008 019 018 027 0112.59 0213.92 0001.90 | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0011 0003 0006 0000 0004 0001 0000 -- 03 003 006 003 007 009 010 0031.70 0055.47 0001.75 | def is_async_stmt_or_funcdef(leaf: Leaf) -> bool:
0011 0003 0006 0000 0004 0001 0000 -- 03 003 006 003 007 009 010 0031.70 0055.47 0001.75 | """Return True if the given leaf starts an async def/for/with statement.
0011 0003 0006 0000 0004 0001 0000 -- 03 003 006 003 007 009 010 0031.70 0055.47 0001.75 |
0011 0003 0006 0000 0004 0001 0000 -- 03 003 006 003 007 009 010 0031.70 0055.47 0001.75 | Note that `async def` can be either an `async_stmt` or `async_funcdef`,
0011 0003 0006 0000 0004 0001 0000 -- 03 003 006 003 007 009 010 0031.70 0055.47 0001.75 | the latter is used when it has decorators.
0011 0003 0006 0000 0004 0001 0000 -- 03 003 006 003 007 009 010 0031.70 0055.47 0001.75 | """
0011 0003 0006 0000 0004 0001 0000 -- 03 003 006 003 007 009 010 0031.70 0055.47 0001.75 | return bool(
0011 0003 0006 0000 0004 0001 0000 -- 03 003 006 003 007 009 010 0031.70 0055.47 0001.75 | leaf.type == token.ASYNC
0011 0003 0006 0000 0004 0001 0000 -- 03 003 006 003 007 009 010 0031.70 0055.47 0001.75 | and leaf.parent
0011 0003 0006 0000 0004 0001 0000 -- 03 003 006 003 007 009 010 0031.70 0055.47 0001.75 | and leaf.parent.type in {syms.async_stmt, syms.async_funcdef}
0011 0003 0006 0000 0004 0001 0000 -- -- 003 006 003 007 009 010 0031.70 0055.47 0001.75 | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0008 0005 0004 0000 0004 0000 0000 -- 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | def is_type_comment(leaf: Leaf) -> bool:
0008 0005 0004 0000 0004 0000 0000 -- 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | """Return True if the given leaf is a type comment. This function should only
0008 0005 0004 0000 0004 0000 0000 -- 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | be used for general type comments (excluding ignore annotations, which should
0008 0005 0004 0000 0004 0000 0000 -- 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | use `is_type_ignore_comment`). Note that general type comments are no longer
0008 0005 0004 0000 0004 0000 0000 -- 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | used in modern version of Python, this function may be deprecated in the future."""
0008 0005 0004 0000 0004 0000 0000 -- 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | t = leaf.type
0008 0005 0004 0000 0004 0000 0000 -- 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | v = leaf.value
0008 0005 0004 0000 0004 0000 0000 -- 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | return t in {token.COMMENT, STANDALONE_COMMENT} and v.startswith("# type:")
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0005 0005 0004 0000 0000 0000 0001 -- 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | def is_type_ignore_comment(leaf: Leaf) -> bool:
0005 0005 0004 0000 0000 0000 0001 -- 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | """Return True if the given leaf is a type comment with ignore annotation."""
0005 0005 0004 0000 0000 0000 0001 -- 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | t = leaf.type
0005 0005 0004 0000 0000 0000 0001 -- 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | v = leaf.value
0005 0005 0004 0000 0000 0000 0001 -- 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | return t in {token.COMMENT, STANDALONE_COMMENT} and is_type_ignore_comment_string(v)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0004 0003 0002 0000 0002 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def is_type_ignore_comment_string(value: str) -> bool:
0004 0003 0002 0000 0002 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """Return True if the given string match with type comment with
0004 0003 0002 0000 0002 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | ignore annotation."""
0004 0003 0002 0000 0002 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return value.startswith("# type: ignore")
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0016 0010 0009 0000 0005 0002 0000 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def wrap_in_parentheses(parent: Node, child: LN, *, visible: bool = True) -> None:
0016 0010 0009 0000 0005 0002 0000 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Wrap `child` in parentheses.
0016 0010 0009 0000 0005 0002 0000 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0016 0010 0009 0000 0005 0002 0000 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | This replaces `child` with an atom holding the parentheses and the old
0016 0010 0009 0000 0005 0002 0000 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | child. That requires moving the prefix.
0016 0010 0009 0000 0005 0002 0000 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0016 0010 0009 0000 0005 0002 0000 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | If `visible` is False, the leaves will be valueless (and thus invisible).
0016 0010 0009 0000 0005 0002 0000 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """
0016 0010 0009 0000 0005 0002 0000 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | lpar = Leaf(token.LPAR, "(" if visible else "")
0016 0010 0009 0000 0005 0002 0000 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | rpar = Leaf(token.RPAR, ")" if visible else "")
0016 0010 0009 0000 0005 0002 0000 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | prefix = child.prefix
0016 0010 0009 0000 0005 0002 0000 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | child.prefix = ""
0016 0010 0009 0000 0005 0002 0000 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | index = child.remove() or 0
0016 0010 0009 0000 0005 0002 0000 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | new_child = Node(syms.atom, [lpar, child, rpar])
0016 0010 0009 0000 0005 0002 0000 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | new_child.prefix = prefix
0016 0010 0009 0000 0005 0002 0000 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | parent.insert_child(index, new_child)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0012 0008 0007 0000 0002 0003 0000 -- 04 004 008 005 009 012 014 0050.19 0112.93 0002.25 | def unwrap_singleton_parenthesis(node: LN) -> Optional[LN]:
0012 0008 0007 0000 0002 0003 0000 -- 04 004 008 005 009 012 014 0050.19 0112.93 0002.25 | """Returns `wrapped` if `node` is of the shape ( wrapped ).
0012 0008 0007 0000 0002 0003 0000 -- 04 004 008 005 009 012 014 0050.19 0112.93 0002.25 |
0012 0008 0007 0000 0002 0003 0000 -- 04 004 008 005 009 012 014 0050.19 0112.93 0002.25 | Parenthesis can be optional. Returns None otherwise"""
0012 0008 0007 0000 0002 0003 0000 -- 04 004 008 005 009 012 014 0050.19 0112.93 0002.25 | if len(node.children) != 3:
0012 0008 0007 0000 0002 0003 0000 -- 04 004 008 005 009 012 014 0050.19 0112.93 0002.25 | return None
0012 0008 0007 0000 0002 0003 0000 -- 04 004 008 005 009 012 014 0050.19 0112.93 0002.25 |
0012 0008 0007 0000 0002 0003 0000 -- 04 004 008 005 009 012 014 0050.19 0112.93 0002.25 | lpar, wrapped, rpar = node.children
0012 0008 0007 0000 0002 0003 0000 -- 04 004 008 005 009 012 014 0050.19 0112.93 0002.25 | if not (lpar.type == token.LPAR and rpar.type == token.RPAR):
0012 0008 0007 0000 0002 0003 0000 -- 04 004 008 005 009 012 014 0050.19 0112.93 0002.25 | return None
0012 0008 0007 0000 0002 0003 0000 -- 04 004 008 005 009 012 014 0050.19 0112.93 0002.25 |
0012 0008 0007 0000 0002 0003 0000 -- 04 004 008 005 009 012 014 0050.19 0112.93 0002.25 | return wrapped
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0010 0006 0005 0000 0004 0001 0000 -- 03 001 003 002 004 004 006 0012.00 0008.00 0000.67 | def ensure_visible(leaf: Leaf) -> None:
0010 0006 0005 0000 0004 0001 0000 -- 03 001 003 002 004 004 006 0012.00 0008.00 0000.67 | """Make sure parentheses are visible.
0010 0006 0005 0000 0004 0001 0000 -- 03 001 003 002 004 004 006 0012.00 0008.00 0000.67 |
0010 0006 0005 0000 0004 0001 0000 -- 03 001 003 002 004 004 006 0012.00 0008.00 0000.67 | They could be invisible as part of some statements (see
0010 0006 0005 0000 0004 0001 0000 -- 03 001 003 002 004 004 006 0012.00 0008.00 0000.67 | :func:`normalize_invisible_parens` and :func:`visit_import_from`).
0010 0006 0005 0000 0004 0001 0000 -- 03 001 003 002 004 004 006 0012.00 0008.00 0000.67 | """
0010 0006 0005 0000 0004 0001 0000 -- 03 001 003 002 004 004 006 0012.00 0008.00 0000.67 | if leaf.type == token.LPAR:
0010 0006 0005 0000 0004 0001 0000 -- 03 001 003 002 004 004 006 0012.00 0008.00 0000.67 | leaf.value = "("
0010 0006 0005 0000 0004 0001 0000 -- 03 001 003 002 004 004 006 0012.00 0008.00 0000.67 | elif leaf.type == token.RPAR:
0010 0006 0005 0000 0004 0001 0000 -- 03 001 003 002 004 004 006 0012.00 0008.00 0000.67 | leaf.value = ")"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0002 0002 0002 0000 0000 0000 0000 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def is_name_token(nl: NL) -> TypeGuard[Leaf]:
0002 0002 0002 0000 0000 0000 0000 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return nl.type == token.NAME
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0002 0002 0002 0000 0000 0000 0000 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def is_lpar_token(nl: NL) -> TypeGuard[Leaf]:
0002 0002 0002 0000 0000 0000 0000 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return nl.type == token.LPAR
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0002 0002 0002 0000 0000 0000 0000 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def is_rpar_token(nl: NL) -> TypeGuard[Leaf]:
0002 0002 0002 0000 0000 0000 0000 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return nl.type == token.RPAR
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0002 0002 0002 0000 0000 0000 0000 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def is_string_token(nl: NL) -> TypeGuard[Leaf]:
0002 0002 0002 0000 0000 0000 0000 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return nl.type == token.STRING
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0002 0002 0002 0000 0000 0000 0000 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def is_number_token(nl: NL) -> TypeGuard[Leaf]:
0002 0002 0002 0000 0000 0000 0000 -- 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return nl.type == token.NUMBER
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0010 0010 0009 0000 0000 0000 0001 -- 06 003 009 005 010 012 015 0053.77 0089.62 0001.67 | def is_part_of_annotation(leaf: Leaf) -> bool:
0010 0010 0009 0000 0000 0000 0001 -- 06 003 009 005 010 012 015 0053.77 0089.62 0001.67 | """Returns whether this leaf is part of type annotations."""
0010 0010 0009 0000 0000 0000 0001 -- 06 003 009 005 010 012 015 0053.77 0089.62 0001.67 | ancestor = leaf.parent
0010 0010 0009 0000 0000 0000 0001 -- 06 003 009 005 010 012 015 0053.77 0089.62 0001.67 | while ancestor is not None:
0010 0010 0009 0000 0000 0000 0001 -- 06 003 009 005 010 012 015 0053.77 0089.62 0001.67 | if ancestor.prev_sibling and ancestor.prev_sibling.type == token.RARROW:
0010 0010 0009 0000 0000 0000 0001 -- 06 003 009 005 010 012 015 0053.77 0089.62 0001.67 | return True
0010 0010 0009 0000 0000 0000 0001 -- 06 003 009 005 010 012 015 0053.77 0089.62 0001.67 | if ancestor.parent and ancestor.parent.type == syms.tname:
0010 0010 0009 0000 0000 0000 0001 -- 06 003 009 005 010 012 015 0053.77 0089.62 0001.67 | return True
0010 0010 0009 0000 0000 0000 0001 -- 06 003 009 005 010 012 015 0053.77 0089.62 0001.67 | ancestor = ancestor.parent
0010 0010 0009 0000 0000 0000 0001 -- 06 003 009 005 010 012 015 0053.77 0089.62 0001.67 | return False
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0008 0008 0007 0000 0000 0000 0001 -- 03 001 001 001 001 002 002 0002.00 0001.00 0000.50 | def first_leaf(node: LN) -> Optional[Leaf]:
0008 0008 0007 0000 0000 0000 0001 -- 03 001 001 001 001 002 002 0002.00 0001.00 0000.50 | """Returns the first leaf of the ancestor node."""
0008 0008 0007 0000 0000 0000 0001 -- 03 001 001 001 001 002 002 0002.00 0001.00 0000.50 | if isinstance(node, Leaf):
0008 0008 0007 0000 0000 0000 0001 -- 03 001 001 001 001 002 002 0002.00 0001.00 0000.50 | return node
0008 0008 0007 0000 0000 0000 0001 -- 03 001 001 001 001 002 002 0002.00 0001.00 0000.50 | elif not node.children:
0008 0008 0007 0000 0000 0000 0001 -- 03 001 001 001 001 002 002 0002.00 0001.00 0000.50 | return None
0008 0008 0007 0000 0000 0000 0001 -- 03 001 001 001 001 002 002 0002.00 0001.00 0000.50 | else:
0008 0008 0007 0000 0000 0000 0001 -- 03 001 001 001 001 002 002 0002.00 0001.00 0000.50 | return first_leaf(node.children[0])
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0008 0008 0007 0000 0000 0000 0001 -- 03 002 002 002 002 004 004 0008.00 0008.00 0001.00 | def last_leaf(node: LN) -> Optional[Leaf]:
0008 0008 0007 0000 0000 0000 0001 -- 03 002 002 002 002 004 004 0008.00 0008.00 0001.00 | """Returns the last leaf of the ancestor node."""
0008 0008 0007 0000 0000 0000 0001 -- 03 002 002 002 002 004 004 0008.00 0008.00 0001.00 | if isinstance(node, Leaf):
0008 0008 0007 0000 0000 0000 0001 -- 03 002 002 002 002 004 004 0008.00 0008.00 0001.00 | return node
0008 0008 0007 0000 0000 0000 0001 -- 03 002 002 002 002 004 004 0008.00 0008.00 0001.00 | elif not node.children:
0008 0008 0007 0000 0000 0000 0001 -- 03 002 002 002 002 004 004 0008.00 0008.00 0001.00 | return None
0008 0008 0007 0000 0000 0000 0001 -- 03 002 002 002 002 004 004 0008.00 0008.00 0001.00 | else:
0008 0008 0007 0000 0000 0000 0001 -- 03 002 002 002 002 004 004 0008.00 0008.00 0001.00 | return last_leaf(node.children[-1])
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0006 0007 0005 0000 0000 0000 0001 -- 04 003 006 003 006 009 009 0028.53 0042.79 0001.50 | def furthest_ancestor_with_last_leaf(leaf: Leaf) -> LN:
0006 0007 0005 0000 0000 0000 0001 -- 04 003 006 003 006 009 009 0028.53 0042.79 0001.50 | """Returns the furthest ancestor that has this leaf node as the last leaf."""
0006 0007 0005 0000 0000 0000 0001 -- 04 003 006 003 006 009 009 0028.53 0042.79 0001.50 | node: LN = leaf
0006 0007 0005 0000 0000 0000 0001 -- 04 003 006 003 006 009 009 0028.53 0042.79 0001.50 | while node.parent and node.parent.children and node is node.parent.children[-1]:
0006 0007 0005 0000 0000 0000 0001 -- 04 003 006 003 006 009 009 0028.53 0042.79 0001.50 | node = node.parent
0006 0007 0005 0000 0000 0000 0001 -- 04 003 006 003 006 009 009 0028.53 0042.79 0001.50 | return node