src/black/lines.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
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import itertools
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import math
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from dataclasses import dataclass, field
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from typing import (
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Callable,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Dict,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Iterator,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | List,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Optional,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Sequence,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Tuple,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | TypeVar,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Union,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | cast,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.brackets import COMMA_PRIORITY, DOT_PRIORITY, BracketTracker
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.mode import Mode, Preview
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.nodes import (
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | BRACKETS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | CLOSING_BRACKETS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | OPENING_BRACKETS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | STANDALONE_COMMENT,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | TEST_DESCENDANTS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | child_towards,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_docstring,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_import,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_multiline_string,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_one_sequence_between,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_type_comment,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_type_ignore_comment,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_with_or_async_with_stmt,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | make_simple_prefix,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | replace_child,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | whitespace,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.strings import str_width
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from blib2to3.pgen2 import token
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from blib2to3.pytree import Leaf, Node
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # types
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | T = TypeVar("T")
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Index = int
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | LeafID = int
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | LN = Union[Leaf, Node]
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @dataclass
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | class Line:
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | """Holds leaves and comments. Can be printed with `str(line)`."""
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | mode: Mode = field(repr=False)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | depth: int = 0
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | leaves: List[Leaf] = field(default_factory=list)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | # keys ordered like `leaves`
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | comments: Dict[LeafID, List[Leaf]] = field(default_factory=dict)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | bracket_tracker: BracketTracker = field(default_factory=BracketTracker)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | inside_brackets: bool = False
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | should_split_rhs: bool = False
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | magic_trailing_comma: Optional[Leaf] = None
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | def append(
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | self, leaf: Leaf, preformatted: bool = False, track_bracket: bool = False
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | ) -> None:
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | """Add a new `leaf` to the end of the line.
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 |
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | Unless `preformatted` is True, the `leaf` will receive a new consistent
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | whitespace prefix and metadata applied by :class:`BracketTracker`.
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | Trailing commas are maybe removed, unpacked for loop variables are
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | demoted from being delimiters.
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 |
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | Inline comments are put aside.
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | """
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | has_value = leaf.type in BRACKETS or bool(leaf.value.strip())
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | if not has_value:
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | return
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 |
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | if token.COLON == leaf.type and self.is_class_paren_empty:
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | del self.leaves[-2:]
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | if self.leaves and not preformatted:
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | # Note: at this point leaf.prefix should be empty except for
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | # imports, for which we only preserve newlines.
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | leaf.prefix += whitespace(
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | leaf,
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | complex_subscript=self.is_complex_subscript(leaf),
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | mode=self.mode,
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | )
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | if self.inside_brackets or not preformatted or track_bracket:
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | self.bracket_tracker.mark(leaf)
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | if self.mode.magic_trailing_comma:
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | if self.has_magic_trailing_comma(leaf):
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | self.magic_trailing_comma = leaf
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | elif self.has_magic_trailing_comma(leaf):
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | self.remove_trailing_comma()
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | if not self.append_comment(leaf):
0035 0019 0023 0002 0007 0003 0002 05 14 007 018 012 020 025 032 0148.60 0577.90 0003.89 | self.leaves.append(leaf)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 | def append_safe(self, leaf: Leaf, preformatted: bool = False) -> None:
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 | """Like :func:`append()` but disallow invalid standalone comment structure.
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 |
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 | Raises ValueError when any `leaf` is appended after a standalone comment
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 | or when a standalone comment is not the first leaf on the line.
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 | """
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 | if (
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 | self.bracket_tracker.depth == 0
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 | or self.bracket_tracker.any_open_for_or_lambda()
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 | ):
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 | if self.is_comment:
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 | raise ValueError("cannot append to standalone comments")
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 |
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 | if self.leaves and leaf.type == STANDALONE_COMMENT:
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 | raise ValueError(
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 | "cannot append standalone comments to a populated line"
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 | )
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 |
0019 0008 0012 0000 0004 0003 0000 05 06 003 008 004 008 011 012 0041.51 0062.27 0001.50 | self.append(leaf, preformatted=preformatted)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | @property
0004 0004 0003 0000 0000 0000 0001 05 02 002 006 003 006 008 009 0027.00 0027.00 0001.00 | def is_comment(self) -> bool:
0004 0004 0003 0000 0000 0000 0001 05 02 002 006 003 006 008 009 0027.00 0027.00 0001.00 | """Is this line a standalone comment?"""
0004 0004 0003 0000 0000 0000 0001 05 02 002 006 003 006 008 009 0027.00 0027.00 0001.00 | return len(self.leaves) == 1 and self.leaves[0].type == STANDALONE_COMMENT
0004 0004 0003 0000 0000 0000 0001 05 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | @property
0004 0004 0003 0000 0000 0000 0001 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | def is_decorator(self) -> bool:
0004 0004 0003 0000 0000 0000 0001 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | """Is this line a decorator?"""
0004 0004 0003 0000 0000 0000 0001 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | return bool(self) and self.leaves[0].type == token.AT
0004 0004 0003 0000 0000 0000 0001 05 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | @property
0004 0004 0003 0000 0000 0000 0001 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def is_import(self) -> bool:
0004 0004 0003 0000 0000 0000 0001 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Is this an import line?"""
0004 0004 0003 0000 0000 0000 0001 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return bool(self) and is_import(self.leaves[0])
0004 0004 0003 0000 0000 0000 0001 05 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | @property
0004 0004 0003 0000 0000 0000 0001 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def is_with_or_async_with_stmt(self) -> bool:
0004 0004 0003 0000 0000 0000 0001 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Is this a with_stmt line?"""
0004 0004 0003 0000 0000 0000 0001 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return bool(self) and is_with_or_async_with_stmt(self.leaves[0])
0004 0004 0003 0000 0000 0000 0001 05 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | @property
0008 0004 0007 0000 0000 0000 0001 05 03 002 007 003 007 009 010 0031.70 0031.70 0001.00 | def is_class(self) -> bool:
0008 0004 0007 0000 0000 0000 0001 05 03 002 007 003 007 009 010 0031.70 0031.70 0001.00 | """Is this line a class definition?"""
0008 0004 0007 0000 0000 0000 0001 05 03 002 007 003 007 009 010 0031.70 0031.70 0001.00 | return (
0008 0004 0007 0000 0000 0000 0001 05 03 002 007 003 007 009 010 0031.70 0031.70 0001.00 | bool(self)
0008 0004 0007 0000 0000 0000 0001 05 03 002 007 003 007 009 010 0031.70 0031.70 0001.00 | and self.leaves[0].type == token.NAME
0008 0004 0007 0000 0000 0000 0001 05 03 002 007 003 007 009 010 0031.70 0031.70 0001.00 | and self.leaves[0].value == "class"
0008 0004 0007 0000 0000 0000 0001 05 -- 002 007 003 007 009 010 0031.70 0031.70 0001.00 | )
0008 0004 0007 0000 0000 0000 0001 05 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | @property
0006 0005 0005 0000 0000 0000 0001 05 03 003 005 003 005 008 008 0024.00 0036.00 0001.50 | def is_stub_class(self) -> bool:
0006 0005 0005 0000 0000 0000 0001 05 03 003 005 003 005 008 008 0024.00 0036.00 0001.50 | """Is this line a class definition with a body consisting only of "..."?"""
0006 0005 0005 0000 0000 0000 0001 05 03 003 005 003 005 008 008 0024.00 0036.00 0001.50 | return self.is_class and self.leaves[-3:] == [
0006 0005 0005 0000 0000 0000 0001 05 03 003 005 003 005 008 008 0024.00 0036.00 0001.50 | Leaf(token.DOT, ".") for _ in range(3)
0006 0005 0005 0000 0000 0000 0001 05 -- 003 005 003 005 008 008 0024.00 0036.00 0001.50 | ]
0006 0005 0005 0000 0000 0000 0001 05 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | @property
0018 0013 0016 0000 0000 0001 0001 05 08 004 015 009 020 019 029 0123.19 0328.51 0002.67 | def is_def(self) -> bool:
0018 0013 0016 0000 0000 0001 0001 05 08 004 015 009 020 019 029 0123.19 0328.51 0002.67 | """Is this a function definition? (Also returns True for async defs.)"""
0018 0013 0016 0000 0000 0001 0001 05 08 004 015 009 020 019 029 0123.19 0328.51 0002.67 | try:
0018 0013 0016 0000 0000 0001 0001 05 08 004 015 009 020 019 029 0123.19 0328.51 0002.67 | first_leaf = self.leaves[0]
0018 0013 0016 0000 0000 0001 0001 05 08 004 015 009 020 019 029 0123.19 0328.51 0002.67 | except IndexError:
0018 0013 0016 0000 0000 0001 0001 05 08 004 015 009 020 019 029 0123.19 0328.51 0002.67 | return False
0018 0013 0016 0000 0000 0001 0001 05 08 004 015 009 020 019 029 0123.19 0328.51 0002.67 |
0018 0013 0016 0000 0000 0001 0001 05 08 004 015 009 020 019 029 0123.19 0328.51 0002.67 | try:
0018 0013 0016 0000 0000 0001 0001 05 08 004 015 009 020 019 029 0123.19 0328.51 0002.67 | second_leaf: Optional[Leaf] = self.leaves[1]
0018 0013 0016 0000 0000 0001 0001 05 08 004 015 009 020 019 029 0123.19 0328.51 0002.67 | except IndexError:
0018 0013 0016 0000 0000 0001 0001 05 08 004 015 009 020 019 029 0123.19 0328.51 0002.67 | second_leaf = None
0018 0013 0016 0000 0000 0001 0001 05 08 004 015 009 020 019 029 0123.19 0328.51 0002.67 | return (first_leaf.type == token.NAME and first_leaf.value == "def") or (
0018 0013 0016 0000 0000 0001 0001 05 08 004 015 009 020 019 029 0123.19 0328.51 0002.67 | first_leaf.type == token.ASYNC
0018 0013 0016 0000 0000 0001 0001 05 08 004 015 009 020 019 029 0123.19 0328.51 0002.67 | and second_leaf is not None
0018 0013 0016 0000 0000 0001 0001 05 08 004 015 009 020 019 029 0123.19 0328.51 0002.67 | and second_leaf.type == token.NAME
0018 0013 0016 0000 0000 0001 0001 05 08 004 015 009 020 019 029 0123.19 0328.51 0002.67 | and second_leaf.value == "def"
0018 0013 0016 0000 0000 0001 0001 05 -- 004 015 009 020 019 029 0123.19 0328.51 0002.67 | )
0018 0013 0016 0000 0000 0001 0001 05 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | @property
0006 0005 0005 0000 0000 0000 0001 05 03 004 007 004 007 011 011 0038.05 0076.11 0002.00 | def is_stub_def(self) -> bool:
0006 0005 0005 0000 0000 0000 0001 05 03 004 007 004 007 011 011 0038.05 0076.11 0002.00 | """Is this line a function definition with a body consisting only of "..."?"""
0006 0005 0005 0000 0000 0000 0001 05 03 004 007 004 007 011 011 0038.05 0076.11 0002.00 | return self.is_def and self.leaves[-4:] == [Leaf(token.COLON, ":")] + [
0006 0005 0005 0000 0000 0000 0001 05 03 004 007 004 007 011 011 0038.05 0076.11 0002.00 | Leaf(token.DOT, ".") for _ in range(3)
0006 0005 0005 0000 0000 0000 0001 05 -- 004 007 004 007 011 011 0038.05 0076.11 0002.00 | ]
0006 0005 0005 0000 0000 0000 0001 05 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | @property
0015 0004 0011 0000 0003 0001 0000 05 07 002 015 006 017 017 023 0094.01 0106.55 0001.13 | def is_class_paren_empty(self) -> bool:
0015 0004 0011 0000 0003 0001 0000 05 07 002 015 006 017 017 023 0094.01 0106.55 0001.13 | """Is this a class with no base classes but using parentheses?
0015 0004 0011 0000 0003 0001 0000 05 07 002 015 006 017 017 023 0094.01 0106.55 0001.13 |
0015 0004 0011 0000 0003 0001 0000 05 07 002 015 006 017 017 023 0094.01 0106.55 0001.13 | Those are unnecessary and should be removed.
0015 0004 0011 0000 0003 0001 0000 05 07 002 015 006 017 017 023 0094.01 0106.55 0001.13 | """
0015 0004 0011 0000 0003 0001 0000 05 07 002 015 006 017 017 023 0094.01 0106.55 0001.13 | return (
0015 0004 0011 0000 0003 0001 0000 05 07 002 015 006 017 017 023 0094.01 0106.55 0001.13 | bool(self)
0015 0004 0011 0000 0003 0001 0000 05 07 002 015 006 017 017 023 0094.01 0106.55 0001.13 | and len(self.leaves) == 4
0015 0004 0011 0000 0003 0001 0000 05 07 002 015 006 017 017 023 0094.01 0106.55 0001.13 | and self.is_class
0015 0004 0011 0000 0003 0001 0000 05 07 002 015 006 017 017 023 0094.01 0106.55 0001.13 | and self.leaves[2].type == token.LPAR
0015 0004 0011 0000 0003 0001 0000 05 07 002 015 006 017 017 023 0094.01 0106.55 0001.13 | and self.leaves[2].value == "("
0015 0004 0011 0000 0003 0001 0000 05 07 002 015 006 017 017 023 0094.01 0106.55 0001.13 | and self.leaves[3].type == token.RPAR
0015 0004 0011 0000 0003 0001 0000 05 07 002 015 006 017 017 023 0094.01 0106.55 0001.13 | and self.leaves[3].value == ")"
0015 0004 0011 0000 0003 0001 0000 05 -- 002 015 006 017 017 023 0094.01 0106.55 0001.13 | )
0015 0004 0011 0000 0003 0001 0000 05 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | @property
0013 0011 0012 0000 0000 0000 0001 05 06 005 009 005 009 014 014 0053.30 0133.26 0002.50 | def _is_triple_quoted_string(self) -> bool:
0013 0011 0012 0000 0000 0000 0001 05 06 005 009 005 009 014 014 0053.30 0133.26 0002.50 | """Is the line a triple quoted string?"""
0013 0011 0012 0000 0000 0000 0001 05 06 005 009 005 009 014 014 0053.30 0133.26 0002.50 | if not self or self.leaves[0].type != token.STRING:
0013 0011 0012 0000 0000 0000 0001 05 06 005 009 005 009 014 014 0053.30 0133.26 0002.50 | return False
0013 0011 0012 0000 0000 0000 0001 05 06 005 009 005 009 014 014 0053.30 0133.26 0002.50 | value = self.leaves[0].value
0013 0011 0012 0000 0000 0000 0001 05 06 005 009 005 009 014 014 0053.30 0133.26 0002.50 | if value.startswith(('"""', "'''")):
0013 0011 0012 0000 0000 0000 0001 05 06 005 009 005 009 014 014 0053.30 0133.26 0002.50 | return True
0013 0011 0012 0000 0000 0000 0001 05 06 005 009 005 009 014 014 0053.30 0133.26 0002.50 | if Preview.accept_raw_docstrings in self.mode and value.startswith(
0013 0011 0012 0000 0000 0000 0001 05 06 005 009 005 009 014 014 0053.30 0133.26 0002.50 | ("r'''", 'r"""', "R'''", 'R"""')
0013 0011 0012 0000 0000 0000 0001 05 06 005 009 005 009 014 014 0053.30 0133.26 0002.50 | ):
0013 0011 0012 0000 0000 0000 0001 05 06 005 009 005 009 014 014 0053.30 0133.26 0002.50 | return True
0013 0011 0012 0000 0000 0000 0001 05 06 005 009 005 009 014 014 0053.30 0133.26 0002.50 | return False
0013 0011 0012 0000 0000 0000 0001 05 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | @property
0006 0006 0005 0000 0000 0000 0001 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | def is_docstring(self) -> bool:
0006 0006 0005 0000 0000 0000 0001 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | """Is the line a docstring?"""
0006 0006 0005 0000 0000 0000 0001 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | if Preview.unify_docstring_detection not in self.mode:
0006 0006 0005 0000 0000 0000 0001 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | return self._is_triple_quoted_string
0006 0006 0005 0000 0000 0000 0001 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | return bool(self) and is_docstring(self.leaves[0], self.mode)
0006 0006 0005 0000 0000 0000 0001 05 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | @property
0004 0004 0003 0000 0000 0000 0001 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def is_chained_assignment(self) -> bool:
0004 0004 0003 0000 0000 0000 0001 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Is the line a chained assignment"""
0004 0004 0003 0000 0000 0000 0001 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return [leaf.type for leaf in self.leaves].count(token.EQUAL) > 1
0004 0004 0003 0000 0000 0000 0001 05 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | @property
0006 0006 0005 0000 0000 0000 0001 05 02 002 005 003 005 007 008 0022.46 0022.46 0001.00 | def opens_block(self) -> bool:
0006 0006 0005 0000 0000 0000 0001 05 02 002 005 003 005 007 008 0022.46 0022.46 0001.00 | """Does this line open a new level of indentation."""
0006 0006 0005 0000 0000 0000 0001 05 02 002 005 003 005 007 008 0022.46 0022.46 0001.00 | if len(self.leaves) == 0:
0006 0006 0005 0000 0000 0000 0001 05 02 002 005 003 005 007 008 0022.46 0022.46 0001.00 | return False
0006 0006 0005 0000 0000 0000 0001 05 02 002 005 003 005 007 008 0022.46 0022.46 0001.00 | return self.leaves[-1].type == token.COLON
0006 0006 0005 0000 0000 0000 0001 05 -- --- --- --- --- --- --- ------- ------- ------- |
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 | def is_fmt_pass_converted(
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 | self, *, first_leaf_matches: Optional[Callable[[Leaf], bool]] = None
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 | ) -> bool:
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 | """Is this line converted from fmt off/skip code?
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 |
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 | If first_leaf_matches is not None, it only returns True if the first
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 | leaf of converted code matches.
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 | """
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 | if len(self.leaves) != 1:
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 | return False
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 | leaf = self.leaves[0]
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 | if (
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 | leaf.type != STANDALONE_COMMENT
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 | or leaf.fmt_pass_converted_first_leaf is None
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 | ):
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 | return False
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 | return first_leaf_matches is None or first_leaf_matches(
0019 0008 0014 0000 0004 0001 0000 05 05 003 011 006 012 014 018 0068.53 0112.14 0001.64 | leaf.fmt_pass_converted_first_leaf
0019 0008 0014 0000 0004 0001 0000 05 -- 003 011 006 012 014 018 0068.53 0112.14 0001.64 | )
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0007 0006 0005 0000 0000 0001 0001 05 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def contains_standalone_comments(self) -> bool:
0007 0006 0005 0000 0000 0001 0001 05 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """If so, needs to be split before emitting."""
0007 0006 0005 0000 0000 0001 0001 05 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | for leaf in self.leaves:
0007 0006 0005 0000 0000 0001 0001 05 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | if leaf.type == STANDALONE_COMMENT:
0007 0006 0005 0000 0000 0001 0001 05 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return True
0007 0006 0005 0000 0000 0001 0001 05 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0007 0006 0005 0000 0000 0001 0001 05 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return False
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0014 0012 0013 0000 0000 0000 0001 05 06 002 004 002 004 006 006 0015.51 0015.51 0001.00 | def contains_implicit_multiline_string_with_comments(self) -> bool:
0014 0012 0013 0000 0000 0000 0001 05 06 002 004 002 004 006 006 0015.51 0015.51 0001.00 | """Chck if we have an implicit multiline string with comments on the line"""
0014 0012 0013 0000 0000 0000 0001 05 06 002 004 002 004 006 006 0015.51 0015.51 0001.00 | for leaf_type, leaf_group_iterator in itertools.groupby(
0014 0012 0013 0000 0000 0000 0001 05 06 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.leaves, lambda leaf: leaf.type
0014 0012 0013 0000 0000 0000 0001 05 06 002 004 002 004 006 006 0015.51 0015.51 0001.00 | ):
0014 0012 0013 0000 0000 0000 0001 05 06 002 004 002 004 006 006 0015.51 0015.51 0001.00 | if leaf_type != token.STRING:
0014 0012 0013 0000 0000 0000 0001 05 06 002 004 002 004 006 006 0015.51 0015.51 0001.00 | continue
0014 0012 0013 0000 0000 0000 0001 05 06 002 004 002 004 006 006 0015.51 0015.51 0001.00 | leaf_list = list(leaf_group_iterator)
0014 0012 0013 0000 0000 0000 0001 05 06 002 004 002 004 006 006 0015.51 0015.51 0001.00 | if len(leaf_list) == 1:
0014 0012 0013 0000 0000 0000 0001 05 06 002 004 002 004 006 006 0015.51 0015.51 0001.00 | continue
0014 0012 0013 0000 0000 0000 0001 05 06 002 004 002 004 006 006 0015.51 0015.51 0001.00 | for leaf in leaf_list:
0014 0012 0013 0000 0000 0000 0001 05 06 002 004 002 004 006 006 0015.51 0015.51 0001.00 | if self.comments_after(leaf):
0014 0012 0013 0000 0000 0000 0001 05 06 002 004 002 004 006 006 0015.51 0015.51 0001.00 | return True
0014 0012 0013 0000 0000 0000 0001 05 06 002 004 002 004 006 006 0015.51 0015.51 0001.00 | return False
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | def contains_uncollapsable_type_comments(self) -> bool:
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | ignored_ids = set()
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | try:
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | last_leaf = self.leaves[-1]
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | ignored_ids.add(id(last_leaf))
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | if last_leaf.type == token.COMMA or (
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | last_leaf.type == token.RPAR and not last_leaf.value
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | ):
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | # When trailing commas or optional parens are inserted by Black for
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | # consistency, comments after the previous last element are not moved
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | # (they don't have to, rendering will still be correct). So we ignore
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | # trailing commas and invisible.
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | last_leaf = self.leaves[-2]
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | ignored_ids.add(id(last_leaf))
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | except IndexError:
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | return False
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 |
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | # A type comment is uncollapsable if it is attached to a leaf
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | # that isn't at the end of the line (since that could cause it
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | # to get associated to a different argument) or if there are
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | # comments before it (since that could cause it to get hidden
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | # behind a comment.
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | comment_seen = False
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | for leaf_id, comments in self.comments.items():
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | for comment in comments:
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | if is_type_comment(comment):
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | if comment_seen or (
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | not is_type_ignore_comment(comment)
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | and leaf_id not in ignored_ids
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | ):
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | return True
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 |
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | comment_seen = True
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 |
0035 0018 0023 0009 0000 0003 0009 05 11 006 017 011 018 023 029 0131.18 0416.70 0003.18 | return False
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | def contains_unsplittable_type_ignore(self) -> bool:
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | if not self.leaves:
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | return False
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 |
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | # If a 'type: ignore' is attached to the end of a line, we
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | # can't split the line, because we can't know which of the
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | # subexpressions the ignore was meant to apply to.
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | #
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | # We only want this to apply to actual physical lines from the
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | # original source, though: we don't want the presence of a
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | # 'type: ignore' at the end of a multiline expression to
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | # justify pushing it all onto one line. Thus we
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | # (unfortunately) need to check the actual source lines and
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | # only report an unsplittable 'type: ignore' if this line was
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | # one line in the original code.
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 |
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | # Grab the first and last line numbers, skipping generated leaves
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | first_line = next((leaf.lineno for leaf in self.leaves if leaf.lineno != 0), 0)
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | last_line = next(
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | (leaf.lineno for leaf in reversed(self.leaves) if leaf.lineno != 0), 0
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | )
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 |
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | if first_line == last_line:
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | # We look at the last two leaves since a comma or an
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | # invisible paren could have been added at the end of the
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | # line.
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | for node in self.leaves[-2:]:
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | for comment in self.comments.get(id(node), []):
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | if is_type_ignore_comment(comment):
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | return True
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 |
0032 0011 0013 0015 0000 0004 0015 05 10 004 006 005 008 010 013 0043.19 0115.16 0002.67 | return False
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0002 0002 0002 0000 0000 0000 0000 05 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def contains_multiline_strings(self) -> bool:
0002 0002 0002 0000 0000 0000 0000 05 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return any(is_multiline_string(leaf) for leaf in self.leaves)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | def has_magic_trailing_comma(self, closing: Leaf) -> bool:
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | """Return True if we have a magic trailing comma, that is when:
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | - there's a trailing comma here
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | - it's not from single-element square bracket indexing
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | - it's not a one-tuple
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | """
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | if not (
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | closing.type in CLOSING_BRACKETS
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | and self.leaves
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | and self.leaves[-1].type == token.COMMA
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | ):
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | return False
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 |
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | if closing.type == token.RBRACE:
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | return True
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 |
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | if closing.type == token.RSQB:
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | if (
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | closing.parent is not None
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | and closing.parent.type == syms.trailer
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | and closing.opening_bracket is not None
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | and is_one_sequence_between(
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | closing.opening_bracket,
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | closing,
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | self.leaves,
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | brackets=(token.LSQB, token.RSQB),
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | )
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | ):
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | assert closing.prev_sibling is not None
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | assert closing.prev_sibling.type == syms.subscriptlist
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | return False
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 |
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | return True
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 |
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | if self.is_import:
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | return True
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 |
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | if closing.opening_bracket is not None and not is_one_sequence_between(
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | closing.opening_bracket, closing, self.leaves
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | ):
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | return True
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 |
0043 0017 0032 0000 0005 0006 0000 05 13 006 023 016 032 029 048 0233.18 0973.29 0004.17 | return False
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | def append_comment(self, comment: Leaf) -> bool:
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | """Add an inline or standalone comment to the line."""
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | if (
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | comment.type == STANDALONE_COMMENT
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | and self.bracket_tracker.any_open_brackets()
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | ):
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | comment.prefix = ""
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | return False
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 |
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | if comment.type != token.COMMENT:
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | return False
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 |
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | if not self.leaves:
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | comment.type = STANDALONE_COMMENT
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | comment.prefix = ""
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | return False
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 |
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | last_leaf = self.leaves[-1]
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | if (
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | last_leaf.type == token.RPAR
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | and not last_leaf.value
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | and last_leaf.parent
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | and len(list(last_leaf.parent.leaves())) <= 3
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | and not is_type_comment(comment)
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | ):
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | # Comments on an optional parens wrapping a single leaf should belong to
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | # the wrapped node except if it's a type comment. Pinning the comment like
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | # this avoids unstable formatting caused by comment migration.
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | if len(self.leaves) < 2:
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | comment.type = STANDALONE_COMMENT
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | comment.prefix = ""
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | return False
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 |
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | last_leaf = self.leaves[-2]
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | self.comments.setdefault(id(last_leaf), []).append(comment)
0036 0020 0028 0003 0000 0004 0004 05 11 007 019 012 022 026 034 0159.81 0647.67 0004.05 | return True
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0003 0003 0002 0000 0000 0000 0001 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def comments_after(self, leaf: Leaf) -> List[Leaf]:
0003 0003 0002 0000 0000 0000 0001 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """Generate comments that should appear directly after `leaf`."""
0003 0003 0002 0000 0000 0000 0001 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return self.comments.get(id(leaf), [])
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0007 0005 0006 0000 0000 0000 0001 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | def remove_trailing_comma(self) -> None:
0007 0005 0006 0000 0000 0000 0001 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | """Remove the trailing comma and moves the comments attached to it."""
0007 0005 0006 0000 0000 0000 0001 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | trailing_comma = self.leaves.pop()
0007 0005 0006 0000 0000 0000 0001 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | trailing_comma_comments = self.comments.pop(id(trailing_comma), [])
0007 0005 0006 0000 0000 0000 0001 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | self.comments.setdefault(id(self.leaves[-1]), []).extend(
0007 0005 0006 0000 0000 0000 0001 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | trailing_comma_comments
0007 0005 0006 0000 0000 0000 0001 05 -- 001 001 001 001 002 002 0002.00 0001.00 0000.50 | )
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | def is_complex_subscript(self, leaf: Leaf) -> bool:
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | """Return True iff `leaf` is part of a slice with non-trivial exprs."""
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | open_lsqb = self.bracket_tracker.get_open_lsqb()
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | if open_lsqb is None:
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | return False
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 |
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | subscript_start = open_lsqb.next_sibling
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 |
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | if isinstance(subscript_start, Node):
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | if subscript_start.type == syms.listmaker:
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | return False
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 |
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | if subscript_start.type == syms.subscriptlist:
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | subscript_start = child_towards(subscript_start, leaf)
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 |
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | # When this is moved out of preview, add syms.namedexpr_test directly to
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | # TEST_DESCENDANTS in nodes.py
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | if Preview.walrus_subscript in self.mode:
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | test_decendants = TEST_DESCENDANTS | {syms.namedexpr_test}
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | else:
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | test_decendants = TEST_DESCENDANTS
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | return subscript_start is not None and any(
0024 0016 0017 0002 0000 0004 0003 05 08 006 013 008 016 019 024 0101.95 0376.43 0003.69 | n.type in test_decendants for n in subscript_start.pre_order()
0024 0016 0017 0002 0000 0004 0003 05 -- 006 013 008 016 019 024 0101.95 0376.43 0003.69 | )
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 | def enumerate_with_length(
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 | self, is_reversed: bool = False
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 | ) -> Iterator[Tuple[Index, Leaf, int]]:
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 | """Return an enumeration of leaves with their length.
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 |
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 | Stops prematurely on multiline strings and standalone comments.
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 | """
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 | op = cast(
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 | Callable[[Sequence[Leaf]], Iterator[Tuple[Index, Leaf]]],
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 | enumerate_reversed if is_reversed else enumerate,
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 | )
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 | for index, leaf in op(self.leaves):
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 | length = len(leaf.prefix) + len(leaf.value)
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 | if "\n" in leaf.value:
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 | return # Multiline strings, we can't continue.
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 |
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 | for comment in self.comments_after(leaf):
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 | length += len(comment.value)
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 |
0020 0010 0014 0001 0003 0003 0000 05 05 002 006 003 006 008 009 0027.00 0027.00 0001.00 | yield index, leaf, length
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0008 0002 0008 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def clone(self) -> "Line":
0008 0002 0008 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return Line(
0008 0002 0008 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | mode=self.mode,
0008 0002 0008 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | depth=self.depth,
0008 0002 0008 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | inside_brackets=self.inside_brackets,
0008 0002 0008 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | should_split_rhs=self.should_split_rhs,
0008 0002 0008 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | magic_trailing_comma=self.magic_trailing_comma,
0008 0002 0008 0000 0000 0000 0000 05 -- 000 000 000 000 000 000 0000.00 0000.00 0000.00 | )
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0015 0013 0012 0000 0000 0002 0001 05 04 003 007 005 009 010 014 0046.51 0089.69 0001.93 | def __str__(self) -> str:
0015 0013 0012 0000 0000 0002 0001 05 04 003 007 005 009 010 014 0046.51 0089.69 0001.93 | """Render the line."""
0015 0013 0012 0000 0000 0002 0001 05 04 003 007 005 009 010 014 0046.51 0089.69 0001.93 | if not self:
0015 0013 0012 0000 0000 0002 0001 05 04 003 007 005 009 010 014 0046.51 0089.69 0001.93 | return "\n"
0015 0013 0012 0000 0000 0002 0001 05 04 003 007 005 009 010 014 0046.51 0089.69 0001.93 |
0015 0013 0012 0000 0000 0002 0001 05 04 003 007 005 009 010 014 0046.51 0089.69 0001.93 | indent = " " * self.depth
0015 0013 0012 0000 0000 0002 0001 05 04 003 007 005 009 010 014 0046.51 0089.69 0001.93 | leaves = iter(self.leaves)
0015 0013 0012 0000 0000 0002 0001 05 04 003 007 005 009 010 014 0046.51 0089.69 0001.93 | first = next(leaves)
0015 0013 0012 0000 0000 0002 0001 05 04 003 007 005 009 010 014 0046.51 0089.69 0001.93 | res = f"{first.prefix}{indent}{first.value}"
0015 0013 0012 0000 0000 0002 0001 05 04 003 007 005 009 010 014 0046.51 0089.69 0001.93 | for leaf in leaves:
0015 0013 0012 0000 0000 0002 0001 05 04 003 007 005 009 010 014 0046.51 0089.69 0001.93 | res += str(leaf)
0015 0013 0012 0000 0000 0002 0001 05 04 003 007 005 009 010 014 0046.51 0089.69 0001.93 | for comment in itertools.chain.from_iterable(self.comments.values()):
0015 0013 0012 0000 0000 0002 0001 05 04 003 007 005 009 010 014 0046.51 0089.69 0001.93 | res += str(comment)
0015 0013 0012 0000 0000 0002 0001 05 04 003 007 005 009 010 014 0046.51 0089.69 0001.93 |
0015 0013 0012 0000 0000 0002 0001 05 04 003 007 005 009 010 014 0046.51 0089.69 0001.93 | return res + "\n"
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0003 0003 0002 0000 0000 0000 0001 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def __bool__(self) -> bool:
0003 0003 0002 0000 0000 0000 0001 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Return True if the line has leaves or comments."""
0003 0003 0002 0000 0000 0000 0001 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return bool(self.leaves or self.comments)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @dataclass
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | class RHSResult:
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | """Intermediate split result from a right hand split."""
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | head: Line
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | body: Line
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | tail: Line
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | opening_bracket: Leaf
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | closing_bracket: Leaf
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @dataclass
---- ---- ---- ---- ---- ---- ---- 02 -- --- --- --- --- --- --- ------- ------- ------- | class LinesBlock:
---- ---- ---- ---- ---- ---- ---- 02 -- --- --- --- --- --- --- ------- ------- ------- | """Class that holds information about a block of formatted lines.
---- ---- ---- ---- ---- ---- ---- 02 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 02 -- --- --- --- --- --- --- ------- ------- ------- | This is introduced so that the EmptyLineTracker can look behind the standalone
---- ---- ---- ---- ---- ---- ---- 02 -- --- --- --- --- --- --- ------- ------- ------- | comments and adjust their empty lines for class or def lines.
---- ---- ---- ---- ---- ---- ---- 02 -- --- --- --- --- --- --- ------- ------- ------- | """
---- ---- ---- ---- ---- ---- ---- 02 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 02 -- --- --- --- --- --- --- ------- ------- ------- | mode: Mode
---- ---- ---- ---- ---- ---- ---- 02 -- --- --- --- --- --- --- ------- ------- ------- | previous_block: Optional["LinesBlock"]
---- ---- ---- ---- ---- ---- ---- 02 -- --- --- --- --- --- --- ------- ------- ------- | original_line: Line
---- ---- ---- ---- ---- ---- ---- 02 -- --- --- --- --- --- --- ------- ------- ------- | before: int = 0
---- ---- ---- ---- ---- ---- ---- 02 -- --- --- --- --- --- --- ------- ------- ------- | content_lines: List[str] = field(default_factory=list)
---- ---- ---- ---- ---- ---- ---- 02 -- --- --- --- --- --- --- ------- ------- ------- | after: int = 0
---- ---- ---- ---- ---- ---- ---- 02 -- --- --- --- --- --- --- ------- ------- ------- | form_feed: bool = False
---- ---- ---- ---- ---- ---- ---- 02 -- --- --- --- --- --- --- ------- ------- ------- |
0004 0004 0004 0000 0000 0000 0000 02 01 002 006 003 006 008 009 0027.00 0027.00 0001.00 | def all_lines(self) -> List[str]:
0004 0004 0004 0000 0000 0000 0000 02 01 002 006 003 006 008 009 0027.00 0027.00 0001.00 | empty_line = str(Line(mode=self.mode))
0004 0004 0004 0000 0000 0000 0000 02 01 002 006 003 006 008 009 0027.00 0027.00 0001.00 | prefix = make_simple_prefix(self.before, self.form_feed, empty_line)
0004 0004 0004 0000 0000 0000 0000 02 01 002 006 003 006 008 009 0027.00 0027.00 0001.00 | return [prefix] + self.content_lines + [empty_line * self.after]
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @dataclass
---- ---- ---- ---- ---- ---- ---- 35 -- --- --- --- --- --- --- ------- ------- ------- | class EmptyLineTracker:
---- ---- ---- ---- ---- ---- ---- 35 -- --- --- --- --- --- --- ------- ------- ------- | """Provides a stateful method that returns the number of potential extra
---- ---- ---- ---- ---- ---- ---- 35 -- --- --- --- --- --- --- ------- ------- ------- | empty lines needed before and after the currently processed line.
---- ---- ---- ---- ---- ---- ---- 35 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 35 -- --- --- --- --- --- --- ------- ------- ------- | Note: this tracker works on lines that haven't been split yet. It assumes
---- ---- ---- ---- ---- ---- ---- 35 -- --- --- --- --- --- --- ------- ------- ------- | the prefix of the first leaf consists of optional newlines. Those newlines
---- ---- ---- ---- ---- ---- ---- 35 -- --- --- --- --- --- --- ------- ------- ------- | are consumed by `maybe_empty_lines()` and included in the computation.
---- ---- ---- ---- ---- ---- ---- 35 -- --- --- --- --- --- --- ------- ------- ------- | """
---- ---- ---- ---- ---- ---- ---- 35 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 35 -- --- --- --- --- --- --- ------- ------- ------- | mode: Mode
---- ---- ---- ---- ---- ---- ---- 35 -- --- --- --- --- --- --- ------- ------- ------- | previous_line: Optional[Line] = None
---- ---- ---- ---- ---- ---- ---- 35 -- --- --- --- --- --- --- ------- ------- ------- | previous_block: Optional[LinesBlock] = None
---- ---- ---- ---- ---- ---- ---- 35 -- --- --- --- --- --- --- ------- ------- ------- | previous_defs: List[Line] = field(default_factory=list)
---- ---- ---- ---- ---- ---- ---- 35 -- --- --- --- --- --- --- ------- ------- ------- | semantic_leading_comment: Optional[LinesBlock] = None
---- ---- ---- ---- ---- ---- ---- 35 -- --- --- --- --- --- --- ------- ------- ------- |
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | def maybe_empty_lines(self, current_line: Line) -> LinesBlock:
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | """Return the number of extra empty lines before and after the `current_line`.
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 |
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | This is for separating `def`, `async def` and `class` with extra empty
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | lines (two on module-level).
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | """
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | form_feed = (
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | Preview.allow_form_feeds in self.mode
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | and current_line.depth == 0
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | and bool(current_line.leaves)
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | and "\f\n" in current_line.leaves[0].prefix
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | )
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | before, after = self._maybe_empty_lines(current_line)
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | previous_after = self.previous_block.after if self.previous_block else 0
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | before = (
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | # Black should not insert empty lines at the beginning
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | # of the file
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | 0
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | if self.previous_line is None
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | else before - previous_after
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | )
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | if (
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | Preview.module_docstring_newlines in current_line.mode
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | and self.previous_block
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | and self.previous_block.previous_block is None
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | and len(self.previous_block.original_line.leaves) == 1
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | and self.previous_block.original_line.is_docstring
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | and not (current_line.is_class or current_line.is_def)
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | ):
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | before = 1
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 |
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | block = LinesBlock(
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | mode=self.mode,
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | previous_block=self.previous_block,
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | original_line=current_line,
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | before=before,
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | after=after,
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | form_feed=form_feed,
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | )
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 |
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | # Maintain the semantic_leading_comment state.
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | if current_line.is_comment:
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | if self.previous_line is None or (
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | not self.previous_line.is_decorator
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | # `or before` means this comment already has an empty line before
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | and (not self.previous_line.is_comment or before)
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | and (self.semantic_leading_comment is None or before)
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | ):
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | self.semantic_leading_comment = block
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | # `or before` means this decorator already has an empty line before
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | elif not current_line.is_decorator or before:
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | self.semantic_leading_comment = None
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 |
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | self.previous_line = current_line
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | self.previous_block = block
0056 0017 0046 0005 0004 0004 0002 35 22 007 037 022 047 044 069 0376.70 1674.79 0004.45 | return block
---- ---- ---- ---- ---- ---- ---- 35 -- --- --- --- --- --- --- ------- ------- ------- |
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | max_allowed = 1
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | if current_line.depth == 0:
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | max_allowed = 1 if self.mode.is_pyi else 2
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | if current_line.leaves:
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | # Consume the first leaf's extra newlines.
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | first_leaf = current_line.leaves[0]
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | before = first_leaf.prefix.count("\n")
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | before = min(before, max_allowed)
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | first_leaf.prefix = ""
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | else:
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | before = 0
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 |
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | user_had_newline = bool(before)
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | depth = current_line.depth
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 |
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | previous_def = None
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | while self.previous_defs and self.previous_defs[-1].depth >= depth:
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | previous_def = self.previous_defs.pop()
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 |
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | if previous_def is not None:
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | assert self.previous_line is not None
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | if self.mode.is_pyi:
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | if (
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | Preview.blank_line_after_nested_stub_class in self.mode
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | and previous_def.is_class
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | and not previous_def.is_stub_class
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | ):
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | before = 1
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | elif depth and not current_line.is_def and self.previous_line.is_def:
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | # Empty lines between attributes and methods should be preserved.
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | before = 1 if user_had_newline else 0
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | elif depth:
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | before = 0
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | else:
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | before = 1
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | else:
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | if depth:
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | before = 1
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | elif (
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | not depth
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | and previous_def.depth
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | and current_line.leaves[-1].type == token.COLON
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | and (
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | current_line.leaves[0].value
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | not in ("with", "try", "for", "while", "if", "match")
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | )
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | ):
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | # We shouldn't add two newlines between an indented function and
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | # a dependent non-indented clause. This is to avoid issues with
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | # conditional function definitions that are technically top-level
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | # and therefore get two trailing newlines, but look weird and
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | # inconsistent when they're followed by elif, else, etc. This is
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | # worse because these functions only get *one* preceding newline
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | # already.
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | before = 1
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | else:
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | before = 2
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 |
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | if current_line.is_decorator or current_line.is_def or current_line.is_class:
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | return self._maybe_empty_lines_for_class_or_def(
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | current_line, before, user_had_newline
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | )
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 |
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | if (
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | self.previous_line
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | and self.previous_line.is_import
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | and not current_line.is_import
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | and not current_line.is_fmt_pass_converted(first_leaf_matches=is_import)
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | and depth == self.previous_line.depth
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | ):
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | return (before or 1), 0
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 |
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | if (
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | self.previous_line
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | and self.previous_line.is_class
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | and current_line.is_docstring
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | ):
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | if Preview.no_blank_line_before_class_docstring in current_line.mode:
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | return 0, 1
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | return before, 1
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 |
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | # In preview mode, always allow blank lines, except right before a function
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | # docstring
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | is_empty_first_line_ok = (
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | Preview.allow_empty_first_line_in_block in current_line.mode
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | and (
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | not current_line.is_docstring
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | or (self.previous_line and not self.previous_line.is_def)
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | )
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | )
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 |
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | if (
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | self.previous_line
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | and self.previous_line.opens_block
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | and not is_empty_first_line_ok
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | ):
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | return 0, 0
0099 0046 0080 0011 0000 0008 0011 35 40 009 041 032 064 050 096 0541.81 3805.89 0007.02 | return before, 0
---- ---- ---- ---- ---- ---- ---- 35 -- --- --- --- --- --- --- ------- ------- ------- |
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | def _maybe_empty_lines_for_class_or_def( # noqa: C901
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | self, current_line: Line, before: int, user_had_newline: bool
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | ) -> Tuple[int, int]:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | if not current_line.is_decorator:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | self.previous_defs.append(current_line)
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | if self.previous_line is None:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | # Don't insert empty lines before the first line in the file.
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | return 0, 0
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 |
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | if self.previous_line.is_decorator:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | if self.mode.is_pyi and current_line.is_stub_class:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | # Insert an empty line after a decorated stub class
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | return 0, 1
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 |
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | return 0, 0
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 |
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | if self.previous_line.depth < current_line.depth and (
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | self.previous_line.is_class or self.previous_line.is_def
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | ):
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | if self.mode.is_pyi or not Preview.allow_empty_first_line_in_block:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | return 0, 0
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | else:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | return 1 if user_had_newline else 0, 0
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 |
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | comment_to_add_newlines: Optional[LinesBlock] = None
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | if (
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | self.previous_line.is_comment
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | and self.previous_line.depth == current_line.depth
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | and before == 0
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | ):
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | slc = self.semantic_leading_comment
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | if (
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | slc is not None
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | and slc.previous_block is not None
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | and not slc.previous_block.original_line.is_class
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | and not slc.previous_block.original_line.opens_block
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | and slc.before <= 1
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | ):
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | comment_to_add_newlines = slc
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | else:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | return 0, 0
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 |
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | if self.mode.is_pyi:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | if current_line.is_class or self.previous_line.is_class:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | if self.previous_line.depth < current_line.depth:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | newlines = 0
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | elif self.previous_line.depth > current_line.depth:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | newlines = 1
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | elif current_line.is_stub_class and self.previous_line.is_stub_class:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | # No blank line between classes with an empty body
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | newlines = 0
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | else:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | newlines = 1
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | # Remove case `self.previous_line.depth > current_line.depth` below when
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | # this becomes stable.
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | #
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | # Don't inspect the previous line if it's part of the body of the previous
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | # statement in the same level, we always want a blank line if there's
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | # something with a body preceding.
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | elif (
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | Preview.blank_line_between_nested_and_def_stub_file in current_line.mode
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | and self.previous_line.depth > current_line.depth
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | ):
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | newlines = 1
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | elif (
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | current_line.is_def or current_line.is_decorator
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | ) and not self.previous_line.is_def:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | if current_line.depth:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | # In classes empty lines between attributes and methods should
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | # be preserved.
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | newlines = min(1, before)
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | else:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | # Blank line between a block of functions (maybe with preceding
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | # decorators) and a block of non-functions
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | newlines = 1
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | elif self.previous_line.depth > current_line.depth:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | newlines = 1
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | else:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | newlines = 0
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | else:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | newlines = 1 if current_line.depth else 2
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | # If a user has left no space after a dummy implementation, don't insert
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | # new lines. This is useful for instance for @overload or Protocols.
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | if (
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | Preview.dummy_implementations in self.mode
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | and self.previous_line.is_stub_def
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | and not user_had_newline
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | ):
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | newlines = 0
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | if comment_to_add_newlines is not None:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | previous_block = comment_to_add_newlines.previous_block
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | if previous_block is not None:
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | comment_to_add_newlines.before = (
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | max(comment_to_add_newlines.before, newlines) - previous_block.after
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | )
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | newlines = 0
0097 0053 0077 0016 0000 0005 0015 35 40 011 040 034 067 051 101 0572.91 5277.98 0009.21 | return newlines, 0
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0006 0006 0005 0000 0000 0000 0001 -- 02 001 003 002 004 004 006 0012.00 0008.00 0000.67 | def enumerate_reversed(sequence: Sequence[T]) -> Iterator[Tuple[Index, T]]:
0006 0006 0005 0000 0000 0000 0001 -- 02 001 003 002 004 004 006 0012.00 0008.00 0000.67 | """Like `reversed(enumerate(sequence))` if that were possible."""
0006 0006 0005 0000 0000 0000 0001 -- 02 001 003 002 004 004 006 0012.00 0008.00 0000.67 | index = len(sequence) - 1
0006 0006 0005 0000 0000 0000 0001 -- 02 001 003 002 004 004 006 0012.00 0008.00 0000.67 | for element in reversed(sequence):
0006 0006 0005 0000 0000 0000 0001 -- 02 001 003 002 004 004 006 0012.00 0008.00 0000.67 | yield (index, element)
0006 0006 0005 0000 0000 0000 0001 -- 02 001 003 002 004 004 006 0012.00 0008.00 0000.67 | index -= 1
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def append_leaves(
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | new_line: Line, old_line: Line, leaves: List[Leaf], preformatted: bool = False
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | ) -> None:
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | Append leaves (taken from @old_line) to @new_line, making sure to fix the
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | underlying Node structure where appropriate.
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | All of the leaves in @leaves are duplicated. The duplicates are then
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | appended to @new_line and used to replace their originals in the underlying
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | Node structure. Any comments attached to the old leaves are reattached to
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | the new leaves.
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | Pre-conditions:
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | set(@leaves) is a subset of set(@old_line.leaves).
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | for old_leaf in leaves:
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | new_leaf = Leaf(old_leaf.type, old_leaf.value)
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | replace_child(old_leaf, new_leaf)
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | new_line.append(new_leaf, preformatted=preformatted)
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | for comment_leaf in old_line.comments_after(old_leaf):
0022 0008 0009 0000 0010 0003 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | new_line.append(comment_leaf, preformatted=True)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | def is_line_short_enough( # noqa: C901
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | line: Line, *, mode: Mode, line_str: str = ""
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | ) -> bool:
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | """For non-multiline strings, return True if `line` is no longer than `line_length`.
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | For multiline strings, looks at the context around `line` to determine
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | if it should be inlined or split up.
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | Uses the provided `line_str` rendering, if any, otherwise computes a new one.
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | """
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | if not line_str:
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | line_str = line_to_string(line)
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 |
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | width = str_width if Preview.respect_east_asian_width in mode else len
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 |
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | if Preview.multiline_string_handling not in mode:
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | return (
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | width(line_str) <= mode.line_length
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | and "\n" not in line_str # multiline strings
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | and not line.contains_standalone_comments()
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | )
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 |
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | if line.contains_standalone_comments():
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | return False
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | if "\n" not in line_str:
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | # No multiline strings (MLS) present
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | return width(line_str) <= mode.line_length
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 |
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | first, *_, last = line_str.split("\n")
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | if width(first) > mode.line_length or width(last) > mode.line_length:
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | return False
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 |
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | # Traverse the AST to examine the context of the multiline string (MLS),
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | # tracking aspects such as depth and comma existence,
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | # to determine whether to split the MLS or keep it together.
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | # Depth (which is based on the existing bracket_depth concept)
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | # is needed to determine nesting level of the MLS.
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | # Includes special case for trailing commas.
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | commas: List[int] = [] # tracks number of commas per depth level
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | multiline_string: Optional[Leaf] = None
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | # store the leaves that contain parts of the MLS
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | multiline_string_contexts: List[LN] = []
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 |
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | max_level_to_update: Union[int, float] = math.inf # track the depth of the MLS
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | for i, leaf in enumerate(line.leaves):
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | if max_level_to_update == math.inf:
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | had_comma: Optional[int] = None
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | if leaf.bracket_depth + 1 > len(commas):
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | commas.append(0)
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | elif leaf.bracket_depth + 1 < len(commas):
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | had_comma = commas.pop()
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | if (
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | had_comma is not None
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | and multiline_string is not None
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | and multiline_string.bracket_depth == leaf.bracket_depth + 1
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | ):
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | # Have left the level with the MLS, stop tracking commas
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | max_level_to_update = leaf.bracket_depth
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | if had_comma > 0:
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | # MLS was in parens with at least one comma - force split
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | return False
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 |
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | if leaf.bracket_depth <= max_level_to_update and leaf.type == token.COMMA:
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | # Ignore non-nested trailing comma
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | # directly after MLS/MLS-containing expression
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | ignore_ctxs: List[Optional[LN]] = [None]
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | ignore_ctxs += multiline_string_contexts
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | if not (leaf.prev_sibling in ignore_ctxs and i == len(line.leaves) - 1):
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | commas[leaf.bracket_depth] += 1
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | if max_level_to_update != math.inf:
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | max_level_to_update = min(max_level_to_update, leaf.bracket_depth)
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 |
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | if is_multiline_string(leaf):
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | if len(multiline_string_contexts) > 0:
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | # >1 multiline string cannot fit on a single line - force split
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | return False
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | multiline_string = leaf
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | ctx: LN = leaf
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | # fetch the leaf components of the MLS in the AST
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | while str(ctx) in line_str:
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | multiline_string_contexts.append(ctx)
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | if ctx.parent is None:
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | break
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | ctx = ctx.parent
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 |
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | # May not have a triple-quoted multiline string at all,
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | # in case of a regular string with embedded newlines and line continuations
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | if len(multiline_string_contexts) == 0:
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | return True
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 |
0089 0056 0058 0020 0005 0010 0016 -- 29 014 051 039 077 065 116 0698.59 7383.19 0010.57 | return all(val == 0 for val in commas)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | def can_be_split(line: Line) -> bool:
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | """Return False if the line cannot be split *for sure*.
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 |
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | This is not an exhaustive search but a cheap heuristic that we can use to
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | avoid some unfortunate formattings (mostly around wrapping unsplittable code
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | in unnecessary parentheses).
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | """
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | leaves = line.leaves
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | if len(leaves) < 2:
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | return False
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 |
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | if leaves[0].type == token.STRING and leaves[1].type == token.DOT:
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | call_count = 0
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | dot_count = 0
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | next = leaves[-1]
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | for leaf in leaves[-2::-1]:
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | if leaf.type in OPENING_BRACKETS:
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | if next.type not in CLOSING_BRACKETS:
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | return False
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 |
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | call_count += 1
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | elif leaf.type == token.DOT:
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | dot_count += 1
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | elif leaf.type == token.NAME:
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | if not (next.type == token.DOT or next.type in OPENING_BRACKETS):
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | return False
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 |
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | elif leaf.type not in CLOSING_BRACKETS:
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | return False
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 |
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | if dot_count > 1 and call_count > 1:
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | return False
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 |
0034 0024 0023 0000 0005 0006 0000 -- 14 010 018 021 038 028 059 0283.63 2993.91 0010.56 | return True
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | def can_omit_invisible_parens(
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | rhs: RHSResult,
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | line_length: int,
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | ) -> bool:
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | """Does `rhs.body` have a shape safe to reformat without optional parens around it?
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 |
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | Returns True for only a subset of potentially nice looking formattings but
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | the point is to not return false positives that end up producing lines that
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | are too long.
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | """
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | line = rhs.body
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 |
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # We need optional parens in order to split standalone comments to their own lines
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # if there are no nested parens around the standalone comments
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | closing_bracket: Optional[Leaf] = None
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | for leaf in reversed(line.leaves):
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | if closing_bracket and leaf is closing_bracket.opening_bracket:
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | closing_bracket = None
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | if leaf.type == STANDALONE_COMMENT and not closing_bracket:
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | return False
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | if (
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | not closing_bracket
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | and leaf.type in CLOSING_BRACKETS
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | and leaf.opening_bracket in line.leaves
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | and leaf.value
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | ):
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | closing_bracket = leaf
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 |
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | bt = line.bracket_tracker
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | if not bt.delimiters:
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # Without delimiters the optional parentheses are useless.
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | return True
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 |
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | max_priority = bt.max_delimiter_priority()
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | delimiter_count = bt.delimiter_count_with_priority(max_priority)
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | if delimiter_count > 1:
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # With more than one delimiter of a kind the optional parentheses read better.
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | return False
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 |
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | if delimiter_count == 1:
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | if (
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | Preview.wrap_multiple_context_managers_in_parens in line.mode
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | and max_priority == COMMA_PRIORITY
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | and rhs.head.is_with_or_async_with_stmt
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | ):
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # For two context manager with statements, the optional parentheses read
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # better. In this case, `rhs.body` is the context managers part of
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # the with statement. `rhs.head` is the `with (` part on the previous
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # line.
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | return False
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # Otherwise it may also read better, but we don't do it today and requires
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # careful considerations for all possible cases. See
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # https://github.com/psf/black/issues/2156.
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 |
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | if max_priority == DOT_PRIORITY:
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # A single stranded method call doesn't require optional parentheses.
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | return True
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 |
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | assert len(line.leaves) >= 2, "Stranded delimiter"
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 |
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # With a single delimiter, omit if the expression starts or ends with
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # a bracket.
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | first = line.leaves[0]
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | second = line.leaves[1]
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | if first.type in OPENING_BRACKETS and second.type not in CLOSING_BRACKETS:
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | if _can_omit_opening_paren(line, first=first, line_length=line_length):
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | return True
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 |
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # Note: we are not returning False here because a line might have *both*
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # a leading opening bracket and a trailing closing bracket. If the
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # opening bracket doesn't match our rule, maybe the closing will.
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 |
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | penultimate = line.leaves[-2]
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | last = line.leaves[-1]
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 |
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | if (
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | last.type == token.RPAR
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | or last.type == token.RBRACE
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | or (
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # don't use indexing for omitting optional parentheses;
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # it looks weird
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | last.type == token.RSQB
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | and last.parent
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | and last.parent.type != syms.trailer
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | )
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | ):
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | if penultimate.type in OPENING_BRACKETS:
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # Empty brackets don't help.
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | return False
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 |
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | if is_multiline_string(first):
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # Additional wrapping of a multiline string in this situation is
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | # unnecessary.
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | return True
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 |
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | if _can_omit_closing_paren(line, last=last, line_length=line_length):
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | return True
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 |
0099 0040 0060 0022 0005 0014 0020 -- 28 011 040 029 058 051 087 0493.50 3935.67 0007.97 | return False
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | def _can_omit_opening_paren(line: Line, *, first: Leaf, line_length: int) -> bool:
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | """See `can_omit_invisible_parens`."""
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | remainder = False
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | length = 4 * line.depth
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | _index = -1
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | for _index, leaf, leaf_length in line.enumerate_with_length():
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | if leaf.type in CLOSING_BRACKETS and leaf.opening_bracket is first:
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | remainder = True
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | if remainder:
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | length += leaf_length
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | if length > line_length:
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | break
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 |
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | if leaf.type in OPENING_BRACKETS:
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | # There are brackets we can further split on.
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | remainder = False
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 |
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | else:
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | # checked the entire string and line length wasn't exceeded
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | if len(line.leaves) == _index + 1:
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | return True
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 |
0023 0018 0017 0002 0000 0003 0003 -- 09 008 016 010 019 024 029 0132.96 0631.58 0004.75 | return False
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0015 0012 0011 0001 0000 0002 0002 -- 06 006 011 006 012 017 018 0073.57 0240.79 0003.27 | def _can_omit_closing_paren(line: Line, *, last: Leaf, line_length: int) -> bool:
0015 0012 0011 0001 0000 0002 0002 -- 06 006 011 006 012 017 018 0073.57 0240.79 0003.27 | """See `can_omit_invisible_parens`."""
0015 0012 0011 0001 0000 0002 0002 -- 06 006 011 006 012 017 018 0073.57 0240.79 0003.27 | length = 4 * line.depth
0015 0012 0011 0001 0000 0002 0002 -- 06 006 011 006 012 017 018 0073.57 0240.79 0003.27 | seen_other_brackets = False
0015 0012 0011 0001 0000 0002 0002 -- 06 006 011 006 012 017 018 0073.57 0240.79 0003.27 | for _index, leaf, leaf_length in line.enumerate_with_length():
0015 0012 0011 0001 0000 0002 0002 -- 06 006 011 006 012 017 018 0073.57 0240.79 0003.27 | length += leaf_length
0015 0012 0011 0001 0000 0002 0002 -- 06 006 011 006 012 017 018 0073.57 0240.79 0003.27 | if leaf is last.opening_bracket:
0015 0012 0011 0001 0000 0002 0002 -- 06 006 011 006 012 017 018 0073.57 0240.79 0003.27 | if seen_other_brackets or length <= line_length:
0015 0012 0011 0001 0000 0002 0002 -- 06 006 011 006 012 017 018 0073.57 0240.79 0003.27 | return True
0015 0012 0011 0001 0000 0002 0002 -- 06 006 011 006 012 017 018 0073.57 0240.79 0003.27 |
0015 0012 0011 0001 0000 0002 0002 -- 06 006 011 006 012 017 018 0073.57 0240.79 0003.27 | elif leaf.type in OPENING_BRACKETS:
0015 0012 0011 0001 0000 0002 0002 -- 06 006 011 006 012 017 018 0073.57 0240.79 0003.27 | # There are brackets we can further split on.
0015 0012 0011 0001 0000 0002 0002 -- 06 006 011 006 012 017 018 0073.57 0240.79 0003.27 | seen_other_brackets = True
0015 0012 0011 0001 0000 0002 0002 -- 06 006 011 006 012 017 018 0073.57 0240.79 0003.27 |
0015 0012 0011 0001 0000 0002 0002 -- 06 006 011 006 012 017 018 0073.57 0240.79 0003.27 | return False
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0006 0003 0002 0000 0003 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def line_to_string(line: Line) -> str:
0006 0003 0002 0000 0003 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """Returns the string representation of @line.
0006 0003 0002 0000 0003 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0006 0003 0002 0000 0003 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | WARNING: This is known to be computationally expensive.
0006 0003 0002 0000 0003 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """
0006 0003 0002 0000 0003 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return str(line).strip("\n")