src/black/linegen.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
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | """
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Generating lines of code.
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | """
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import re
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import sys
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from dataclasses import replace
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from enum import Enum, auto
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from functools import partial, wraps
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from typing import Collection, Iterator, List, Optional, Set, Union, cast
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.brackets import (
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | COMMA_PRIORITY,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | DOT_PRIORITY,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | get_leaves_inside_matching_brackets,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | max_delimiter_priority_in_atom,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.comments import FMT_OFF, generate_comments, list_comments
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.lines import (
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Line,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | RHSResult,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | append_leaves,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | can_be_split,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | can_omit_invisible_parens,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_line_short_enough,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | line_to_string,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.mode import Feature, Mode, Preview
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.nodes import (
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ASSIGNMENTS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | BRACKETS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | CLOSING_BRACKETS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | OPENING_BRACKETS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | RARROW,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | STANDALONE_COMMENT,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | STATEMENT,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | WHITESPACE,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Visitor,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ensure_visible,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_arith_like,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_async_stmt_or_funcdef,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_atom_with_invisible_parens,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_docstring,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_empty_tuple,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_lpar_token,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_multiline_string,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_name_token,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_one_sequence_between,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_one_tuple,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_parent_function_or_class,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_rpar_token,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_stub_body,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_stub_suite,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_tuple_containing_walrus,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_type_ignore_comment_string,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_vararg,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_walrus_assignment,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_yield,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | wrap_in_parentheses,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.numerics import normalize_numeric_literal
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.strings import (
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | fix_docstring,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | get_string_prefix,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | normalize_string_prefix,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | normalize_string_quotes,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | normalize_unicode_escape_sequences,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.trans import (
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | CannotTransform,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | StringMerger,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | StringParenStripper,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | StringParenWrapper,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | StringSplitter,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Transformer,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | hug_power_op,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from blib2to3.pgen2 import token
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from blib2to3.pytree import Leaf, Node
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # types
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | LeafID = int
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | LN = Union[Leaf, Node]
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | class CannotSplit(CannotTransform):
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | """A readable split that fits the allotted line length is impossible."""
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # This isn't a dataclass because @dataclass + Generic breaks mypyc.
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # See also https://github.com/mypyc/mypyc/issues/827.
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | class LineGenerator(Visitor[Line]):
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | """Generates reformatted Line objects. Empty lines are not emitted.
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | Note: destroys the tree it's visiting by mutating prefixes of its leaves
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | in ways that will no longer stringify to valid Python code on the tree.
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | """
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0005 0006 0005 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def __init__(self, mode: Mode, features: Collection[Feature]) -> None:
0005 0006 0005 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | self.mode = mode
0005 0006 0005 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | self.features = features
0005 0006 0005 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | self.current_line: Line
0005 0006 0005 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | self.__post_init__()
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | def line(self, indent: int = 0) -> Iterator[Line]:
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | """Generate a line.
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 |
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | If the line is empty, only emit if it makes sense.
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | If the line is too long, split it first and then generate.
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 |
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | If any lines were generated, set up a new current_line.
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | """
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | if not self.current_line:
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | self.current_line.depth += indent
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | return # Line is empty, don't emit. Creating a new one unnecessary.
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 |
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | if (
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | Preview.improved_async_statements_handling in self.mode
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | and len(self.current_line.leaves) == 1
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | and is_async_stmt_or_funcdef(self.current_line.leaves[0])
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | ):
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | # Special case for async def/for/with statements. `visit_async_stmt`
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | # adds an `ASYNC` leaf then visits the child def/for/with statement
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | # nodes. Line yields from those nodes shouldn't treat the former
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | # `ASYNC` leaf as a complete line.
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | return
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 |
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | complete_line = self.current_line
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | self.current_line = Line(mode=self.mode, depth=complete_line.depth + indent)
0026 0010 0013 0005 0005 0004 0004 05 05 005 010 006 012 015 018 0070.32 0210.97 0003.00 | yield complete_line
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | def visit_default(self, node: LN) -> Iterator[Line]:
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | """Default `visit_*()` implementation. Recurses to children of `node`."""
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | if isinstance(node, Leaf):
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | any_open_brackets = self.current_line.bracket_tracker.any_open_brackets()
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | for comment in generate_comments(node):
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | if any_open_brackets:
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | # any comment within brackets is subject to splitting
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | self.current_line.append(comment)
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | elif comment.type == token.COMMENT:
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | # regular trailing comment
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | self.current_line.append(comment)
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | yield from self.line()
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 |
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | else:
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | # regular standalone comment
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | yield from self.line()
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 |
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | self.current_line.append(comment)
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | yield from self.line()
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 |
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | if any_open_brackets:
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | node.prefix = ""
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | if self.mode.string_normalization and node.type == token.STRING:
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | node.value = normalize_string_prefix(node.value)
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | node.value = normalize_string_quotes(node.value)
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | if node.type == token.NUMBER:
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | normalize_numeric_literal(node)
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | if node.type not in WHITESPACE:
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | self.current_line.append(node)
0030 0024 0023 0003 0000 0003 0004 05 10 003 007 005 010 010 015 0049.83 0106.78 0002.14 | yield from super().visit_default(node)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 | def visit_test(self, node: Node) -> Iterator[Line]:
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 | """Visit an `x if y else z` test"""
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 |
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 | if Preview.parenthesize_conditional_expressions in self.mode:
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 | already_parenthesized = (
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 | node.prev_sibling and node.prev_sibling.type == token.LPAR
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 | )
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 |
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 | if not already_parenthesized:
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 | # Similar to logic in wrap_in_parentheses
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 | lpar = Leaf(token.LPAR, "")
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 | rpar = Leaf(token.RPAR, "")
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 | prefix = node.prefix
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 | node.prefix = ""
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 | lpar.prefix = prefix
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 | node.insert_child(0, lpar)
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 | node.append_child(rpar)
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 |
0019 0013 0014 0001 0000 0003 0002 05 04 004 007 004 007 011 011 0038.05 0076.11 0002.00 | yield from self.visit_default(node)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0005 0004 0003 0001 0000 0000 0002 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | def visit_INDENT(self, node: Leaf) -> Iterator[Line]:
0005 0004 0003 0001 0000 0000 0002 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | """Increase indentation level, maybe yield a line."""
0005 0004 0003 0001 0000 0000 0002 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | # In blib2to3 INDENT never holds comments.
0005 0004 0003 0001 0000 0000 0002 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | yield from self.line(+1)
0005 0004 0003 0001 0000 0000 0002 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | yield from self.visit_default(node)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0013 0005 0004 0006 0000 0002 0007 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | def visit_DEDENT(self, node: Leaf) -> Iterator[Line]:
0013 0005 0004 0006 0000 0002 0007 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | """Decrease indentation level, maybe yield a line."""
0013 0005 0004 0006 0000 0002 0007 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | # The current line might still wait for trailing comments. At DEDENT time
0013 0005 0004 0006 0000 0002 0007 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | # there won't be any (they would be prefixes on the preceding NEWLINE).
0013 0005 0004 0006 0000 0002 0007 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | # Emit the line then.
0013 0005 0004 0006 0000 0002 0007 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | yield from self.line()
0013 0005 0004 0006 0000 0002 0007 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 |
0013 0005 0004 0006 0000 0002 0007 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | # While DEDENT has no value, its prefix may contain standalone comments
0013 0005 0004 0006 0000 0002 0007 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | # that belong to the current indentation level. Get 'em.
0013 0005 0004 0006 0000 0002 0007 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | yield from self.visit_default(node)
0013 0005 0004 0006 0000 0002 0007 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 |
0013 0005 0004 0006 0000 0002 0007 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | # Finally, emit the dedent.
0013 0005 0004 0006 0000 0002 0007 05 01 001 001 001 001 002 002 0002.00 0001.00 0000.50 | yield from self.line(-1)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | def visit_stmt(
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self, node: Node, keywords: Set[str], parens: Set[str]
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | ) -> Iterator[Line]:
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | """Visit a statement.
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 |
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | This implementation is shared for `if`, `while`, `for`, `try`, `except`,
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | `def`, `with`, `class`, `assert`, and assignments.
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 |
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | The relevant Python language `keywords` for a given statement will be
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | NAME leaves within it. This methods puts those on a separate line.
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 |
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | `parens` holds a set of string leaf values immediately after which
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | invisible parens should be put.
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | """
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | normalize_invisible_parens(
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | node, parens_after=parens, mode=self.mode, features=self.features
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | )
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | for child in node.children:
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | if is_name_token(child) and child.value in keywords:
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | yield from self.line()
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 |
0022 0007 0010 0000 0008 0004 0000 05 04 002 004 002 004 006 006 0015.51 0015.51 0001.00 | yield from self.visit(child)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0003 0003 0003 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def visit_typeparams(self, node: Node) -> Iterator[Line]:
0003 0003 0003 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | yield from self.visit_default(node)
0003 0003 0003 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | node.children[0].prefix = ""
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0003 0003 0003 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def visit_typevartuple(self, node: Node) -> Iterator[Line]:
0003 0003 0003 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | yield from self.visit_default(node)
0003 0003 0003 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | node.children[1].prefix = ""
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0003 0003 0003 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def visit_paramspec(self, node: Node) -> Iterator[Line]:
0003 0003 0003 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | yield from self.visit_default(node)
0003 0003 0003 0000 0000 0000 0000 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | node.children[1].prefix = ""
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0016 0012 0016 0000 0000 0000 0000 05 08 004 011 007 014 015 021 0082.04 0208.84 0002.55 | def visit_dictsetmaker(self, node: Node) -> Iterator[Line]:
0016 0012 0016 0000 0000 0000 0000 05 08 004 011 007 014 015 021 0082.04 0208.84 0002.55 | if Preview.wrap_long_dict_values_in_parens in self.mode:
0016 0012 0016 0000 0000 0000 0000 05 08 004 011 007 014 015 021 0082.04 0208.84 0002.55 | for i, child in enumerate(node.children):
0016 0012 0016 0000 0000 0000 0000 05 08 004 011 007 014 015 021 0082.04 0208.84 0002.55 | if i == 0:
0016 0012 0016 0000 0000 0000 0000 05 08 004 011 007 014 015 021 0082.04 0208.84 0002.55 | continue
0016 0012 0016 0000 0000 0000 0000 05 08 004 011 007 014 015 021 0082.04 0208.84 0002.55 | if node.children[i - 1].type == token.COLON:
0016 0012 0016 0000 0000 0000 0000 05 08 004 011 007 014 015 021 0082.04 0208.84 0002.55 | if child.type == syms.atom and child.children[0].type == token.LPAR:
0016 0012 0016 0000 0000 0000 0000 05 08 004 011 007 014 015 021 0082.04 0208.84 0002.55 | if maybe_make_parens_invisible_in_atom(
0016 0012 0016 0000 0000 0000 0000 05 08 004 011 007 014 015 021 0082.04 0208.84 0002.55 | child,
0016 0012 0016 0000 0000 0000 0000 05 08 004 011 007 014 015 021 0082.04 0208.84 0002.55 | parent=node,
0016 0012 0016 0000 0000 0000 0000 05 08 004 011 007 014 015 021 0082.04 0208.84 0002.55 | remove_brackets_around_comma=False,
0016 0012 0016 0000 0000 0000 0000 05 08 004 011 007 014 015 021 0082.04 0208.84 0002.55 | ):
0016 0012 0016 0000 0000 0000 0000 05 08 004 011 007 014 015 021 0082.04 0208.84 0002.55 | wrap_in_parentheses(node, child, visible=False)
0016 0012 0016 0000 0000 0000 0000 05 08 004 011 007 014 015 021 0082.04 0208.84 0002.55 | else:
0016 0012 0016 0000 0000 0000 0000 05 08 004 011 007 014 015 021 0082.04 0208.84 0002.55 | wrap_in_parentheses(node, child, visible=False)
0016 0012 0016 0000 0000 0000 0000 05 08 004 011 007 014 015 021 0082.04 0208.84 0002.55 | yield from self.visit_default(node)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | def visit_funcdef(self, node: Node) -> Iterator[Line]:
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | """Visit function definition."""
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | yield from self.line()
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 |
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | # Remove redundant brackets around return type annotation.
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | is_return_annotation = False
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | for child in node.children:
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | if child.type == token.RARROW:
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | is_return_annotation = True
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | elif is_return_annotation:
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | if child.type == syms.atom and child.children[0].type == token.LPAR:
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | if maybe_make_parens_invisible_in_atom(
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | child,
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | parent=node,
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | remove_brackets_around_comma=False,
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | ):
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | wrap_in_parentheses(node, child, visible=False)
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | else:
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | wrap_in_parentheses(node, child, visible=False)
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | is_return_annotation = False
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 |
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | for child in node.children:
0023 0016 0019 0001 0000 0002 0002 05 08 002 006 004 008 008 012 0036.00 0048.00 0001.33 | yield from self.visit(child)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0009 0006 0007 0000 0000 0001 0001 05 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def visit_match_case(self, node: Node) -> Iterator[Line]:
0009 0006 0007 0000 0000 0001 0001 05 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """Visit either a match or case statement."""
0009 0006 0007 0000 0000 0001 0001 05 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | normalize_invisible_parens(
0009 0006 0007 0000 0000 0001 0001 05 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | node, parens_after=set(), mode=self.mode, features=self.features
0009 0006 0007 0000 0000 0001 0001 05 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | )
0009 0006 0007 0000 0000 0001 0001 05 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0009 0006 0007 0000 0000 0001 0001 05 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | yield from self.line()
0009 0006 0007 0000 0000 0001 0001 05 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | for child in node.children:
0009 0006 0007 0000 0000 0001 0001 05 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | yield from self.visit(child)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0008 0006 0007 0000 0000 0000 0001 05 04 003 006 003 006 009 009 0028.53 0042.79 0001.50 | def visit_suite(self, node: Node) -> Iterator[Line]:
0008 0006 0007 0000 0000 0000 0001 05 04 003 006 003 006 009 009 0028.53 0042.79 0001.50 | """Visit a suite."""
0008 0006 0007 0000 0000 0000 0001 05 04 003 006 003 006 009 009 0028.53 0042.79 0001.50 | if (
0008 0006 0007 0000 0000 0000 0001 05 04 003 006 003 006 009 009 0028.53 0042.79 0001.50 | self.mode.is_pyi or Preview.dummy_implementations in self.mode
0008 0006 0007 0000 0000 0000 0001 05 04 003 006 003 006 009 009 0028.53 0042.79 0001.50 | ) and is_stub_suite(node, self.mode):
0008 0006 0007 0000 0000 0000 0001 05 04 003 006 003 006 009 009 0028.53 0042.79 0001.50 | yield from self.visit(node.children[2])
0008 0006 0007 0000 0000 0000 0001 05 04 003 006 003 006 009 009 0028.53 0042.79 0001.50 | else:
0008 0006 0007 0000 0000 0000 0001 05 04 003 006 003 006 009 009 0028.53 0042.79 0001.50 | yield from self.visit_default(node)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | """Visit a statement without nested statements."""
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | prev_type: Optional[int] = None
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | for child in node.children:
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | if (prev_type is None or prev_type == token.SEMI) and is_arith_like(child):
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | wrap_in_parentheses(node, child, visible=False)
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | prev_type = child.type
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 |
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | if node.parent and node.parent.type in STATEMENT:
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | if Preview.dummy_implementations in self.mode:
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | condition = is_parent_function_or_class(node)
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | else:
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | condition = self.mode.is_pyi
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | if condition and is_stub_body(node):
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | yield from self.visit_default(node)
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | else:
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | yield from self.line(+1)
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | yield from self.visit_default(node)
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | yield from self.line(-1)
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 |
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | else:
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | if (
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | not (self.mode.is_pyi or Preview.dummy_implementations in self.mode)
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | or not node.parent
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | or not is_stub_suite(node.parent, self.mode)
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | ):
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | yield from self.line()
0028 0023 0025 0000 0000 0002 0001 05 14 008 023 016 028 031 044 0217.98 1061.49 0004.87 | yield from self.visit_default(node)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 | def visit_async_stmt(self, node: Node) -> Iterator[Line]:
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 | """Visit `async def`, `async for`, `async with`."""
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 | yield from self.line()
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 |
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 | children = iter(node.children)
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 | for child in children:
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 | yield from self.visit(child)
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 |
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 | if child.type == token.ASYNC or child.type == STANDALONE_COMMENT:
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 | # STANDALONE_COMMENT happens when `# fmt: skip` is applied on the async
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 | # line.
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 | break
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 |
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 | internal_stmt = next(children)
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 | if Preview.improved_async_statements_handling in self.mode:
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 | yield from self.visit(internal_stmt)
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 | else:
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 | for child in internal_stmt.children:
0019 0014 0013 0002 0000 0003 0003 05 06 003 007 004 008 010 012 0039.86 0068.34 0001.71 | yield from self.visit(child)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0005 0005 0004 0000 0000 0000 0001 05 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def visit_decorators(self, node: Node) -> Iterator[Line]:
0005 0005 0004 0000 0000 0000 0001 05 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """Visit decorators."""
0005 0005 0004 0000 0000 0000 0001 05 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | for child in node.children:
0005 0005 0004 0000 0000 0000 0001 05 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | yield from self.line()
0005 0005 0004 0000 0000 0000 0001 05 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | yield from self.visit(child)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | def visit_power(self, node: Node) -> Iterator[Line]:
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | for idx, leaf in enumerate(node.children[:-1]):
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | next_leaf = node.children[idx + 1]
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 |
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | if not isinstance(leaf, Leaf):
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | continue
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 |
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | value = leaf.value.lower()
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | if (
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | leaf.type == token.NUMBER
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | and next_leaf.type == syms.trailer
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | # Ensure that we are in an attribute trailer
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | and next_leaf.children[0].type == token.DOT
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | # It shouldn't wrap hexadecimal, binary and octal literals
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | and not value.startswith(("0x", "0b", "0o"))
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | # It shouldn't wrap complex literals
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | and "j" not in value
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | ):
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | wrap_in_parentheses(node, leaf)
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 |
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | remove_await_parens(node)
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 |
0023 0010 0019 0003 0000 0004 0000 05 08 006 015 009 018 021 027 0118.59 0426.93 0003.60 | yield from self.visit_default(node)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0003 0003 0002 0000 0000 0000 0001 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def visit_SEMI(self, leaf: Leaf) -> Iterator[Line]:
0003 0003 0002 0000 0000 0000 0001 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """Remove a semicolon and put the other statement on a separate line."""
0003 0003 0002 0000 0000 0000 0001 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | yield from self.line()
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0004 0004 0003 0000 0000 0000 0001 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def visit_ENDMARKER(self, leaf: Leaf) -> Iterator[Line]:
0004 0004 0003 0000 0000 0000 0001 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """End of file. Process outstanding comments and end with a newline."""
0004 0004 0003 0000 0000 0000 0001 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | yield from self.visit_default(leaf)
0004 0004 0003 0000 0000 0000 0001 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | yield from self.line()
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0004 0004 0004 0000 0000 0000 0000 05 02 001 001 001 001 002 002 0002.00 0001.00 0000.50 | def visit_STANDALONE_COMMENT(self, leaf: Leaf) -> Iterator[Line]:
0004 0004 0004 0000 0000 0000 0000 05 02 001 001 001 001 002 002 0002.00 0001.00 0000.50 | if not self.current_line.bracket_tracker.any_open_brackets():
0004 0004 0004 0000 0000 0000 0000 05 02 001 001 001 001 002 002 0002.00 0001.00 0000.50 | yield from self.line()
0004 0004 0004 0000 0000 0000 0000 05 02 001 001 001 001 002 002 0002.00 0001.00 0000.50 | yield from self.visit_default(leaf)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0016 0009 0012 0000 0003 0001 0000 05 05 003 010 005 011 013 016 0059.21 0097.69 0001.65 | def visit_factor(self, node: Node) -> Iterator[Line]:
0016 0009 0012 0000 0003 0001 0000 05 05 003 010 005 011 013 016 0059.21 0097.69 0001.65 | """Force parentheses between a unary op and a binary power:
0016 0009 0012 0000 0003 0001 0000 05 05 003 010 005 011 013 016 0059.21 0097.69 0001.65 |
0016 0009 0012 0000 0003 0001 0000 05 05 003 010 005 011 013 016 0059.21 0097.69 0001.65 | -2 ** 8 -> -(2 ** 8)
0016 0009 0012 0000 0003 0001 0000 05 05 003 010 005 011 013 016 0059.21 0097.69 0001.65 | """
0016 0009 0012 0000 0003 0001 0000 05 05 003 010 005 011 013 016 0059.21 0097.69 0001.65 | _operator, operand = node.children
0016 0009 0012 0000 0003 0001 0000 05 05 003 010 005 011 013 016 0059.21 0097.69 0001.65 | if (
0016 0009 0012 0000 0003 0001 0000 05 05 003 010 005 011 013 016 0059.21 0097.69 0001.65 | operand.type == syms.power
0016 0009 0012 0000 0003 0001 0000 05 05 003 010 005 011 013 016 0059.21 0097.69 0001.65 | and len(operand.children) == 3
0016 0009 0012 0000 0003 0001 0000 05 05 003 010 005 011 013 016 0059.21 0097.69 0001.65 | and operand.children[1].type == token.DOUBLESTAR
0016 0009 0012 0000 0003 0001 0000 05 05 003 010 005 011 013 016 0059.21 0097.69 0001.65 | ):
0016 0009 0012 0000 0003 0001 0000 05 05 003 010 005 011 013 016 0059.21 0097.69 0001.65 | lpar = Leaf(token.LPAR, "(")
0016 0009 0012 0000 0003 0001 0000 05 05 003 010 005 011 013 016 0059.21 0097.69 0001.65 | rpar = Leaf(token.RPAR, ")")
0016 0009 0012 0000 0003 0001 0000 05 05 003 010 005 011 013 016 0059.21 0097.69 0001.65 | index = operand.remove() or 0
0016 0009 0012 0000 0003 0001 0000 05 05 003 010 005 011 013 016 0059.21 0097.69 0001.65 | node.insert_child(index, Node(syms.atom, [lpar, operand, rpar]))
0016 0009 0012 0000 0003 0001 0000 05 05 003 010 005 011 013 016 0059.21 0097.69 0001.65 | yield from self.visit_default(node)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0017 0007 0006 0000 0008 0003 0000 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | def visit_tname(self, node: Node) -> Iterator[Line]:
0017 0007 0006 0000 0008 0003 0000 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | """
0017 0007 0006 0000 0008 0003 0000 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | Add potential parentheses around types in function parameter lists to be made
0017 0007 0006 0000 0008 0003 0000 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | into real parentheses in case the type hint is too long to fit on a line
0017 0007 0006 0000 0008 0003 0000 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | Examples:
0017 0007 0006 0000 0008 0003 0000 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | def foo(a: int, b: float = 7): ...
0017 0007 0006 0000 0008 0003 0000 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 |
0017 0007 0006 0000 0008 0003 0000 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | ->
0017 0007 0006 0000 0008 0003 0000 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 |
0017 0007 0006 0000 0008 0003 0000 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | def foo(a: (int), b: (float) = 7): ...
0017 0007 0006 0000 0008 0003 0000 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | """
0017 0007 0006 0000 0008 0003 0000 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | if Preview.parenthesize_long_type_hints in self.mode:
0017 0007 0006 0000 0008 0003 0000 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | assert len(node.children) == 3
0017 0007 0006 0000 0008 0003 0000 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | if maybe_make_parens_invisible_in_atom(node.children[2], parent=node):
0017 0007 0006 0000 0008 0003 0000 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | wrap_in_parentheses(node, node.children[2], visible=False)
0017 0007 0006 0000 0008 0003 0000 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 |
0017 0007 0006 0000 0008 0003 0000 05 03 002 004 002 004 006 006 0015.51 0015.51 0001.00 | yield from self.visit_default(node)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | if Preview.hex_codes_in_unicode_sequences in self.mode:
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | normalize_unicode_escape_sequences(leaf)
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 |
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | if is_docstring(leaf, self.mode) and not re.search(r"\\\s*\n", leaf.value):
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # We're ignoring docstrings with backslash newline escapes because changing
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # indentation of those changes the AST representation of the code.
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | if self.mode.string_normalization:
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | docstring = normalize_string_prefix(leaf.value)
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # visit_default() does handle string normalization for us, but
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # since this method acts differently depending on quote style (ex.
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # see padding logic below), there's a possibility for unstable
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # formatting as visit_default() is called *after*. To avoid a
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # situation where this function formats a docstring differently on
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # the second pass, normalize it early.
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | docstring = normalize_string_quotes(docstring)
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | else:
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | docstring = leaf.value
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | prefix = get_string_prefix(docstring)
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | docstring = docstring[len(prefix) :] # Remove the prefix
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | quote_char = docstring[0]
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # A natural way to remove the outer quotes is to do:
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # docstring = docstring.strip(quote_char)
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # but that breaks on """""x""" (which is '""x').
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # So we actually need to remove the first character and the next two
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # characters but only if they are the same as the first.
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | quote_len = 1 if docstring[1] != quote_char else 3
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | docstring = docstring[quote_len:-quote_len]
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | docstring_started_empty = not docstring
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | indent = " " * 4 * self.current_line.depth
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 |
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | if is_multiline_string(leaf):
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | docstring = fix_docstring(docstring, indent)
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | else:
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | docstring = docstring.strip()
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 |
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | has_trailing_backslash = False
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | if docstring:
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # Add some padding if the docstring starts / ends with a quote mark.
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | if docstring[0] == quote_char:
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | docstring = " " + docstring
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | if docstring[-1] == quote_char:
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | docstring += " "
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | if docstring[-1] == "\\":
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | backslash_count = len(docstring) - len(docstring.rstrip("\\"))
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | if backslash_count % 2:
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # Odd number of tailing backslashes, add some padding to
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # avoid escaping the closing string quote.
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | docstring += " "
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | has_trailing_backslash = True
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | elif not docstring_started_empty:
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | docstring = " "
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 |
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # We could enforce triple quotes at this point.
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | quote = quote_char * quote_len
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 |
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # It's invalid to put closing single-character quotes on a new line.
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | if quote_len == 3:
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # We need to find the length of the last line of the docstring
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # to find if we can add the closing quotes to the line without
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # exceeding the maximum line length.
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # If docstring is one line, we don't put the closing quotes on a
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # separate line because it looks ugly (#3320).
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | lines = docstring.splitlines()
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | last_line_length = len(lines[-1]) if docstring else 0
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 |
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # If adding closing quotes would cause the last line to exceed
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # the maximum line length then put a line break before the
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | # closing quotes
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | if (
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | len(lines) > 1
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | and last_line_length + quote_len > self.mode.line_length
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | and len(indent) + quote_len <= self.mode.line_length
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | and not has_trailing_backslash
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | ):
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | leaf.value = prefix + quote + docstring + "\n" + indent + quote
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | else:
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | leaf.value = prefix + quote + docstring + quote
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | else:
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | leaf.value = prefix + quote + docstring + quote
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 |
0082 0046 0049 0027 0000 0007 0026 05 19 012 047 040 074 059 114 0670.62 6335.23 0009.45 | yield from self.visit_default(leaf)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | def __post_init__(self) -> None:
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | """You are in a twisty little maze of passages."""
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.current_line = Line(mode=self.mode)
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 |
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | v = self.visit_stmt
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | Ø: Set[str] = set()
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.visit_assert_stmt = partial(v, keywords={"assert"}, parens={"assert", ","})
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.visit_if_stmt = partial(
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | v, keywords={"if", "else", "elif"}, parens={"if", "elif"}
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | )
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.visit_while_stmt = partial(v, keywords={"while", "else"}, parens={"while"})
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.visit_for_stmt = partial(v, keywords={"for", "else"}, parens={"for", "in"})
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.visit_try_stmt = partial(
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | v, keywords={"try", "except", "else", "finally"}, parens=Ø
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | )
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.visit_except_clause = partial(v, keywords={"except"}, parens={"except"})
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.visit_with_stmt = partial(v, keywords={"with"}, parens={"with"})
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.visit_classdef = partial(v, keywords={"class"}, parens=Ø)
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 |
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | # When this is moved out of preview, add ":" directly to ASSIGNMENTS in nodes.py
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | if Preview.parenthesize_long_type_hints in self.mode:
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | assignments = ASSIGNMENTS | {":"}
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | else:
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | assignments = ASSIGNMENTS
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.visit_expr_stmt = partial(v, keywords=Ø, parens=assignments)
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 |
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.visit_return_stmt = partial(v, keywords={"return"}, parens={"return"})
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.visit_import_from = partial(v, keywords=Ø, parens={"import"})
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.visit_del_stmt = partial(v, keywords=Ø, parens={"del"})
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.visit_async_funcdef = self.visit_async_stmt
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.visit_decorated = self.visit_decorators
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 |
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | # PEP 634
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.visit_match_stmt = self.visit_match_case
0035 0026 0028 0002 0000 0004 0003 05 02 002 004 002 004 006 006 0015.51 0015.51 0001.00 | self.visit_case_block = self.visit_match_case
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0009 0005 0009 0000 0000 0000 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def _hugging_power_ops_line_to_string(
0009 0005 0009 0000 0000 0000 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | line: Line,
0009 0005 0009 0000 0000 0000 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | features: Collection[Feature],
0009 0005 0009 0000 0000 0000 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | mode: Mode,
0009 0005 0009 0000 0000 0000 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | ) -> Optional[str]:
0009 0005 0009 0000 0000 0000 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | try:
0009 0005 0009 0000 0000 0000 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return line_to_string(next(hug_power_op(line, features, mode)))
0009 0005 0009 0000 0000 0000 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | except CannotTransform:
0009 0005 0009 0000 0000 0000 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return None
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | def transform_line(
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | line: Line, mode: Mode, features: Collection[Feature] = ()
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | ) -> Iterator[Line]:
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | """Transform a `line`, potentially splitting it into many lines.
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 |
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | They should fit in the allotted `line_length` but might not be able to.
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 |
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | `features` are syntactical features that may be used in the output.
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | """
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | if line.is_comment:
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | yield line
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | return
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 |
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | line_str = line_to_string(line)
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 |
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # We need the line string when power operators are hugging to determine if we should
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # split the line. Default to line_str, if no power operator are present on the line.
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | line_str_hugging_power_ops = (
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | (_hugging_power_ops_line_to_string(line, features, mode) or line_str)
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | if Preview.fix_power_op_line_length in mode
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | else line_str
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | )
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 |
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | ll = mode.line_length
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | sn = mode.string_normalization
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | string_merge = StringMerger(ll, sn)
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | string_paren_strip = StringParenStripper(ll, sn)
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | string_split = StringSplitter(ll, sn)
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | string_paren_wrap = StringParenWrapper(ll, sn)
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 |
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | transformers: List[Transformer]
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | if (
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | not line.contains_uncollapsable_type_comments()
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | and not line.should_split_rhs
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | and not line.magic_trailing_comma
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | and (
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | is_line_short_enough(line, mode=mode, line_str=line_str_hugging_power_ops)
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | or line.contains_unsplittable_type_ignore()
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | )
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | and not (line.inside_brackets and line.contains_standalone_comments())
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | and not line.contains_implicit_multiline_string_with_comments()
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | ):
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # Only apply basic string preprocessing, since lines shouldn't be split here.
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | if Preview.string_processing in mode:
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | transformers = [string_merge, string_paren_strip]
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | else:
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | transformers = []
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | elif line.is_def and not should_split_funcdef_with_rhs(line, mode):
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | transformers = [left_hand_split]
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | else:
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 |
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | def _rhs(
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | self: object, line: Line, features: Collection[Feature], mode: Mode
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | ) -> Iterator[Line]:
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | """Wraps calls to `right_hand_split`.
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 |
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | The calls increasingly `omit` right-hand trailers (bracket pairs with
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | content), meaning the trailers get glued together to split on another
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | bracket pair instead.
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | """
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | for omit in generate_trailers_to_omit(line, mode.line_length):
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | lines = list(right_hand_split(line, mode, features, omit=omit))
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # Note: this check is only able to figure out if the first line of the
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # *current* transformation fits in the line length. This is true only
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # for simple cases. All others require running more transforms via
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # `transform_line()`. This check doesn't know if those would succeed.
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | if is_line_short_enough(lines[0], mode=mode):
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | yield from lines
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | return
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 |
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # All splits failed, best effort split with no omits.
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # This mostly happens to multiline strings that are by definition
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # reported as not fitting a single line, as well as lines that contain
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # trailing commas (those have to be exploded).
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | yield from right_hand_split(line, mode, features=features)
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 |
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # HACK: nested functions (like _rhs) compiled by mypyc don't retain their
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # __name__ attribute which is needed in `run_transformer` further down.
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # Unfortunately a nested class breaks mypyc too. So a class must be created
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # via type ... https://github.com/mypyc/mypyc/issues/884
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | rhs = type("rhs", (), {"__call__": _rhs})()
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 |
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | if Preview.string_processing in mode:
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | if line.inside_brackets:
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | transformers = [
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | string_merge,
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | string_paren_strip,
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | string_split,
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | delimiter_split,
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | standalone_comment_split,
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | string_paren_wrap,
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | rhs,
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | ]
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | else:
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | transformers = [
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | string_merge,
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | string_paren_strip,
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | string_split,
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | string_paren_wrap,
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | rhs,
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | ]
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | else:
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | if line.inside_brackets:
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | transformers = [delimiter_split, standalone_comment_split, rhs]
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | else:
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | transformers = [rhs]
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # It's always safe to attempt hugging of power operations and pretty much every line
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # could match.
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | transformers.append(hug_power_op)
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 |
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | for transform in transformers:
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # We are accumulating lines in `result` because we might want to abort
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # mission and return the original line in the end, or attempt a different
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | # split altogether.
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | try:
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | result = run_transformer(line, transform, mode, features, line_str=line_str)
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | except CannotTransform:
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | continue
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | else:
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | yield from result
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | break
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 |
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | else:
0124 0054 0082 0020 0009 0013 0020 -- 22 004 023 014 026 027 040 0190.20 0430.01 0002.26 | yield line
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | def should_split_funcdef_with_rhs(line: Line, mode: Mode) -> bool:
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | """If a funcdef has a magic trailing comma in the return type, then we should first
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | split the line with rhs to respect the comma.
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | """
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | if Preview.respect_magic_trailing_comma_in_return_type not in mode:
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | return False
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 |
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | return_type_leaves: List[Leaf] = []
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | in_return_type = False
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 |
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | for leaf in line.leaves:
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | if leaf.type == token.COLON:
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | in_return_type = False
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | if in_return_type:
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | return_type_leaves.append(leaf)
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | if leaf.type == token.RARROW:
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | in_return_type = True
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 |
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | # using `bracket_split_build_line` will mess with whitespace, so we duplicate a
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | # couple lines from it.
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | result = Line(mode=line.mode, depth=line.depth)
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | leaves_to_track = get_leaves_inside_matching_brackets(return_type_leaves)
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | for leaf in return_type_leaves:
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | result.append(
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | leaf,
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | preformatted=True,
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | track_bracket=id(leaf) in leaves_to_track,
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | )
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 |
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | # we could also return true if the line is too long, and the return type is longer
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | # than the param list. Or if `should_split_rhs` returns True.
0032 0019 0021 0004 0003 0004 0004 -- 07 004 009 005 010 013 015 0055.51 0123.35 0002.22 | return result.magic_trailing_comma is not None
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | class _BracketSplitComponent(Enum):
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | head = auto()
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | body = auto()
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | tail = auto()
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | def left_hand_split(
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | line: Line, _features: Collection[Feature], mode: Mode
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | ) -> Iterator[Line]:
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | """Split line into many lines, starting with the first matching bracket pair.
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 |
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | Note: this usually looks weird, only use this for function definitions.
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | Prefer RHS otherwise. This is why this function is not symmetrical with
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | :func:`right_hand_split` which also handles optional parentheses.
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | """
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | tail_leaves: List[Leaf] = []
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | body_leaves: List[Leaf] = []
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | head_leaves: List[Leaf] = []
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | current_leaves = head_leaves
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | matching_bracket: Optional[Leaf] = None
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | for leaf in line.leaves:
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | if (
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | current_leaves is body_leaves
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | and leaf.type in CLOSING_BRACKETS
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | and leaf.opening_bracket is matching_bracket
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | and isinstance(matching_bracket, Leaf)
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | ):
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | ensure_visible(leaf)
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | ensure_visible(matching_bracket)
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | current_leaves = tail_leaves if body_leaves else head_leaves
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | current_leaves.append(leaf)
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | if current_leaves is head_leaves:
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | if leaf.type in OPENING_BRACKETS:
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | matching_bracket = leaf
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | current_leaves = body_leaves
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | if not matching_bracket or not tail_leaves:
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | raise CannotSplit("No brackets found")
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 |
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | head = bracket_split_build_line(
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | head_leaves, line, matching_bracket, component=_BracketSplitComponent.head
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | )
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | body = bracket_split_build_line(
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | body_leaves, line, matching_bracket, component=_BracketSplitComponent.body
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | )
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | tail = bracket_split_build_line(
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | tail_leaves, line, matching_bracket, component=_BracketSplitComponent.tail
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | )
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | bracket_split_succeeded_or_raise(head, body, tail)
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | for result in (head, body, tail):
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | if result:
0045 0030 0038 0000 0005 0002 0000 -- 13 005 015 009 018 020 027 0116.69 0350.08 0003.00 | yield result
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0018 0004 0010 0000 0006 0002 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def right_hand_split(
0018 0004 0010 0000 0006 0002 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | line: Line,
0018 0004 0010 0000 0006 0002 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | mode: Mode,
0018 0004 0010 0000 0006 0002 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | features: Collection[Feature] = (),
0018 0004 0010 0000 0006 0002 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | omit: Collection[LeafID] = (),
0018 0004 0010 0000 0006 0002 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | ) -> Iterator[Line]:
0018 0004 0010 0000 0006 0002 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """Split line into many lines, starting with the last matching bracket pair.
0018 0004 0010 0000 0006 0002 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0018 0004 0010 0000 0006 0002 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | If the split was by optional parentheses, attempt splitting without them, too.
0018 0004 0010 0000 0006 0002 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | `omit` is a collection of closing bracket IDs that shouldn't be considered for
0018 0004 0010 0000 0006 0002 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | this split.
0018 0004 0010 0000 0006 0002 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0018 0004 0010 0000 0006 0002 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | Note: running this function modifies `bracket_depth` on the leaves of `line`.
0018 0004 0010 0000 0006 0002 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """
0018 0004 0010 0000 0006 0002 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | rhs_result = _first_right_hand_split(line, omit=omit)
0018 0004 0010 0000 0006 0002 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | yield from _maybe_split_omitting_optional_parens(
0018 0004 0010 0000 0006 0002 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | rhs_result, line, mode, features=features, omit=omit
0018 0004 0010 0000 0006 0002 0000 -- -- 000 000 000 000 000 000 0000.00 0000.00 0000.00 | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | def _first_right_hand_split(
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | line: Line,
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | omit: Collection[LeafID] = (),
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | ) -> RHSResult:
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | """Split the line into head, body, tail starting with the last bracket pair.
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 |
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | Note: this function should not have side effects. It's relied upon by
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | _maybe_split_omitting_optional_parens to get an opinion whether to prefer
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | splitting on the right side of an assignment statement.
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | """
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | tail_leaves: List[Leaf] = []
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | body_leaves: List[Leaf] = []
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | head_leaves: List[Leaf] = []
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | current_leaves = tail_leaves
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | opening_bracket: Optional[Leaf] = None
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | closing_bracket: Optional[Leaf] = None
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | for leaf in reversed(line.leaves):
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | if current_leaves is body_leaves:
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | if leaf is opening_bracket:
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | current_leaves = head_leaves if body_leaves else tail_leaves
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | current_leaves.append(leaf)
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | if current_leaves is tail_leaves:
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | if leaf.type in CLOSING_BRACKETS and id(leaf) not in omit:
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | opening_bracket = leaf.opening_bracket
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | closing_bracket = leaf
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | current_leaves = body_leaves
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | if not (opening_bracket and closing_bracket and head_leaves):
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | # If there is no opening or closing_bracket that means the split failed and
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | # all content is in the tail. Otherwise, if `head_leaves` are empty, it means
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | # the matching `opening_bracket` wasn't available on `line` anymore.
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | raise CannotSplit("No brackets found")
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 |
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | tail_leaves.reverse()
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | body_leaves.reverse()
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | head_leaves.reverse()
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 |
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | body: Optional[Line] = None
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | if (
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | Preview.hug_parens_with_braces_and_square_brackets in line.mode
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | and tail_leaves[0].value
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | and tail_leaves[0].opening_bracket is head_leaves[-1]
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | ):
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | inner_body_leaves = list(body_leaves)
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | hugged_opening_leaves: List[Leaf] = []
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | hugged_closing_leaves: List[Leaf] = []
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | is_unpacking = body_leaves[0].type in [token.STAR, token.DOUBLESTAR]
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | unpacking_offset: int = 1 if is_unpacking else 0
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | while (
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | len(inner_body_leaves) >= 2 + unpacking_offset
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | and inner_body_leaves[-1].type in CLOSING_BRACKETS
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | and inner_body_leaves[-1].opening_bracket
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | is inner_body_leaves[unpacking_offset]
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | ):
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | if unpacking_offset:
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | hugged_opening_leaves.append(inner_body_leaves.pop(0))
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | unpacking_offset = 0
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | hugged_opening_leaves.append(inner_body_leaves.pop(0))
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | hugged_closing_leaves.insert(0, inner_body_leaves.pop())
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 |
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | if hugged_opening_leaves and inner_body_leaves:
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | inner_body = bracket_split_build_line(
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | inner_body_leaves,
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | line,
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | hugged_opening_leaves[-1],
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | component=_BracketSplitComponent.body,
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | )
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | if (
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | line.mode.magic_trailing_comma
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | and inner_body_leaves[-1].type == token.COMMA
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | ):
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | should_hug = True
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | else:
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | line_length = line.mode.line_length - sum(
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | len(str(leaf))
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | for leaf in hugged_opening_leaves + hugged_closing_leaves
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | )
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | if is_line_short_enough(
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | inner_body, mode=replace(line.mode, line_length=line_length)
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | ):
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | # Do not hug if it fits on a single line.
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | should_hug = False
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | else:
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | should_hug = True
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | if should_hug:
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | body_leaves = inner_body_leaves
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | head_leaves.extend(hugged_opening_leaves)
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | tail_leaves = hugged_closing_leaves + tail_leaves
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | body = inner_body # No need to re-calculate the body again later.
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 |
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | head = bracket_split_build_line(
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | head_leaves, line, opening_bracket, component=_BracketSplitComponent.head
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | )
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | if body is None:
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | body = bracket_split_build_line(
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | body_leaves, line, opening_bracket, component=_BracketSplitComponent.body
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | )
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | tail = bracket_split_build_line(
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | tail_leaves, line, opening_bracket, component=_BracketSplitComponent.tail
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | )
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | bracket_split_succeeded_or_raise(head, body, tail)
0101 0066 0087 0005 0005 0005 0004 -- 27 010 040 029 055 050 084 0474.08 3259.33 0006.88 | return RHSResult(head, body, tail, opening_bracket, closing_bracket)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | def _maybe_split_omitting_optional_parens(
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | rhs: RHSResult,
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | line: Line,
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | mode: Mode,
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | features: Collection[Feature] = (),
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | omit: Collection[LeafID] = (),
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | ) -> Iterator[Line]:
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | if (
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | Feature.FORCE_OPTIONAL_PARENTHESES not in features
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | # the opening bracket is an optional paren
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | and rhs.opening_bracket.type == token.LPAR
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | and not rhs.opening_bracket.value
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | # the closing bracket is an optional paren
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | and rhs.closing_bracket.type == token.RPAR
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | and not rhs.closing_bracket.value
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | # it's not an import (optional parens are the only thing we can split on
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | # in this case; attempting a split without them is a waste of time)
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | and not line.is_import
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | # and we can actually remove the parens
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | and can_omit_invisible_parens(rhs, mode.line_length)
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | ):
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | omit = {id(rhs.closing_bracket), *omit}
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | try:
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | # The RHSResult Omitting Optional Parens.
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | rhs_oop = _first_right_hand_split(line, omit=omit)
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | prefer_splitting_rhs_mode = (
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | Preview.prefer_splitting_right_hand_side_of_assignments in line.mode
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | )
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | is_split_right_after_equal = (
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | len(rhs.head.leaves) >= 2 and rhs.head.leaves[-2].type == token.EQUAL
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | )
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | rhs_head_contains_brackets = any(
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | leaf.type in BRACKETS for leaf in rhs.head.leaves[:-1]
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | )
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | # the -1 is for the ending optional paren
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | rhs_head_short_enough = is_line_short_enough(
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | rhs.head, mode=replace(mode, line_length=mode.line_length - 1)
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | )
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | rhs_head_explode_blocked_by_magic_trailing_comma = (
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | rhs.head.magic_trailing_comma is None
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | )
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | if (
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | not (
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | prefer_splitting_rhs_mode
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | and is_split_right_after_equal
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | and rhs_head_contains_brackets
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | and rhs_head_short_enough
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | and rhs_head_explode_blocked_by_magic_trailing_comma
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | )
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | # the omit optional parens split is preferred by some other reason
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | or _prefer_split_rhs_oop_over_rhs(rhs_oop, rhs, mode)
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | ):
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | yield from _maybe_split_omitting_optional_parens(
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | rhs_oop, line, mode, features=features, omit=omit
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | )
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | return
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 |
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | except CannotSplit as e:
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | # For chained assignments we want to use the previous successful split
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | if line.is_chained_assignment:
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | pass
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 |
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | elif not can_be_split(rhs.body) and not is_line_short_enough(
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | rhs.body, mode=mode
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | ):
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | raise CannotSplit(
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | "Splitting failed, body is still too long and can't be split."
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | ) from e
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 |
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | elif (
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | rhs.head.contains_multiline_strings()
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | or rhs.tail.contains_multiline_strings()
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | ):
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | raise CannotSplit(
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | "The current optional pair of parentheses is bound to fail to"
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | " satisfy the splitting algorithm because the head or the tail"
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | " contains multiline strings which by definition never fit one"
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | " line."
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | ) from e
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 |
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | ensure_visible(rhs.opening_bracket)
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | ensure_visible(rhs.closing_bracket)
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | for result in (rhs.head, rhs.body, rhs.tail):
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | if result:
0085 0026 0078 0009 0000 0004 0003 -- 24 010 040 023 046 050 069 0389.43 2239.20 0005.75 | yield result
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | def _prefer_split_rhs_oop_over_rhs(
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | rhs_oop: RHSResult, rhs: RHSResult, mode: Mode
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | ) -> bool:
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | """
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | Returns whether we should prefer the result from a split omitting optional parens
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | (rhs_oop) over the original (rhs).
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | """
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | # If we have multiple targets, we prefer more `=`s on the head vs pushing them to
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | # the body
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | rhs_head_equal_count = [leaf.type for leaf in rhs.head.leaves].count(token.EQUAL)
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | rhs_oop_head_equal_count = [leaf.type for leaf in rhs_oop.head.leaves].count(
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | token.EQUAL
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | )
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | if rhs_head_equal_count > 1 and rhs_head_equal_count > rhs_oop_head_equal_count:
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | return False
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 |
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | has_closing_bracket_after_assign = False
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | for leaf in reversed(rhs_oop.head.leaves):
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | if leaf.type == token.EQUAL:
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | break
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | if leaf.type in CLOSING_BRACKETS:
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | has_closing_bracket_after_assign = True
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | break
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | return (
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | # contains matching brackets after the `=` (done by checking there is a
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | # closing bracket)
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | has_closing_bracket_after_assign
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | or (
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | # the split is actually from inside the optional parens (done by checking
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | # the first line still contains the `=`)
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | any(leaf.type == token.EQUAL for leaf in rhs_oop.head.leaves)
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | # the first line is short enough
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | and is_line_short_enough(rhs_oop.head, mode=mode)
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | )
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | # contains unsplittable type ignore
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | or rhs_oop.head.contains_unsplittable_type_ignore()
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | or rhs_oop.body.contains_unsplittable_type_ignore()
0039 0014 0032 0008 0004 0001 0002 -- 14 005 015 008 019 020 027 0116.69 0369.52 0003.17 | or rhs_oop.tail.contains_unsplittable_type_ignore()
0039 0014 0032 0008 0004 0001 0002 -- -- 005 015 008 019 020 027 0116.69 0369.52 0003.17 | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | def bracket_split_succeeded_or_raise(head: Line, body: Line, tail: Line) -> None:
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | """Raise :exc:`CannotSplit` if the last left- or right-hand split failed.
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 |
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | Do nothing otherwise.
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 |
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | A left- or right-hand split is based on a pair of brackets. Content before
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | (and including) the opening bracket is left on one line, content inside the
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | brackets is put on a separate line, and finally content starting with and
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | following the closing bracket is put on a separate line.
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 |
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | Those are called `head`, `body`, and `tail`, respectively. If the split
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | produced the same line (all content in `head`) or ended up with an empty `body`
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | and the `tail` is just the closing bracket, then it's considered failed.
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | """
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | tail_len = len(str(tail).strip())
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | if not body:
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | if tail_len == 0:
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | raise CannotSplit("Splitting brackets produced the same line")
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 |
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | elif tail_len < 3:
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | raise CannotSplit(
0024 0008 0010 0000 0010 0004 0000 -- 04 003 004 003 005 007 008 0022.46 0042.11 0001.88 | f"Splitting brackets on an empty body to save {tail_len} characters is"
0024 0008 0010 0000 0010 0004 0000 -- -- 003 004 003 005 007 008 0022.46 0042.11 0001.88 | " not worth it"
0024 0008 0010 0000 0010 0004 0000 -- -- 003 004 003 005 007 008 0022.46 0042.11 0001.88 | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | def bracket_split_build_line(
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | leaves: List[Leaf],
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | original: Line,
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | opening_bracket: Leaf,
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | *,
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | component: _BracketSplitComponent,
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | ) -> Line:
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | """Return a new line with given `leaves` and respective comments from `original`.
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 |
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | If it's the head component, brackets will be tracked so trailing commas are
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | respected.
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 |
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | If it's the body component, the result line is one-indented inside brackets and as
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | such has its first leaf's prefix normalized and a trailing comma added when
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | expected.
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | """
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | result = Line(mode=original.mode, depth=original.depth)
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | if component is _BracketSplitComponent.body:
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | result.inside_brackets = True
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | result.depth += 1
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | if leaves:
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | # Ensure a trailing comma for imports and standalone function arguments, but
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | # be careful not to add one after any comments or within type annotations.
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | no_commas = (
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | original.is_def
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | and opening_bracket.value == "("
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | and not any(leaf.type == token.COMMA for leaf in leaves)
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | # In particular, don't add one within a parenthesized return annotation.
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | # Unfortunately the indicator we're in a return annotation (RARROW) may
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | # be defined directly in the parent node, the parent of the parent ...
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | # and so on depending on how complex the return annotation is.
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | # This isn't perfect and there's some false negatives but they are in
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | # contexts were a comma is actually fine.
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | and not any(
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | node.prev_sibling.type == RARROW
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | for node in (
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | leaves[0].parent,
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | getattr(leaves[0].parent, "parent", None),
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | )
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | if isinstance(node, Node) and isinstance(node.prev_sibling, Leaf)
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | )
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | # Except the false negatives above for PEP 604 unions where we
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | # can't add the comma.
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | and not (
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | leaves[0].parent
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | and leaves[0].parent.next_sibling
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | and leaves[0].parent.next_sibling.type == token.VBAR
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | )
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | )
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 |
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | if original.is_import or no_commas:
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | for i in range(len(leaves) - 1, -1, -1):
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | if leaves[i].type == STANDALONE_COMMENT:
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | continue
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 |
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | if leaves[i].type != token.COMMA:
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | new_comma = Leaf(token.COMMA, ",")
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | leaves.insert(i + 1, new_comma)
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | break
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 |
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | leaves_to_track: Set[LeafID] = set()
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | if component is _BracketSplitComponent.head:
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | leaves_to_track = get_leaves_inside_matching_brackets(leaves)
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | # Populate the line
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | for leaf in leaves:
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | result.append(
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | leaf,
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | preformatted=True,
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | track_bracket=id(leaf) in leaves_to_track,
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | )
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | for comment_after in original.comments_after(leaf):
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | result.append(comment_after, preformatted=True)
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | if component is _BracketSplitComponent.body and should_split_line(
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | result, opening_bracket
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | ):
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | result.should_split_rhs = True
0077 0027 0062 0011 0007 0005 0003 -- 23 010 033 023 045 043 068 0368.99 2515.81 0006.82 | return result
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0015 0008 0009 0000 0003 0003 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def dont_increase_indentation(split_func: Transformer) -> Transformer:
0015 0008 0009 0000 0003 0003 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """Normalize prefix of the first leaf in every line returned by `split_func`.
0015 0008 0009 0000 0003 0003 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0015 0008 0009 0000 0003 0003 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | This is a decorator over relevant split functions.
0015 0008 0009 0000 0003 0003 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """
0015 0008 0009 0000 0003 0003 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0015 0008 0009 0000 0003 0003 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | @wraps(split_func)
0015 0008 0009 0000 0003 0003 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def split_wrapper(
0015 0008 0009 0000 0003 0003 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | line: Line, features: Collection[Feature], mode: Mode
0015 0008 0009 0000 0003 0003 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | ) -> Iterator[Line]:
0015 0008 0009 0000 0003 0003 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | for split_line in split_func(line, features, mode):
0015 0008 0009 0000 0003 0003 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | split_line.leaves[0].prefix = ""
0015 0008 0009 0000 0003 0003 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | yield split_line
0015 0008 0009 0000 0003 0003 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0015 0008 0009 0000 0003 0003 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return split_wrapper
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0005 0005 0005 0000 0000 0000 0000 -- 03 003 004 003 005 007 008 0022.46 0042.11 0001.88 | def _get_last_non_comment_leaf(line: Line) -> Optional[int]:
0005 0005 0005 0000 0000 0000 0000 -- 03 003 004 003 005 007 008 0022.46 0042.11 0001.88 | for leaf_idx in range(len(line.leaves) - 1, 0, -1):
0005 0005 0005 0000 0000 0000 0000 -- 03 003 004 003 005 007 008 0022.46 0042.11 0001.88 | if line.leaves[leaf_idx].type != STANDALONE_COMMENT:
0005 0005 0005 0000 0000 0000 0000 -- 03 003 004 003 005 007 008 0022.46 0042.11 0001.88 | return leaf_idx
0005 0005 0005 0000 0000 0000 0000 -- 03 003 004 003 005 007 008 0022.46 0042.11 0001.88 | return None
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0010 0005 0010 0000 0000 0000 0000 -- 05 004 010 006 012 014 018 0068.53 0164.48 0002.40 | def _safe_add_trailing_comma(safe: bool, delimiter_priority: int, line: Line) -> Line:
0010 0005 0010 0000 0000 0000 0000 -- 05 004 010 006 012 014 018 0068.53 0164.48 0002.40 | if (
0010 0005 0010 0000 0000 0000 0000 -- 05 004 010 006 012 014 018 0068.53 0164.48 0002.40 | safe
0010 0005 0010 0000 0000 0000 0000 -- 05 004 010 006 012 014 018 0068.53 0164.48 0002.40 | and delimiter_priority == COMMA_PRIORITY
0010 0005 0010 0000 0000 0000 0000 -- 05 004 010 006 012 014 018 0068.53 0164.48 0002.40 | and line.leaves[-1].type != token.COMMA
0010 0005 0010 0000 0000 0000 0000 -- 05 004 010 006 012 014 018 0068.53 0164.48 0002.40 | and line.leaves[-1].type != STANDALONE_COMMENT
0010 0005 0010 0000 0000 0000 0000 -- 05 004 010 006 012 014 018 0068.53 0164.48 0002.40 | ):
0010 0005 0010 0000 0000 0000 0000 -- 05 004 010 006 012 014 018 0068.53 0164.48 0002.40 | new_comma = Leaf(token.COMMA, ",")
0010 0005 0010 0000 0000 0000 0000 -- 05 004 010 006 012 014 018 0068.53 0164.48 0002.40 | line.append(new_comma)
0010 0005 0010 0000 0000 0000 0000 -- 05 004 010 006 012 014 018 0068.53 0164.48 0002.40 | return line
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @dont_increase_indentation
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | def delimiter_split(
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | line: Line, features: Collection[Feature], mode: Mode
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | ) -> Iterator[Line]:
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | """Split according to delimiters of the highest priority.
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 |
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | If the appropriate Features are given, the split will add trailing commas
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | also in function signatures and calls that contain `*` and `**`.
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | """
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | try:
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | last_leaf = line.leaves[-1]
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | except IndexError:
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | raise CannotSplit("Line empty") from None
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 |
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | bt = line.bracket_tracker
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | try:
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | delimiter_priority = bt.max_delimiter_priority(exclude={id(last_leaf)})
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | except ValueError:
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | raise CannotSplit("No delimiters found") from None
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 |
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | if delimiter_priority == DOT_PRIORITY:
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | if bt.delimiter_count_with_priority(delimiter_priority) == 1:
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | raise CannotSplit("Splitting a single attribute from its owner looks wrong")
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 |
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | current_line = Line(
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | )
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | lowest_depth = sys.maxsize
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | trailing_comma_safe = True
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 |
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | def append_to_line(leaf: Leaf) -> Iterator[Line]:
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | """Append `leaf` to current line or to new line if appending impossible."""
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | nonlocal current_line
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | try:
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | current_line.append_safe(leaf, preformatted=True)
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | except ValueError:
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | yield current_line
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 |
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | current_line = Line(
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | )
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | current_line.append(leaf)
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 |
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | last_non_comment_leaf = _get_last_non_comment_leaf(line)
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | for leaf_idx, leaf in enumerate(line.leaves):
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | yield from append_to_line(leaf)
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 |
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | for comment_after in line.comments_after(leaf):
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | yield from append_to_line(comment_after)
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 |
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | lowest_depth = min(lowest_depth, leaf.bracket_depth)
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | if leaf.bracket_depth == lowest_depth:
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | if is_vararg(leaf, within={syms.typedargslist}):
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | trailing_comma_safe = (
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | trailing_comma_safe and Feature.TRAILING_COMMA_IN_DEF in features
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | )
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | elif is_vararg(leaf, within={syms.arglist, syms.argument}):
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | trailing_comma_safe = (
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | trailing_comma_safe and Feature.TRAILING_COMMA_IN_CALL in features
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | )
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 |
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | if (
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | Preview.add_trailing_comma_consistently in mode
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | and last_leaf.type == STANDALONE_COMMENT
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | and leaf_idx == last_non_comment_leaf
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | ):
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | current_line = _safe_add_trailing_comma(
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | trailing_comma_safe, delimiter_priority, current_line
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | )
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 |
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | leaf_priority = bt.delimiters.get(id(leaf))
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | if leaf_priority == delimiter_priority:
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | yield current_line
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 |
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | current_line = Line(
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | )
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | if current_line:
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | current_line = _safe_add_trailing_comma(
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | trailing_comma_safe, delimiter_priority, current_line
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | )
0082 0047 0065 0000 0004 0012 0001 -- 17 004 022 013 026 026 039 0183.32 0433.30 0002.36 | yield current_line
0082 0047 0065 0000 0004 0012 0001 -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @dont_increase_indentation
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | def standalone_comment_split(
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | line: Line, features: Collection[Feature], mode: Mode
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | ) -> Iterator[Line]:
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | """Split standalone comments from the rest of the line."""
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | if not line.contains_standalone_comments():
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | raise CannotSplit("Line does not have any standalone comments")
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 |
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | current_line = Line(
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | )
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 |
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | def append_to_line(leaf: Leaf) -> Iterator[Line]:
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | """Append `leaf` to current line or to new line if appending impossible."""
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | nonlocal current_line
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | try:
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | current_line.append_safe(leaf, preformatted=True)
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | except ValueError:
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | yield current_line
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 |
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | current_line = Line(
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | line.mode, depth=line.depth, inside_brackets=line.inside_brackets
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | )
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | current_line.append(leaf)
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 |
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | for leaf in line.leaves:
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | yield from append_to_line(leaf)
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 |
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | for comment_after in line.comments_after(leaf):
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | yield from append_to_line(comment_after)
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 |
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | if current_line:
0033 0021 0025 0000 0000 0006 0002 -- 05 001 001 001 001 002 002 0002.00 0001.00 0000.50 | yield current_line
0033 0021 0025 0000 0000 0006 0002 -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | def normalize_invisible_parens( # noqa: C901
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | node: Node, parens_after: Set[str], *, mode: Mode, features: Collection[Feature]
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | ) -> None:
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | """Make existing optional parentheses invisible or create new ones.
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 |
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | `parens_after` is a set of string leaf values immediately after which parens
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | should be put.
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 |
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | Standardizes on visible parentheses for single-element tuples, and keeps
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | existing visible parentheses for other tuples and generator expressions.
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | """
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | for pc in list_comments(node.prefix, is_endmarker=False):
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | if pc.value in FMT_OFF:
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | # This `node` has a prefix with `# fmt: off`, don't mess with parens.
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | return
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 |
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | # The multiple context managers grammar has a different pattern, thus this is
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | # separate from the for-loop below. This possibly wraps them in invisible parens,
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | # and later will be removed in remove_with_parens when needed.
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | if node.type == syms.with_stmt:
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | _maybe_wrap_cms_in_parens(node, mode, features)
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 |
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | check_lpar = False
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | for index, child in enumerate(list(node.children)):
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | # Fixes a bug where invisible parens are not properly stripped from
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | # assignment statements that contain type annotations.
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | if isinstance(child, Node) and child.type == syms.annassign:
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | normalize_invisible_parens(
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | child, parens_after=parens_after, mode=mode, features=features
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | )
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 |
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | # Fixes a bug where invisible parens are not properly wrapped around
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | # case blocks.
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | if (
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | isinstance(child, Node)
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | and child.type == syms.case_block
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | and Preview.long_case_block_line_splitting in mode
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | ):
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | normalize_invisible_parens(
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | child, parens_after={"case"}, mode=mode, features=features
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | )
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 |
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | # Add parentheses around long tuple unpacking in assignments.
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | if (
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | index == 0
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | and isinstance(child, Node)
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | and child.type == syms.testlist_star_expr
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | ):
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | check_lpar = True
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 |
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | if check_lpar:
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | if (
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | child.type == syms.atom
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | and node.type == syms.for_stmt
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | and isinstance(child.prev_sibling, Leaf)
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | and child.prev_sibling.type == token.NAME
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | and child.prev_sibling.value == "for"
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | ):
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | if maybe_make_parens_invisible_in_atom(
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | child,
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | parent=node,
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | remove_brackets_around_comma=True,
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | ):
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | wrap_in_parentheses(node, child, visible=False)
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | elif isinstance(child, Node) and node.type == syms.with_stmt:
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | remove_with_parens(child, node)
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | elif child.type == syms.atom:
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | if maybe_make_parens_invisible_in_atom(
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | child,
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | parent=node,
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | ):
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | wrap_in_parentheses(node, child, visible=False)
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | elif is_one_tuple(child):
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | wrap_in_parentheses(node, child, visible=True)
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | elif node.type == syms.import_from:
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | _normalize_import_from(node, child, index)
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | break
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | elif (
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | index == 1
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | and child.type == token.STAR
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | and node.type == syms.except_clause
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | ):
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | # In except* (PEP 654), the star is actually part of
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | # of the keyword. So we need to skip the insertion of
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | # invisible parentheses to work more precisely.
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | continue
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 |
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | elif (
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | isinstance(child, Leaf)
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | and child.next_sibling is not None
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | and child.next_sibling.type == token.COLON
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | and child.value == "case"
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | and Preview.long_case_block_line_splitting in mode
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | ):
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | # A special patch for "case case:" scenario, the second occurrence
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | # of case will be not parsed as a Python keyword.
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | break
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 |
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | elif not (isinstance(child, Leaf) and is_multiline_string(child)):
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | wrap_in_parentheses(node, child, visible=False)
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 |
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | comma_check = child.type == token.COMMA
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 |
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | check_lpar = isinstance(child, Leaf) and (
0106 0037 0075 0015 0006 0011 0014 -- 38 006 055 034 076 061 110 0652.38 2704.42 0004.15 | child.value in parens_after or comma_check
0106 0037 0075 0015 0006 0011 0014 -- -- 006 055 034 076 061 110 0652.38 2704.42 0004.15 | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0012 0008 0008 0004 0000 0000 0004 -- 03 002 003 003 004 005 007 0016.25 0021.67 0001.33 | def _normalize_import_from(parent: Node, child: LN, index: int) -> None:
0012 0008 0008 0004 0000 0000 0004 -- 03 002 003 003 004 005 007 0016.25 0021.67 0001.33 | # "import from" nodes store parentheses directly as part of
0012 0008 0008 0004 0000 0000 0004 -- 03 002 003 003 004 005 007 0016.25 0021.67 0001.33 | # the statement
0012 0008 0008 0004 0000 0000 0004 -- 03 002 003 003 004 005 007 0016.25 0021.67 0001.33 | if is_lpar_token(child):
0012 0008 0008 0004 0000 0000 0004 -- 03 002 003 003 004 005 007 0016.25 0021.67 0001.33 | assert is_rpar_token(parent.children[-1])
0012 0008 0008 0004 0000 0000 0004 -- 03 002 003 003 004 005 007 0016.25 0021.67 0001.33 | # make parentheses invisible
0012 0008 0008 0004 0000 0000 0004 -- 03 002 003 003 004 005 007 0016.25 0021.67 0001.33 | child.value = ""
0012 0008 0008 0004 0000 0000 0004 -- 03 002 003 003 004 005 007 0016.25 0021.67 0001.33 | parent.children[-1].value = ""
0012 0008 0008 0004 0000 0000 0004 -- 03 002 003 003 004 005 007 0016.25 0021.67 0001.33 | elif child.type != token.STAR:
0012 0008 0008 0004 0000 0000 0004 -- 03 002 003 003 004 005 007 0016.25 0021.67 0001.33 | # insert invisible parentheses
0012 0008 0008 0004 0000 0000 0004 -- 03 002 003 003 004 005 007 0016.25 0021.67 0001.33 | parent.insert_child(index, Leaf(token.LPAR, ""))
0012 0008 0008 0004 0000 0000 0004 -- 03 002 003 003 004 005 007 0016.25 0021.67 0001.33 | parent.append_child(Leaf(token.RPAR, ""))
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | def remove_await_parens(node: Node) -> None:
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | if node.children[0].type == token.AWAIT and len(node.children) > 1:
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | if (
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | node.children[1].type == syms.atom
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | and node.children[1].children[0].type == token.LPAR
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | ):
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | if maybe_make_parens_invisible_in_atom(
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | node.children[1],
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | parent=node,
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | remove_brackets_around_comma=True,
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | ):
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | wrap_in_parentheses(node, node.children[1], visible=False)
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 |
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | # Since await is an expression we shouldn't remove
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | # brackets in cases where this would change
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | # the AST due to operator precedence.
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | # Therefore we only aim to remove brackets around
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | # power nodes that aren't also await expressions themselves.
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | # https://peps.python.org/pep-0492/#updated-operator-precedence-table
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | # N.B. We've still removed any redundant nested brackets though :)
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | opening_bracket = cast(Leaf, node.children[1].children[0])
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | closing_bracket = cast(Leaf, node.children[1].children[-1])
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | bracket_contents = node.children[1].children[1]
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | if isinstance(bracket_contents, Node) and (
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | bracket_contents.type != syms.power
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | or bracket_contents.children[0].type == token.AWAIT
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | or any(
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | isinstance(child, Leaf) and child.type == token.DOUBLESTAR
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | for child in bracket_contents.children
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | )
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | ):
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | ensure_visible(opening_bracket)
0033 0011 0025 0007 0000 0001 0007 -- 12 006 019 013 026 025 039 0181.11 0743.51 0004.11 | ensure_visible(closing_bracket)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | def _maybe_wrap_cms_in_parens(
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | node: Node, mode: Mode, features: Collection[Feature]
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | ) -> None:
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | """When enabled and safe, wrap the multiple context managers in invisible parens.
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 |
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | It is only safe when `features` contain Feature.PARENTHESIZED_CONTEXT_MANAGERS.
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | """
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | if (
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | Feature.PARENTHESIZED_CONTEXT_MANAGERS not in features
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | or Preview.wrap_multiple_context_managers_in_parens not in mode
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | or len(node.children) <= 2
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | # If it's an atom, it's already wrapped in parens.
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | or node.children[1].type == syms.atom
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | ):
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | return
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | colon_index: Optional[int] = None
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | for i in range(2, len(node.children)):
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | if node.children[i].type == token.COLON:
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | colon_index = i
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | break
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | if colon_index is not None:
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | lpar = Leaf(token.LPAR, "")
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | rpar = Leaf(token.RPAR, "")
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | context_managers = node.children[1:colon_index]
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | for child in context_managers:
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | child.remove()
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | # After wrapping, the with_stmt will look like this:
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | # with_stmt
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | # NAME 'with'
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | # atom
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | # LPAR ''
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | # testlist_gexp
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | # ... <-- context_managers
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | # /testlist_gexp
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | # RPAR ''
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | # /atom
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | # COLON ':'
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | new_child = Node(
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | syms.atom, [lpar, Node(syms.testlist_gexp, context_managers), rpar]
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | )
0041 0019 0026 0012 0003 0001 0011 -- 09 005 015 007 016 020 023 0099.40 0265.08 0002.67 | node.insert_child(1, new_child)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | def remove_with_parens(node: Node, parent: Node) -> None:
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | """Recursively hide optional parens in `with` statements."""
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | # Removing all unnecessary parentheses in with statements in one pass is a tad
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | # complex as different variations of bracketed statements result in pretty
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | # different parse trees:
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | #
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | # with (open("file")) as f: # this is an asexpr_test
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | # ...
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | #
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | # with (open("file") as f): # this is an atom containing an
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | # ... # asexpr_test
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | #
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | # with (open("file")) as f, (open("file")) as f: # this is asexpr_test, COMMA,
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | # ... # asexpr_test
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | #
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | # with (open("file") as f, open("file") as f): # an atom containing a
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | # ... # testlist_gexp which then
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | # # contains multiple asexpr_test(s)
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | if node.type == syms.atom:
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | if maybe_make_parens_invisible_in_atom(
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | node,
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | parent=parent,
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | remove_brackets_around_comma=True,
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | ):
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | wrap_in_parentheses(parent, node, visible=False)
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | if isinstance(node.children[1], Node):
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | remove_with_parens(node.children[1], node)
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | elif node.type == syms.testlist_gexp:
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | for child in node.children:
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | if isinstance(child, Node):
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | remove_with_parens(child, node)
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | elif node.type == syms.asexpr_test and not any(
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | leaf.type == token.COLONEQUAL for leaf in node.leaves()
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | ):
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | if maybe_make_parens_invisible_in_atom(
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | node.children[0],
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | parent=node,
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | remove_brackets_around_comma=True,
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | ):
0040 0014 0023 0016 0000 0000 0017 -- 11 003 008 006 011 011 017 0058.81 0121.30 0002.06 | wrap_in_parentheses(node, node.children[0], visible=False)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | def maybe_make_parens_invisible_in_atom(
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | node: LN,
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | parent: LN,
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | remove_brackets_around_comma: bool = False,
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | ) -> bool:
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | """If it's safe, make the parens in the atom `node` invisible, recursively.
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | Additionally, remove repeated, adjacent invisible parens from the atom `node`
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | as they are redundant.
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 |
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | Returns whether the node should itself be wrapped in invisible parentheses.
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | """
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | if (
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | node.type not in (syms.atom, syms.expr)
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | or is_empty_tuple(node)
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | or is_one_tuple(node)
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | or (is_yield(node) and parent.type != syms.expr_stmt)
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | or (
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | # This condition tries to prevent removing non-optional brackets
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | # around a tuple, however, can be a bit overzealous so we provide
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | # and option to skip this check for `for` and `with` statements.
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | not remove_brackets_around_comma
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | and max_delimiter_priority_in_atom(node) >= COMMA_PRIORITY
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | )
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | or is_tuple_containing_walrus(node)
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | ):
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | return False
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 |
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | if is_walrus_assignment(node):
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | if parent.type in [
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | syms.annassign,
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | syms.expr_stmt,
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | syms.assert_stmt,
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | syms.return_stmt,
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | syms.except_clause,
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | syms.funcdef,
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | syms.with_stmt,
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | syms.tname,
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | # these ones aren't useful to end users, but they do please fuzzers
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | syms.for_stmt,
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | syms.del_stmt,
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | syms.for_stmt,
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | ]:
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | return False
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 |
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | first = node.children[0]
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | last = node.children[-1]
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | if is_lpar_token(first) and is_rpar_token(last):
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | middle = node.children[1]
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | # make parentheses invisible
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | if (
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | # If the prefix of `middle` includes a type comment with
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | # ignore annotation, then we do not remove the parentheses
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | not is_type_ignore_comment_string(middle.prefix.strip())
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | ):
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | first.value = ""
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | last.value = ""
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | maybe_make_parens_invisible_in_atom(
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | middle,
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | parent=parent,
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | remove_brackets_around_comma=remove_brackets_around_comma,
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | )
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 |
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | if is_atom_with_invisible_parens(middle):
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | # Strip the invisible parens from `middle` by replacing
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | # it with the child in-between the invisible parens
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | middle.replace(middle.children[1])
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 |
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | return False
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 |
0070 0019 0056 0009 0005 0006 0003 -- 15 008 021 011 023 029 034 0165.17 0723.61 0004.38 | return True
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | def should_split_line(line: Line, opening_bracket: Leaf) -> bool:
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | """Should `line` be immediately split with `delimiter_split()` after RHS?"""
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 |
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | if not (opening_bracket.parent and opening_bracket.value in "[{("):
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | return False
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 |
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | # We're essentially checking if the body is delimited by commas and there's more
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | # than one of them (we're excluding the trailing comma and if the delimiter priority
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | # is still commas, that means there's more).
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | exclude = set()
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | trailing_comma = False
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | try:
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | last_leaf = line.leaves[-1]
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | if last_leaf.type == token.COMMA:
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | trailing_comma = True
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | exclude.add(id(last_leaf))
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | max_priority = line.bracket_tracker.max_delimiter_priority(exclude=exclude)
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | except (IndexError, ValueError):
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | return False
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 |
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | return max_priority == COMMA_PRIORITY and (
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | (line.mode.magic_trailing_comma and trailing_comma)
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | # always explode imports
0025 0015 0018 0004 0000 0003 0004 -- 08 006 017 010 018 023 028 0126.66 0402.33 0003.18 | or opening_bracket.parent.type in {syms.atom, syms.import_from}
0025 0015 0018 0004 0000 0003 0004 -- -- 006 017 010 018 023 028 0126.66 0402.33 0003.18 | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[LeafID]]:
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | """Generate sets of closing bracket IDs that should be omitted in a RHS.
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 |
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | Brackets can be omitted if the entire trailer up to and including
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | a preceding closing bracket fits in one line.
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 |
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | Yielded sets are cumulative (contain results of previous yields, too). First
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | set is empty, unless the line should explode, in which case bracket pairs until
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | the one that needs to explode are omitted.
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | """
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 |
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | omit: Set[LeafID] = set()
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | if not line.magic_trailing_comma:
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | yield omit
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 |
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | length = 4 * line.depth
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | opening_bracket: Optional[Leaf] = None
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | closing_bracket: Optional[Leaf] = None
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | inner_brackets: Set[LeafID] = set()
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | for index, leaf, leaf_length in line.enumerate_with_length(is_reversed=True):
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | length += leaf_length
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | if length > line_length:
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | break
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 |
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | has_inline_comment = leaf_length > len(leaf.value) + len(leaf.prefix)
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | if leaf.type == STANDALONE_COMMENT or has_inline_comment:
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | break
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 |
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | if opening_bracket:
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | if leaf is opening_bracket:
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | opening_bracket = None
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | elif leaf.type in CLOSING_BRACKETS:
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | prev = line.leaves[index - 1] if index > 0 else None
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | if (
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | prev
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | and prev.type == token.COMMA
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | and leaf.opening_bracket is not None
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | and not is_one_sequence_between(
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | leaf.opening_bracket, leaf, line.leaves
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | )
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | ):
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | # Never omit bracket pairs with trailing commas.
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | # We need to explode on those.
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | break
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 |
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | inner_brackets.add(id(leaf))
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | elif leaf.type in CLOSING_BRACKETS:
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | prev = line.leaves[index - 1] if index > 0 else None
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | if prev and prev.type in OPENING_BRACKETS:
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | # Empty brackets would fail a split so treat them as "inner"
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | # brackets (e.g. only add them to the `omit` set if another
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | # pair of brackets was good enough.
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | inner_brackets.add(id(leaf))
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | continue
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 |
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | if closing_bracket:
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | omit.add(id(closing_bracket))
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | omit.update(inner_brackets)
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | inner_brackets.clear()
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | yield omit
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 |
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | if (
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | prev
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | and prev.type == token.COMMA
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | and leaf.opening_bracket is not None
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | and not is_one_sequence_between(leaf.opening_bracket, leaf, line.leaves)
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | ):
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | # Never omit bracket pairs with trailing commas.
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | # We need to explode on those.
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | break
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 |
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | if leaf.value:
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | opening_bracket = leaf.opening_bracket
0074 0043 0050 0007 0007 0010 0007 -- 24 011 032 025 051 043 076 0412.40 3614.91 0008.77 | closing_bracket = leaf
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | def run_transformer(
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | line: Line,
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | transform: Transformer,
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | mode: Mode,
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | features: Collection[Feature],
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | *,
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | line_str: str = "",
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | ) -> List[Line]:
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | if not line_str:
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | line_str = line_to_string(line)
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | result: List[Line] = []
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | for transformed_line in transform(line, features, mode):
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | if str(transformed_line).strip("\n") == line_str:
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | raise CannotTransform("Line transformer returned an unchanged result")
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 |
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | result.extend(transform_line(transformed_line, mode=mode, features=features))
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 |
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | features_set = set(features)
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | if (
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | Feature.FORCE_OPTIONAL_PARENTHESES in features_set
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | or transform.__class__.__name__ != "rhs"
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | or not line.bracket_tracker.invisible
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | or any(bracket.value for bracket in line.bracket_tracker.invisible)
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | or line.contains_multiline_strings()
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | or result[0].contains_uncollapsable_type_comments()
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | or result[0].contains_unsplittable_type_ignore()
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | or is_line_short_enough(result[0], mode=mode)
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | # If any leaves have no parents (which _can_ occur since
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | # `transform(line)` potentially destroys the line's underlying node
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | # structure), then we can't proceed. Doing so would cause the below
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | # call to `append_leaves()` to fail.
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | or any(leaf.parent is None for leaf in line.leaves)
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | ):
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | return result
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 |
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | line_copy = line.clone()
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | append_leaves(line_copy, line, line.leaves)
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | features_fop = features_set | {Feature.FORCE_OPTIONAL_PARENTHESES}
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | second_opinion = run_transformer(
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | line_copy, transform, mode, features_fop, line_str=line_str
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | )
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | if all(is_line_short_enough(ln, mode=mode) for ln in second_opinion):
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | result = second_opinion
0044 0019 0041 0004 0000 0003 0000 -- 17 007 019 008 021 026 029 0136.31 0527.32 0003.87 | return result