src/black/__init__.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
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import io
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import json
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import platform
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import re
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import sys
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import tokenize
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import traceback
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from contextlib import contextmanager
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from dataclasses import replace
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from datetime import datetime, timezone
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from enum import Enum
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from json.decoder import JSONDecodeError
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from pathlib import Path
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from typing import (
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Any,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Collection,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Dict,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Generator,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Iterator,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | List,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | MutableMapping,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Optional,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Pattern,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Sequence,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Set,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Sized,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Tuple,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Union,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import click
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from click.core import ParameterSource
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from mypy_extensions import mypyc_attr
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from pathspec import PathSpec
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from pathspec.patterns.gitwildmatch import GitWildMatchPatternError
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from _black_version import version as __version__
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.cache import Cache
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.comments import normalize_fmt_off
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.const import (
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | DEFAULT_EXCLUDES,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | DEFAULT_INCLUDES,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | DEFAULT_LINE_LENGTH,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | STDIN_PLACEHOLDER,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.files import (
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | find_project_root,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | find_pyproject_toml,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | find_user_pyproject_toml,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | gen_python_files,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | get_gitignore,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | normalize_path_maybe_ignore,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | parse_pyproject_toml,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | path_is_excluded,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | wrap_stream_for_windows,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.handle_ipynb_magics import (
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | PYTHON_CELL_MAGICS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | TRANSFORMED_MAGICS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | jupyter_dependencies_are_installed,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | mask_cell,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | put_trailing_semicolon_back,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | remove_trailing_semicolon,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | unmask_cell,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.linegen import LN, LineGenerator, transform_line
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.lines import EmptyLineTracker, LinesBlock
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.mode import FUTURE_FLAG_TO_FEATURE, VERSION_TO_FEATURES, Feature
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.mode import Mode as Mode # re-exported
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.mode import TargetVersion, supports_feature
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.nodes import (
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | STARS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_number_token,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_simple_decorator_expression,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_string_token,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.output import color_diff, diff, dump_to_file, err, ipynb_diff, out
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.parsing import InvalidInput # noqa F401
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.parsing import lib2to3_parse, parse_ast, stringify_ast
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.ranges import adjusted_lines, convert_unchanged_lines, parse_line_ranges
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.report import Changed, NothingChanged, Report
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.trans import iter_fexpr_spans
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from blib2to3.pgen2 import token
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from blib2to3.pytree import Leaf, Node
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | COMPILED = Path(__file__).suffix in (".pyd", ".so")
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # types
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | FileContent = str
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Encoding = str
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | NewLine = str
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 07 -- --- --- --- --- --- --- ------- ------- ------- | class WriteBack(Enum):
---- ---- ---- ---- ---- ---- ---- 07 -- --- --- --- --- --- --- ------- ------- ------- | NO = 0
---- ---- ---- ---- ---- ---- ---- 07 -- --- --- --- --- --- --- ------- ------- ------- | YES = 1
---- ---- ---- ---- ---- ---- ---- 07 -- --- --- --- --- --- --- ------- ------- ------- | DIFF = 2
---- ---- ---- ---- ---- ---- ---- 07 -- --- --- --- --- --- --- ------- ------- ------- | CHECK = 3
---- ---- ---- ---- ---- ---- ---- 07 -- --- --- --- --- --- --- ------- ------- ------- | COLOR_DIFF = 4
---- ---- ---- ---- ---- ---- ---- 07 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 07 -- --- --- --- --- --- --- ------- ------- ------- | @classmethod
0011 0007 0009 0000 0000 0002 0000 07 06 002 004 003 005 006 008 0020.68 0025.85 0001.25 | def from_configuration(
0011 0007 0009 0000 0000 0002 0000 07 06 002 004 003 005 006 008 0020.68 0025.85 0001.25 | cls, *, check: bool, diff: bool, color: bool = False
0011 0007 0009 0000 0000 0002 0000 07 06 002 004 003 005 006 008 0020.68 0025.85 0001.25 | ) -> "WriteBack":
0011 0007 0009 0000 0000 0002 0000 07 06 002 004 003 005 006 008 0020.68 0025.85 0001.25 | if check and not diff:
0011 0007 0009 0000 0000 0002 0000 07 06 002 004 003 005 006 008 0020.68 0025.85 0001.25 | return cls.CHECK
0011 0007 0009 0000 0000 0002 0000 07 06 002 004 003 005 006 008 0020.68 0025.85 0001.25 |
0011 0007 0009 0000 0000 0002 0000 07 06 002 004 003 005 006 008 0020.68 0025.85 0001.25 | if diff and color:
0011 0007 0009 0000 0000 0002 0000 07 06 002 004 003 005 006 008 0020.68 0025.85 0001.25 | return cls.COLOR_DIFF
0011 0007 0009 0000 0000 0002 0000 07 06 002 004 003 005 006 008 0020.68 0025.85 0001.25 |
0011 0007 0009 0000 0000 0002 0000 07 06 002 004 003 005 006 008 0020.68 0025.85 0001.25 | return cls.DIFF if diff else cls.YES
0011 0007 0009 0000 0000 0002 0000 -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # Legacy name, left for integrations.
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | FileMode = Mode
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | def read_pyproject_toml(
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | ctx: click.Context, param: click.Parameter, value: Optional[str]
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | ) -> Optional[str]:
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | """Inject Black configuration from "pyproject.toml" into defaults in `ctx`.
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 |
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | Returns the path to a successfully found and read configuration file, None
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | otherwise.
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | """
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | if not value:
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | value = find_pyproject_toml(
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | ctx.params.get("src", ()), ctx.params.get("stdin_filename", None)
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | )
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | if value is None:
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | return None
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 |
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | try:
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | config = parse_pyproject_toml(value)
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | except (OSError, ValueError) as e:
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | raise click.FileError(
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | filename=value, hint=f"Error reading configuration file: {e}"
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | ) from None
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 |
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | if not config:
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | return None
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | else:
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | # Sanitize the values to be Click friendly. For more information please see:
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | # https://github.com/psf/black/issues/1458
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | # https://github.com/pallets/click/issues/1567
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | config = {
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | k: str(v) if not isinstance(v, (list, dict)) else v
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | for k, v in config.items()
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | }
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 |
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | target_version = config.get("target_version")
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | if target_version is not None and not isinstance(target_version, list):
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | raise click.BadOptionUsage(
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | "target-version", "Config key target-version must be a list"
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | )
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 |
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | exclude = config.get("exclude")
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | if exclude is not None and not isinstance(exclude, str):
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | raise click.BadOptionUsage("exclude", "Config key exclude must be a string")
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 |
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | extend_exclude = config.get("extend_exclude")
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | if extend_exclude is not None and not isinstance(extend_exclude, str):
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | raise click.BadOptionUsage(
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | "extend-exclude", "Config key extend-exclude must be a string"
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | )
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 |
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | line_ranges = config.get("line_ranges")
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | if line_ranges is not None:
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | raise click.BadOptionUsage(
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | "line-ranges", "Cannot use line-ranges in the pyproject.toml file."
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | )
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 |
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | default_map: Dict[str, Any] = {}
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | if ctx.default_map:
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | default_map.update(ctx.default_map)
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | default_map.update(config)
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 |
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | ctx.default_map = default_map
0062 0034 0046 0003 0004 0009 0003 -- 15 004 017 014 022 021 036 0158.12 0409.26 0002.59 | return value
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0009 0003 0004 0000 0004 0001 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def target_version_option_callback(
0009 0003 0004 0000 0004 0001 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | c: click.Context, p: Union[click.Option, click.Parameter], v: Tuple[str, ...]
0009 0003 0004 0000 0004 0001 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | ) -> List[TargetVersion]:
0009 0003 0004 0000 0004 0001 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """Compute the target versions from a --target-version flag.
0009 0003 0004 0000 0004 0001 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0009 0003 0004 0000 0004 0001 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | This is its own function because mypy couldn't infer the type correctly
0009 0003 0004 0000 0004 0001 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | when it was a lambda, causing mypyc trouble.
0009 0003 0004 0000 0004 0001 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """
0009 0003 0004 0000 0004 0001 0000 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return [TargetVersion[val.upper()] for val in v]
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0009 0007 0005 0000 0003 0001 0000 -- 02 002 003 002 004 005 006 0013.93 0018.58 0001.33 | def re_compile_maybe_verbose(regex: str) -> Pattern[str]:
0009 0007 0005 0000 0003 0001 0000 -- 02 002 003 002 004 005 006 0013.93 0018.58 0001.33 | """Compile a regular expression string in `regex`.
0009 0007 0005 0000 0003 0001 0000 -- 02 002 003 002 004 005 006 0013.93 0018.58 0001.33 |
0009 0007 0005 0000 0003 0001 0000 -- 02 002 003 002 004 005 006 0013.93 0018.58 0001.33 | If it contains newlines, use verbose mode.
0009 0007 0005 0000 0003 0001 0000 -- 02 002 003 002 004 005 006 0013.93 0018.58 0001.33 | """
0009 0007 0005 0000 0003 0001 0000 -- 02 002 003 002 004 005 006 0013.93 0018.58 0001.33 | if "\n" in regex:
0009 0007 0005 0000 0003 0001 0000 -- 02 002 003 002 004 005 006 0013.93 0018.58 0001.33 | regex = "(?x)" + regex
0009 0007 0005 0000 0003 0001 0000 -- 02 002 003 002 004 005 006 0013.93 0018.58 0001.33 | compiled: Pattern[str] = re.compile(regex)
0009 0007 0005 0000 0003 0001 0000 -- 02 002 003 002 004 005 006 0013.93 0018.58 0001.33 | return compiled
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0009 0005 0009 0000 0000 0000 0000 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def validate_regex(
0009 0005 0009 0000 0000 0000 0000 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ctx: click.Context,
0009 0005 0009 0000 0000 0000 0000 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | param: click.Parameter,
0009 0005 0009 0000 0000 0000 0000 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | value: Optional[str],
0009 0005 0009 0000 0000 0000 0000 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ) -> Optional[Pattern[str]]:
0009 0005 0009 0000 0000 0000 0000 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | try:
0009 0005 0009 0000 0000 0000 0000 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return re_compile_maybe_verbose(value) if value is not None else None
0009 0005 0009 0000 0000 0000 0000 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | except re.error as e:
0009 0005 0009 0000 0000 0000 0000 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | raise click.BadParameter(f"Not a valid regular expression: {e}") from None
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.command(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | context_settings={"help_option_names": ["-h", "--help"]},
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # While Click does set this field automatically using the docstring, mypyc
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # (annoyingly) strips 'em so we need to set it here too.
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help="The uncompromising code formatter.",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option("-c", "--code", type=str, help="Format the code passed in as a string.")
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "-l",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--line-length",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | type=int,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | default=DEFAULT_LINE_LENGTH,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help="How many characters per line to allow.",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | show_default=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "-t",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--target-version",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | type=click.Choice([v.name.lower() for v in TargetVersion]),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | callback=target_version_option_callback,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | multiple=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "Python versions that should be supported by Black's output. You should"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " include all versions that your code supports. By default, Black will infer"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " target versions from the project metadata in pyproject.toml. If this does"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " not yield conclusive results, Black will use per-file auto-detection."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--pyi",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_flag=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "Format all input files like typing stubs regardless of file extension. This"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " is useful when piping source on standard input."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--ipynb",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_flag=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "Format all input files like Jupyter Notebooks regardless of file extension."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "This is useful when piping source on standard input."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--python-cell-magics",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | multiple=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "When processing Jupyter Notebooks, add the given magic to the list"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | f" of known python-magics ({', '.join(sorted(PYTHON_CELL_MAGICS))})."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " Useful for formatting cells with custom python magics."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | default=[],
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "-x",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--skip-source-first-line",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_flag=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help="Skip the first line of the source code.",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "-S",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--skip-string-normalization",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_flag=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help="Don't normalize string quotes or prefixes.",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "-C",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--skip-magic-trailing-comma",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_flag=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help="Don't use trailing commas as a reason to split lines.",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--experimental-string-processing",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_flag=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | hidden=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help="(DEPRECATED and now included in --preview) Normalize string literals.",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--preview",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_flag=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "Enable potentially disruptive style changes that may be added to Black's main"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " functionality in the next major release."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--check",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_flag=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "Don't write the files back, just return the status. Return code 0 means"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " nothing would change. Return code 1 means some files would be reformatted."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " Return code 123 means there was an internal error."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--diff",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_flag=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "Don't write the files back, just output a diff to indicate what changes"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " Black would've made. They are printed to stdout so capturing them is simple."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--color/--no-color",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_flag=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help="Show (or do not show) colored diff. Only applies when --diff is given.",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--line-ranges",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | multiple=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | metavar="START-END",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "When specified, Black will try its best to only format these lines. This"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " option can be specified multiple times, and a union of the lines will be"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " formatted. Each range must be specified as two integers connected by a `-`:"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " `<START>-<END>`. The `<START>` and `<END>` integer indices are 1-based and"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " inclusive on both ends."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | default=(),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--fast/--safe",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_flag=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "By default, Black performs an AST safety check after formatting your code."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " The --fast flag turns off this check and the --safe flag explicitly enables"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " it. [default: --safe]"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--required-version",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | type=str,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "Require a specific version of Black to be running. This is useful for"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " ensuring that all contributors to your project are using the same"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " version, because different versions of Black may format code a little"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " differently. This option can be set in a configuration file for consistent"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " results across environments."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--exclude",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | type=str,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | callback=validate_regex,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "A regular expression that matches files and directories that should be"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " excluded on recursive searches. An empty value means no paths are excluded."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " Use forward slashes for directories on all platforms (Windows, too)."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " By default, Black also ignores all paths listed in .gitignore. Changing this"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | f" value will override all default exclusions. [default: {DEFAULT_EXCLUDES}]"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | show_default=False,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--extend-exclude",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | type=str,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | callback=validate_regex,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "Like --exclude, but adds additional files and directories on top of the"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " default values instead of overriding them."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--force-exclude",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | type=str,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | callback=validate_regex,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "Like --exclude, but files and directories matching this regex will be excluded"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " even when they are passed explicitly as arguments. This is useful when"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " invoking Black programmatically on changed files, such as in a pre-commit"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " hook or editor plugin."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--stdin-filename",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | type=str,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_eager=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "The name of the file when passing it through stdin. Useful to make sure Black"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " will respect the --force-exclude option on some editors that rely on using"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " stdin."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--include",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | type=str,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | default=DEFAULT_INCLUDES,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | callback=validate_regex,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "A regular expression that matches files and directories that should be"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " included on recursive searches. An empty value means all files are included"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " regardless of the name. Use forward slashes for directories on all platforms"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " (Windows, too). Overrides all exclusions, including from .gitignore and"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " command line options."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | show_default=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "-W",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--workers",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | type=click.IntRange(min=1),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | default=None,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "When Black formats multiple files, it may use a process pool to speed up"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " formatting. This option controls the number of parallel workers. This can"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " also be specified via the BLACK_NUM_WORKERS environment variable. Defaults"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " to the number of CPUs in the system."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "-q",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--quiet",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_flag=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "Stop emitting all non-critical output. Error messages will still be emitted"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " (which can silenced by 2>/dev/null)."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "-v",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--verbose",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_flag=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "Emit messages about files that were not changed or were ignored due to"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " exclusion patterns. If Black is using a configuration file, a message"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | " detailing which one it is using will be emitted."
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.version_option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | version=__version__,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | message=(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | f"%(prog)s, %(version)s (compiled: {'yes' if COMPILED else 'no'})\n"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | f"Python ({platform.python_implementation()}) {platform.python_version()}"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.argument(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "src",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | nargs=-1,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | type=click.Path(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | exists=True, file_okay=True, dir_okay=True, readable=True, allow_dash=True
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_eager=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | metavar="SRC ...",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--config",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | type=click.Path(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | exists=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | file_okay=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | dir_okay=False,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | readable=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | allow_dash=False,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | path_type=str,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ),
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_eager=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | callback=read_pyproject_toml,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help="Read configuration options from a configuration file.",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.pass_context
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | def main( # noqa: C901
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ctx: click.Context,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | code: Optional[str],
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | line_length: int,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | target_version: List[TargetVersion],
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | check: bool,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | diff: bool,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | line_ranges: Sequence[str],
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | color: bool,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | fast: bool,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | pyi: bool,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ipynb: bool,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | python_cell_magics: Sequence[str],
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | skip_source_first_line: bool,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | skip_string_normalization: bool,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | skip_magic_trailing_comma: bool,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | experimental_string_processing: bool,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | preview: bool,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | quiet: bool,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | verbose: bool,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | required_version: Optional[str],
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | include: Pattern[str],
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | exclude: Optional[Pattern[str]],
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | extend_exclude: Optional[Pattern[str]],
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | force_exclude: Optional[Pattern[str]],
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | stdin_filename: Optional[str],
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | workers: Optional[int],
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | src: Tuple[str, ...],
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | config: Optional[str],
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ) -> None:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | """The uncompromising code formatter."""
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ctx.ensure_object(dict)
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 |
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if src and code is not None:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | out(
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | main.get_usage(ctx)
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | + "\n\n'SRC' and 'code' cannot be passed simultaneously."
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | )
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ctx.exit(1)
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if not src and code is None:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | out(main.get_usage(ctx) + "\n\nOne of 'SRC' or 'code' is required.")
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ctx.exit(1)
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 |
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | root, method = (
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | find_project_root(src, stdin_filename) if code is None else (None, None)
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | )
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ctx.obj["root"] = root
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 |
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if verbose:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if root:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | out(
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | f"Identified `{root}` as project root containing a {method}.",
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | fg="blue",
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | )
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 |
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if config:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | config_source = ctx.get_parameter_source("config")
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | user_level_config = str(find_user_pyproject_toml())
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if config == user_level_config:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | out(
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | "Using configuration from user-level config at "
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | f"'{user_level_config}'.",
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | fg="blue",
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | )
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | elif config_source in (
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ParameterSource.DEFAULT,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ParameterSource.DEFAULT_MAP,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ):
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | out("Using configuration from project root.", fg="blue")
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | else:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | out(f"Using configuration in '{config}'.", fg="blue")
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if ctx.default_map:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | for param, value in ctx.default_map.items():
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | out(f"{param}: {value}")
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 |
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | error_msg = "Oh no! 💥 💔 💥"
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if (
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | required_version
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | and required_version != __version__
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | and required_version != __version__.split(".")[0]
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ):
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | err(
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | f"{error_msg} The required version `{required_version}` does not match"
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | f" the running version `{__version__}`!"
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | )
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ctx.exit(1)
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if ipynb and pyi:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | err("Cannot pass both `pyi` and `ipynb` flags!")
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ctx.exit(1)
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 |
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | write_back = WriteBack.from_configuration(check=check, diff=diff, color=color)
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if target_version:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | versions = set(target_version)
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | else:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | # We'll autodetect later.
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | versions = set()
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | mode = Mode(
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | target_versions=versions,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | line_length=line_length,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | is_pyi=pyi,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | is_ipynb=ipynb,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | skip_source_first_line=skip_source_first_line,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | string_normalization=not skip_string_normalization,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | magic_trailing_comma=not skip_magic_trailing_comma,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | experimental_string_processing=experimental_string_processing,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | preview=preview,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | python_cell_magics=set(python_cell_magics),
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | )
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 |
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | lines: List[Tuple[int, int]] = []
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if line_ranges:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if ipynb:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | err("Cannot use --line-ranges with ipynb files.")
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ctx.exit(1)
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 |
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | try:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | lines = parse_line_ranges(line_ranges)
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | except ValueError as e:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | err(str(e))
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ctx.exit(1)
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 |
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if code is not None:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | # Run in quiet mode by default with -c; the extra output isn't useful.
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | # You can still pass -v to get verbose output.
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | quiet = True
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 |
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | report = Report(check=check, diff=diff, quiet=quiet, verbose=verbose)
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 |
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if code is not None:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | reformat_code(
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | content=code,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | fast=fast,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | write_back=write_back,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | mode=mode,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | report=report,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | lines=lines,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | )
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | else:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | assert root is not None # root is only None if code is not None
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | try:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | sources = get_sources(
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | root=root,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | src=src,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | quiet=quiet,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | verbose=verbose,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | include=include,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | exclude=exclude,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | extend_exclude=extend_exclude,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | force_exclude=force_exclude,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | report=report,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | stdin_filename=stdin_filename,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | )
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | except GitWildMatchPatternError:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ctx.exit(1)
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 |
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | path_empty(
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | sources,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | "No Python files are present to be formatted. Nothing to do 😴",
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | quiet,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | verbose,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ctx,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | )
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 |
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if len(sources) == 1:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | reformat_one(
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | src=sources.pop(),
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | fast=fast,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | write_back=write_back,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | mode=mode,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | report=report,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | lines=lines,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | )
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | else:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | from black.concurrency import reformat_many
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 |
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if lines:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | err("Cannot use --line-ranges to format multiple files.")
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ctx.exit(1)
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | reformat_many(
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | sources=sources,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | fast=fast,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | write_back=write_back,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | mode=mode,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | report=report,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | workers=workers,
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | )
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 |
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if verbose or not quiet:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if code is None and (verbose or report.change_count or report.failure_count):
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | out()
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | out(error_msg if report.return_code else "All done! ✨ 🍰 ✨")
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | if code is None:
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | click.echo(str(report), err=True)
0454 0108 0435 0007 0000 0015 0004 -- 35 009 033 026 050 042 076 0409.82 2794.20 0006.82 | ctx.exit(report.return_code)
0454 0108 0435 0007 0000 0015 0004 -- -- --- --- --- --- --- --- ------- ------- ------- |
0454 0108 0435 0007 0000 0015 0004 -- -- --- --- --- --- --- --- ------- ------- ------- |
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | def get_sources(
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | *,
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | root: Path,
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | src: Tuple[str, ...],
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | quiet: bool,
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | verbose: bool,
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | include: Pattern[str],
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | exclude: Optional[Pattern[str]],
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | extend_exclude: Optional[Pattern[str]],
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | force_exclude: Optional[Pattern[str]],
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | report: "Report",
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | stdin_filename: Optional[str],
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | ) -> Set[Path]:
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | """Compute the set of files to be formatted."""
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | sources: Set[Path] = set()
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 |
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | using_default_exclude = exclude is None
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES) if exclude is None else exclude
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | gitignore: Optional[Dict[Path, PathSpec]] = None
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | root_gitignore = get_gitignore(root)
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 |
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | for s in src:
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | if s == "-" and stdin_filename:
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | path = Path(stdin_filename)
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | is_stdin = True
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | else:
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | path = Path(s)
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | is_stdin = False
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 |
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | # Compare the logic here to the logic in `gen_python_files`.
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | if is_stdin or path.is_file():
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | root_relative_path = path.absolute().relative_to(root).as_posix()
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 |
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | root_relative_path = "/" + root_relative_path
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 |
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | # Hard-exclude any files that matches the `--force-exclude` regex.
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | if path_is_excluded(root_relative_path, force_exclude):
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | report.path_ignored(
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | path, "matches the --force-exclude regular expression"
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | )
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | continue
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 |
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | normalized_path: Optional[str] = normalize_path_maybe_ignore(
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | path, root, report
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | )
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | if normalized_path is None:
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | if verbose:
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | out(f'Skipping invalid source: "{normalized_path}"', fg="red")
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | continue
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 |
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | if is_stdin:
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | path = Path(f"{STDIN_PLACEHOLDER}{str(path)}")
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 |
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | if path.suffix == ".ipynb" and not jupyter_dependencies_are_installed(
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | warn=verbose or not quiet
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | ):
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | continue
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 |
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | if verbose:
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | out(f'Found input source: "{normalized_path}"', fg="blue")
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | sources.add(path)
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | elif path.is_dir():
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | path = root / (path.resolve().relative_to(root))
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | if verbose:
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | out(f'Found input source directory: "{path}"', fg="blue")
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 |
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | if using_default_exclude:
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | gitignore = {
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | root: root_gitignore,
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | path: get_gitignore(path),
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | }
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | sources.update(
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | gen_python_files(
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | path.iterdir(),
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | root,
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | include,
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | exclude,
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | extend_exclude,
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | force_exclude,
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | report,
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | gitignore,
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | verbose=verbose,
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | quiet=quiet,
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | )
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | )
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | elif s == "-":
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | if verbose:
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | out("Found input source stdin", fg="blue")
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | sources.add(path)
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | else:
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | err(f"invalid path: {s}")
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 |
0093 0050 0079 0002 0000 0011 0003 -- 20 007 021 014 026 028 040 0192.29 0833.27 0004.33 | return sources
0454 0108 0435 0007 0000 0015 0004 -- -- --- --- --- --- --- --- ------- ------- ------- |
0454 0108 0435 0007 0000 0015 0004 -- -- --- --- --- --- --- --- ------- ------- ------- |
0010 0006 0007 0000 0003 0000 0000 -- 04 002 004 003 004 006 007 0018.09 0018.09 0001.00 | def path_empty(
0010 0006 0007 0000 0003 0000 0000 -- 04 002 004 003 004 006 007 0018.09 0018.09 0001.00 | src: Sized, msg: str, quiet: bool, verbose: bool, ctx: click.Context
0010 0006 0007 0000 0003 0000 0000 -- 04 002 004 003 004 006 007 0018.09 0018.09 0001.00 | ) -> None:
0010 0006 0007 0000 0003 0000 0000 -- 04 002 004 003 004 006 007 0018.09 0018.09 0001.00 | """
0010 0006 0007 0000 0003 0000 0000 -- 04 002 004 003 004 006 007 0018.09 0018.09 0001.00 | Exit if there is no `src` provided for formatting
0010 0006 0007 0000 0003 0000 0000 -- 04 002 004 003 004 006 007 0018.09 0018.09 0001.00 | """
0010 0006 0007 0000 0003 0000 0000 -- 04 002 004 003 004 006 007 0018.09 0018.09 0001.00 | if not src:
0010 0006 0007 0000 0003 0000 0000 -- 04 002 004 003 004 006 007 0018.09 0018.09 0001.00 | if verbose or not quiet:
0010 0006 0007 0000 0003 0000 0000 -- 04 002 004 003 004 006 007 0018.09 0018.09 0001.00 | out(msg)
0010 0006 0007 0000 0003 0000 0000 -- 04 002 004 003 004 006 007 0018.09 0018.09 0001.00 | ctx.exit(0)
0454 0108 0435 0007 0000 0015 0004 -- -- --- --- --- --- --- --- ------- ------- ------- |
0454 0108 0435 0007 0000 0015 0004 -- -- --- --- --- --- --- --- ------- ------- ------- |
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def reformat_code(
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | content: str,
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | fast: bool,
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | write_back: WriteBack,
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | mode: Mode,
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | report: Report,
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | *,
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | lines: Collection[Tuple[int, int]] = (),
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | ) -> None:
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | Reformat and print out `content` without spawning child processes.
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | Similar to `reformat_one`, but for string content.
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | `fast`, `write_back`, and `mode` options are passed to
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | :func:`format_file_in_place` or :func:`format_stdin_to_stdout`.
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | path = Path("<string>")
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | try:
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | changed = Changed.NO
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | if format_stdin_to_stdout(
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | content=content, fast=fast, write_back=write_back, mode=mode, lines=lines
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | ):
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | changed = Changed.YES
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | report.done(path, changed)
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | except Exception as exc:
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | if report.verbose:
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | traceback.print_exc()
0032 0012 0021 0002 0006 0003 0002 -- 04 000 000 000 000 000 000 0000.00 0000.00 0000.00 | report.failed(path, str(exc))
0032 0012 0021 0002 0006 0003 0002 -- -- --- --- --- --- --- --- ------- ------- ------- |
0032 0012 0021 0002 0006 0003 0002 -- -- --- --- --- --- --- --- ------- ------- ------- |
0032 0012 0021 0002 0006 0003 0002 -- -- --- --- --- --- --- --- ------- ------- ------- | # diff-shades depends on being to monkeypatch this function to operate. I know it's
0032 0012 0021 0002 0006 0003 0002 -- -- --- --- --- --- --- --- ------- ------- ------- | # not ideal, but this shouldn't cause any issues ... hopefully. ~ichard26
0454 0108 0435 0007 0000 0015 0004 -- -- --- --- --- --- --- --- ------- ------- ------- | @mypyc_attr(patchable=True)
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | def reformat_one(
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | src: Path,
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | fast: bool,
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | write_back: WriteBack,
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | mode: Mode,
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | report: "Report",
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | *,
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | lines: Collection[Tuple[int, int]] = (),
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | ) -> None:
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | """Reformat a single file under `src` without spawning child processes.
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 |
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | `fast`, `write_back`, and `mode` options are passed to
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | :func:`format_file_in_place` or :func:`format_stdin_to_stdout`.
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | """
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | try:
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | changed = Changed.NO
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 |
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | if str(src) == "-":
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | is_stdin = True
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | elif str(src).startswith(STDIN_PLACEHOLDER):
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | is_stdin = True
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | # Use the original name again in case we want to print something
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | # to the user
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | src = Path(str(src)[len(STDIN_PLACEHOLDER) :])
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | else:
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | is_stdin = False
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 |
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | if is_stdin:
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | if src.suffix == ".pyi":
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | mode = replace(mode, is_pyi=True)
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | elif src.suffix == ".ipynb":
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | mode = replace(mode, is_ipynb=True)
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | if format_stdin_to_stdout(
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | fast=fast, write_back=write_back, mode=mode, lines=lines
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | ):
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | changed = Changed.YES
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | else:
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | cache = Cache.read(mode)
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | if write_back not in (WriteBack.DIFF, WriteBack.COLOR_DIFF):
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | if not cache.is_changed(src):
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | changed = Changed.CACHED
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | if changed is not Changed.CACHED and format_file_in_place(
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | src, fast=fast, write_back=write_back, mode=mode, lines=lines
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | ):
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | changed = Changed.YES
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | if (write_back is WriteBack.YES and changed is not Changed.CACHED) or (
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | write_back is WriteBack.CHECK and changed is Changed.NO
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | ):
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | cache.write([src])
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | report.done(src, changed)
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | except Exception as exc:
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | if report.verbose:
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | traceback.print_exc()
0055 0034 0046 0002 0004 0003 0002 -- 17 007 021 014 027 028 041 0197.10 0886.96 0004.50 | report.failed(src, str(exc))
0055 0034 0046 0002 0004 0003 0002 -- -- --- --- --- --- --- --- ------- ------- ------- |
0454 0108 0435 0007 0000 0015 0004 -- -- --- --- --- --- --- --- ------- ------- ------- |
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | def format_file_in_place(
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | src: Path,
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | fast: bool,
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | mode: Mode,
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | write_back: WriteBack = WriteBack.NO,
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | lock: Any = None, # multiprocessing.Manager().Lock() is some crazy proxy
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | *,
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | lines: Collection[Tuple[int, int]] = (),
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | ) -> bool:
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | """Format file under `src` path. Return True if changed.
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 |
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | If `write_back` is DIFF, write a diff to stdout. If it is YES, write reformatted
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | code to the file.
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | `mode` and `fast` options are passed to :func:`format_file_contents`.
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | """
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | if src.suffix == ".pyi":
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | mode = replace(mode, is_pyi=True)
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | elif src.suffix == ".ipynb":
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | mode = replace(mode, is_ipynb=True)
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 |
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | then = datetime.fromtimestamp(src.stat().st_mtime, timezone.utc)
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | header = b""
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | with open(src, "rb") as buf:
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | if mode.skip_source_first_line:
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | header = buf.readline()
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | src_contents, encoding, newline = decode_bytes(buf.read())
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | try:
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | dst_contents = format_file_contents(
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | src_contents, fast=fast, mode=mode, lines=lines
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | )
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | except NothingChanged:
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | return False
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | except JSONDecodeError:
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | raise ValueError(
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | f"File '{src}' cannot be parsed as valid Jupyter notebook."
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | ) from None
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | src_contents = header.decode(encoding) + src_contents
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | dst_contents = header.decode(encoding) + dst_contents
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 |
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | if write_back == WriteBack.YES:
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | with open(src, "w", encoding=encoding, newline=newline) as f:
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | f.write(dst_contents)
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | elif write_back in (WriteBack.DIFF, WriteBack.COLOR_DIFF):
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | now = datetime.now(timezone.utc)
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | src_name = f"{src}\t{then}"
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | dst_name = f"{src}\t{now}"
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | if mode.is_ipynb:
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | diff_contents = ipynb_diff(src_contents, dst_contents, src_name, dst_name)
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | else:
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | diff_contents = diff(src_contents, dst_contents, src_name, dst_name)
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 |
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | if write_back == WriteBack.COLOR_DIFF:
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | diff_contents = color_diff(diff_contents)
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 |
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | with lock or nullcontext():
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | f = io.TextIOWrapper(
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | sys.stdout.buffer,
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | encoding=encoding,
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | newline=newline,
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | write_through=True,
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | )
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | f = wrap_stream_for_windows(f)
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | f.write(diff_contents)
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | f.detach()
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 |
0066 0039 0055 0001 0005 0006 0000 -- 11 004 013 008 016 017 024 0098.10 0241.47 0002.46 | return True
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | def format_stdin_to_stdout(
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | fast: bool,
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | *,
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | content: Optional[str] = None,
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | write_back: WriteBack = WriteBack.NO,
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | mode: Mode,
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | lines: Collection[Tuple[int, int]] = (),
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | ) -> bool:
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | """Format file on stdin. Return True if changed.
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 |
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | If content is None, it's read from sys.stdin.
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 |
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | If `write_back` is YES, write reformatted code back to stdout. If it is DIFF,
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | write a diff to stdout. The `mode` argument is passed to
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | :func:`format_file_contents`.
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | """
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | then = datetime.now(timezone.utc)
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 |
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | if content is None:
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | src, encoding, newline = decode_bytes(sys.stdin.buffer.read())
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | else:
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | src, encoding, newline = content, "utf-8", ""
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 |
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | dst = src
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | try:
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | dst = format_file_contents(src, fast=fast, mode=mode, lines=lines)
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | return True
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 |
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | except NothingChanged:
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | return False
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 |
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | finally:
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | f = io.TextIOWrapper(
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | sys.stdout.buffer, encoding=encoding, newline=newline, write_through=True
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | )
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | if write_back == WriteBack.YES:
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | # Make sure there's a newline after the content
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | if dst and dst[-1] != "\n":
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | dst += "\n"
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | f.write(dst)
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | elif write_back in (WriteBack.DIFF, WriteBack.COLOR_DIFF):
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | now = datetime.now(timezone.utc)
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | src_name = f"STDIN\t{then}"
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | dst_name = f"STDOUT\t{now}"
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | d = diff(src, dst, src_name, dst_name)
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | if write_back == WriteBack.COLOR_DIFF:
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | d = color_diff(d)
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | f = wrap_stream_for_windows(f)
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | f.write(d)
0050 0029 0037 0001 0006 0006 0001 -- 08 007 011 008 015 018 023 0095.91 0457.74 0004.77 | f.detach()
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0015 0004 0009 0000 0005 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def check_stability_and_equivalence(
0015 0004 0009 0000 0005 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | src_contents: str,
0015 0004 0009 0000 0005 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | dst_contents: str,
0015 0004 0009 0000 0005 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | *,
0015 0004 0009 0000 0005 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | mode: Mode,
0015 0004 0009 0000 0005 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | lines: Collection[Tuple[int, int]] = (),
0015 0004 0009 0000 0005 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | ) -> None:
0015 0004 0009 0000 0005 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """Perform stability and equivalence checks.
0015 0004 0009 0000 0005 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0015 0004 0009 0000 0005 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | Raise AssertionError if source and destination contents are not
0015 0004 0009 0000 0005 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | equivalent, or if a second pass of the formatter would format the
0015 0004 0009 0000 0005 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | content differently.
0015 0004 0009 0000 0005 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """
0015 0004 0009 0000 0005 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | assert_equivalent(src_contents, dst_contents)
0015 0004 0009 0000 0005 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | assert_stable(src_contents, dst_contents, mode=mode, lines=lines)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | def format_file_contents(
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | src_contents: str,
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | *,
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | fast: bool,
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | mode: Mode,
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | lines: Collection[Tuple[int, int]] = (),
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | ) -> FileContent:
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | """Reformat contents of a file and return new contents.
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 |
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | If `fast` is False, additionally confirm that the reformatted code is
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | valid by calling :func:`assert_equivalent` and :func:`assert_stable` on it.
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | `mode` is passed to :func:`format_str`.
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | """
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | if mode.is_ipynb:
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | dst_contents = format_ipynb_string(src_contents, fast=fast, mode=mode)
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | else:
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | dst_contents = format_str(src_contents, mode=mode, lines=lines)
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | if src_contents == dst_contents:
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | raise NothingChanged
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 |
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | if not fast and not mode.is_ipynb:
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | # Jupyter notebooks will already have been checked above.
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | check_stability_and_equivalence(
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | src_contents, dst_contents, mode=mode, lines=lines
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | )
0026 0011 0018 0001 0005 0002 0001 -- 05 003 006 004 006 009 010 0031.70 0047.55 0001.50 | return dst_contents
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | def validate_cell(src: str, mode: Mode) -> None:
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | """Check that cell does not already contain TransformerManager transformations,
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | or non-Python cell magics, which might cause tokenizer_rt to break because of
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | indentations.
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 |
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | If a cell contains ``!ls``, then it'll be transformed to
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | ``get_ipython().system('ls')``. However, if the cell originally contained
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | ``get_ipython().system('ls')``, then it would get transformed in the same way:
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 |
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | >>> TransformerManager().transform_cell("get_ipython().system('ls')")
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | "get_ipython().system('ls')\n"
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | >>> TransformerManager().transform_cell("!ls")
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | "get_ipython().system('ls')\n"
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 |
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | Due to the impossibility of safely roundtripping in such situations, cells
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | containing transformed magics will be ignored.
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | """
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | if any(transformed_magic in src for transformed_magic in TRANSFORMED_MAGICS):
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | raise NothingChanged
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | if (
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | src[:2] == "%%"
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | and src.split()[0][2:] not in PYTHON_CELL_MAGICS | mode.python_cell_magics
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | ):
0024 0006 0008 0000 0013 0003 0000 -- 05 005 010 005 010 015 015 0058.60 0146.51 0002.50 | raise NothingChanged
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | def format_cell(src: str, *, fast: bool, mode: Mode) -> str:
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | """Format code in given cell of Jupyter notebook.
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 |
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | General idea is:
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 |
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | - if cell has trailing semicolon, remove it;
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | - if cell has IPython magics, mask them;
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | - format cell;
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | - reinstate IPython magics;
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | - reinstate trailing semicolon (if originally present);
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | - strip trailing newlines.
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 |
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | Cells with syntax errors will not be processed, as they
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | could potentially be automagics or multi-line magics, which
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | are currently not supported.
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | """
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | validate_cell(src, mode)
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | src_without_trailing_semicolon, has_trailing_semicolon = remove_trailing_semicolon(
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | src
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | )
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | try:
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | masked_src, replacements = mask_cell(src_without_trailing_semicolon)
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | except SyntaxError:
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | raise NothingChanged from None
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | masked_dst = format_str(masked_src, mode=mode)
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | if not fast:
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | check_stability_and_equivalence(masked_src, masked_dst, mode=mode)
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | dst_without_trailing_semicolon = unmask_cell(masked_dst, replacements)
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | dst = put_trailing_semicolon_back(
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | dst_without_trailing_semicolon, has_trailing_semicolon
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | )
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | dst = dst.rstrip("\n")
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | if dst == src:
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | raise NothingChanged from None
0035 0017 0020 0000 0012 0003 0000 -- 04 002 003 002 003 005 005 0011.61 0011.61 0001.00 | return dst
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0010 0005 0004 0000 0005 0001 0000 -- 03 003 005 003 006 008 009 0027.00 0048.60 0001.80 | def validate_metadata(nb: MutableMapping[str, Any]) -> None:
0010 0005 0004 0000 0005 0001 0000 -- 03 003 005 003 006 008 009 0027.00 0048.60 0001.80 | """If notebook is marked as non-Python, don't format it.
0010 0005 0004 0000 0005 0001 0000 -- 03 003 005 003 006 008 009 0027.00 0048.60 0001.80 |
0010 0005 0004 0000 0005 0001 0000 -- 03 003 005 003 006 008 009 0027.00 0048.60 0001.80 | All notebook metadata fields are optional, see
0010 0005 0004 0000 0005 0001 0000 -- 03 003 005 003 006 008 009 0027.00 0048.60 0001.80 | https://nbformat.readthedocs.io/en/latest/format_description.html. So
0010 0005 0004 0000 0005 0001 0000 -- 03 003 005 003 006 008 009 0027.00 0048.60 0001.80 | if a notebook has empty metadata, we will try to parse it anyway.
0010 0005 0004 0000 0005 0001 0000 -- 03 003 005 003 006 008 009 0027.00 0048.60 0001.80 | """
0010 0005 0004 0000 0005 0001 0000 -- 03 003 005 003 006 008 009 0027.00 0048.60 0001.80 | language = nb.get("metadata", {}).get("language_info", {}).get("name", None)
0010 0005 0004 0000 0005 0001 0000 -- 03 003 005 003 006 008 009 0027.00 0048.60 0001.80 | if language is not None and language != "python":
0010 0005 0004 0000 0005 0001 0000 -- 03 003 005 003 006 008 009 0027.00 0048.60 0001.80 | raise NothingChanged from None
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | def format_ipynb_string(src_contents: str, *, fast: bool, mode: Mode) -> FileContent:
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | """Format Jupyter notebook.
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 |
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | Operate cell-by-cell, only on code cells, only for Python notebooks.
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | If the ``.ipynb`` originally had a trailing newline, it'll be preserved.
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | """
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | if not src_contents:
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | raise NothingChanged
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 |
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | trailing_newline = src_contents[-1] == "\n"
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | modified = False
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | nb = json.loads(src_contents)
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | validate_metadata(nb)
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | for cell in nb["cells"]:
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | if cell.get("cell_type", None) == "code":
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | try:
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | src = "".join(cell["source"])
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | dst = format_cell(src, fast=fast, mode=mode)
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | except NothingChanged:
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | pass
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | else:
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | cell["source"] = dst.splitlines(keepends=True)
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | modified = True
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | if modified:
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | dst_contents = json.dumps(nb, indent=1, ensure_ascii=False)
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | if trailing_newline:
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | dst_contents = dst_contents + "\n"
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | return dst_contents
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | else:
0030 0025 0024 0000 0004 0002 0000 -- 08 004 007 005 008 011 013 0044.97 0102.79 0002.29 | raise NothingChanged
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def format_str(
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | src_contents: str, *, mode: Mode, lines: Collection[Tuple[int, int]] = ()
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ) -> str:
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Reformat a string and return new contents.
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | `mode` determines formatting options, such as how many characters per line are
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | allowed. Example:
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | >>> import black
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | >>> print(black.format_str("def f(arg:str='')->None:...", mode=black.Mode()))
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def f(arg: str = "") -> None:
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ...
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | A more complex example:
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | >>> print(
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ... black.format_str(
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ... "def f(arg:str='')->None: hey",
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ... mode=black.Mode(
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ... target_versions={black.TargetVersion.PY36},
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ... line_length=10,
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ... string_normalization=False,
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ... is_pyi=False,
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ... ),
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ... ),
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ... )
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def f(
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | arg: str = '',
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ) -> None:
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | hey
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | dst_contents = _format_str_once(src_contents, mode=mode, lines=lines)
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # Forced second pass to work around optional trailing commas (becoming
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # forced trailing commas on pass 2) interacting differently with optional
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # parentheses. Admittedly ugly.
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | if src_contents != dst_contents:
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | if lines:
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | lines = adjusted_lines(lines, src_contents, dst_contents)
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return _format_str_once(dst_contents, mode=mode, lines=lines)
0041 0008 0009 0003 0024 0005 0003 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return dst_contents
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | def _format_str_once(
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | src_contents: str, *, mode: Mode, lines: Collection[Tuple[int, int]] = ()
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | ) -> str:
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | src_node = lib2to3_parse(src_contents.lstrip(), mode.target_versions)
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | dst_blocks: List[LinesBlock] = []
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | if mode.target_versions:
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | versions = mode.target_versions
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | else:
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | future_imports = get_future_imports(src_node)
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | versions = detect_target_versions(src_node, future_imports=future_imports)
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 |
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | context_manager_features = {
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | feature
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | for feature in {Feature.PARENTHESIZED_CONTEXT_MANAGERS}
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | if supports_feature(versions, feature)
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | }
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | normalize_fmt_off(src_node, mode, lines)
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | if lines:
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | # This should be called after normalize_fmt_off.
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | convert_unchanged_lines(src_node, lines)
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 |
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | line_generator = LineGenerator(mode=mode, features=context_manager_features)
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | elt = EmptyLineTracker(mode=mode)
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | split_line_features = {
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | feature
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | for feature in {Feature.TRAILING_COMMA_IN_CALL, Feature.TRAILING_COMMA_IN_DEF}
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | if supports_feature(versions, feature)
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | }
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | block: Optional[LinesBlock] = None
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | for current_line in line_generator.visit(src_node):
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | block = elt.maybe_empty_lines(current_line)
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | dst_blocks.append(block)
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | for line in transform_line(
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | current_line, mode=mode, features=split_line_features
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | ):
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | block.content_lines.append(str(line))
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | if dst_blocks:
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | dst_blocks[-1].after = 0
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | dst_contents = []
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | for block in dst_blocks:
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | dst_contents.extend(block.all_lines())
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | if not dst_contents:
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | # Use decode_bytes to retrieve the correct source newline (CRLF or LF),
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | # and check if normalized_content has more than one line
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | normalized_content, _, newline = decode_bytes(src_contents.encode("utf-8"))
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | if "\n" in normalized_content:
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | return newline
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | return ""
0049 0034 0044 0003 0000 0002 0003 -- 13 003 004 003 004 007 007 0019.65 0029.48 0001.50 | return "".join(dst_contents)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0015 0011 0009 0000 0004 0002 0000 -- 03 003 004 003 004 007 007 0019.65 0029.48 0001.50 | def decode_bytes(src: bytes) -> Tuple[FileContent, Encoding, NewLine]:
0015 0011 0009 0000 0004 0002 0000 -- 03 003 004 003 004 007 007 0019.65 0029.48 0001.50 | """Return a tuple of (decoded_contents, encoding, newline).
0015 0011 0009 0000 0004 0002 0000 -- 03 003 004 003 004 007 007 0019.65 0029.48 0001.50 |
0015 0011 0009 0000 0004 0002 0000 -- 03 003 004 003 004 007 007 0019.65 0029.48 0001.50 | `newline` is either CRLF or LF but `decoded_contents` is decoded with
0015 0011 0009 0000 0004 0002 0000 -- 03 003 004 003 004 007 007 0019.65 0029.48 0001.50 | universal newlines (i.e. only contains LF).
0015 0011 0009 0000 0004 0002 0000 -- 03 003 004 003 004 007 007 0019.65 0029.48 0001.50 | """
0015 0011 0009 0000 0004 0002 0000 -- 03 003 004 003 004 007 007 0019.65 0029.48 0001.50 | srcbuf = io.BytesIO(src)
0015 0011 0009 0000 0004 0002 0000 -- 03 003 004 003 004 007 007 0019.65 0029.48 0001.50 | encoding, lines = tokenize.detect_encoding(srcbuf.readline)
0015 0011 0009 0000 0004 0002 0000 -- 03 003 004 003 004 007 007 0019.65 0029.48 0001.50 | if not lines:
0015 0011 0009 0000 0004 0002 0000 -- 03 003 004 003 004 007 007 0019.65 0029.48 0001.50 | return "", encoding, "\n"
0015 0011 0009 0000 0004 0002 0000 -- 03 003 004 003 004 007 007 0019.65 0029.48 0001.50 |
0015 0011 0009 0000 0004 0002 0000 -- 03 003 004 003 004 007 007 0019.65 0029.48 0001.50 | newline = "\r\n" if b"\r\n" == lines[0][-2:] else "\n"
0015 0011 0009 0000 0004 0002 0000 -- 03 003 004 003 004 007 007 0019.65 0029.48 0001.50 | srcbuf.seek(0)
0015 0011 0009 0000 0004 0002 0000 -- 03 003 004 003 004 007 007 0019.65 0029.48 0001.50 | with io.TextIOWrapper(srcbuf, encoding) as tiow:
0015 0011 0009 0000 0004 0002 0000 -- 03 003 004 003 004 007 007 0019.65 0029.48 0001.50 | return tiow.read(), encoding, newline
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | def get_features_used( # noqa: C901
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | node: Node, *, future_imports: Optional[Set[str]] = None
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | ) -> Set[Feature]:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | """Return a set of (relatively) new Python features used in this file.
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | Currently looking for:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | - f-strings;
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | - self-documenting expressions in f-strings (f"{x=}");
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | - underscores in numeric literals;
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | - trailing commas after * or ** in function signatures and calls;
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | - positional only arguments in function signatures and lambdas;
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | - assignment expression;
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | - relaxed decorator syntax;
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | - usage of __future__ flags (annotations);
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | - print / exec statements;
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | - parenthesized context managers;
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | - match statements;
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | - except* clause;
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | - variadic generics;
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | """
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features: Set[Feature] = set()
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | if future_imports:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features |= {
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | FUTURE_FLAG_TO_FEATURE[future_import]
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | for future_import in future_imports
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | if future_import in FUTURE_FLAG_TO_FEATURE
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | }
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | for n in node.pre_order():
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | if is_string_token(n):
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | value_head = n.value[:2]
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | if value_head in {'f"', 'F"', "f'", "F'", "rf", "fr", "RF", "FR"}:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features.add(Feature.F_STRINGS)
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | if Feature.DEBUG_F_STRINGS not in features:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | for span_beg, span_end in iter_fexpr_spans(n.value):
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | if n.value[span_beg : span_end - 1].rstrip().endswith("="):
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features.add(Feature.DEBUG_F_STRINGS)
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | break
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | elif is_number_token(n):
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | if "_" in n.value:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features.add(Feature.NUMERIC_UNDERSCORES)
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | elif n.type == token.SLASH:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | if n.parent and n.parent.type in {
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | syms.typedargslist,
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | syms.arglist,
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | syms.varargslist,
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | }:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features.add(Feature.POS_ONLY_ARGUMENTS)
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | elif n.type == token.COLONEQUAL:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features.add(Feature.ASSIGNMENT_EXPRESSIONS)
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | elif n.type == syms.decorator:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | if len(n.children) > 1 and not is_simple_decorator_expression(
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | n.children[1]
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | ):
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features.add(Feature.RELAXED_DECORATORS)
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | elif (
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | n.type in {syms.typedargslist, syms.arglist}
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | and n.children
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | and n.children[-1].type == token.COMMA
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | ):
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | if n.type == syms.typedargslist:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | feature = Feature.TRAILING_COMMA_IN_DEF
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | else:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | feature = Feature.TRAILING_COMMA_IN_CALL
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | for ch in n.children:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | if ch.type in STARS:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features.add(feature)
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | if ch.type == syms.argument:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | for argch in ch.children:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | if argch.type in STARS:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features.add(feature)
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | elif (
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | n.type in {syms.return_stmt, syms.yield_expr}
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | and len(n.children) >= 2
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | and n.children[1].type == syms.testlist_star_expr
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | and any(child.type == syms.star_expr for child in n.children[1].children)
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | ):
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features.add(Feature.UNPACKING_ON_FLOW)
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | elif (
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | n.type == syms.annassign
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | and len(n.children) >= 4
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | and n.children[3].type == syms.testlist_star_expr
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | ):
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features.add(Feature.ANN_ASSIGN_EXTENDED_RHS)
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | elif (
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | n.type == syms.with_stmt
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | and len(n.children) > 2
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | and n.children[1].type == syms.atom
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | ):
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | atom_children = n.children[1].children
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | if (
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | len(atom_children) == 3
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | and atom_children[0].type == token.LPAR
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | and _contains_asexpr(atom_children[1])
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | and atom_children[2].type == token.RPAR
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | ):
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features.add(Feature.PARENTHESIZED_CONTEXT_MANAGERS)
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | elif n.type == syms.match_stmt:
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features.add(Feature.PATTERN_MATCHING)
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | elif (
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | n.type == syms.except_clause
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | and len(n.children) >= 2
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | and n.children[1].type == token.STAR
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | ):
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features.add(Feature.EXCEPT_STAR)
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | elif n.type in {syms.subscriptlist, syms.trailer} and any(
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | child.type == syms.star_expr for child in n.children
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | ):
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features.add(Feature.VARIADIC_GENERICS)
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | elif (
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | n.type == syms.tname_star
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | and len(n.children) == 3
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | and n.children[2].type == syms.star_expr
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | ):
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features.add(Feature.VARIADIC_GENERICS)
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | elif n.type in (syms.type_stmt, syms.typeparams):
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | features.add(Feature.TYPE_PARAMS)
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 |
0134 0059 0100 0001 0016 0018 0000 -- 54 010 075 052 111 085 163 1044.73 7731.01 0007.40 | return features
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0014 0010 0013 0000 0000 0000 0001 -- 08 002 011 007 015 013 022 0081.41 0111.01 0001.36 | def _contains_asexpr(node: Union[Node, Leaf]) -> bool:
0014 0010 0013 0000 0000 0000 0001 -- 08 002 011 007 015 013 022 0081.41 0111.01 0001.36 | """Return True if `node` contains an as-pattern."""
0014 0010 0013 0000 0000 0000 0001 -- 08 002 011 007 015 013 022 0081.41 0111.01 0001.36 | if node.type == syms.asexpr_test:
0014 0010 0013 0000 0000 0000 0001 -- 08 002 011 007 015 013 022 0081.41 0111.01 0001.36 | return True
0014 0010 0013 0000 0000 0000 0001 -- 08 002 011 007 015 013 022 0081.41 0111.01 0001.36 | elif node.type == syms.atom:
0014 0010 0013 0000 0000 0000 0001 -- 08 002 011 007 015 013 022 0081.41 0111.01 0001.36 | if (
0014 0010 0013 0000 0000 0000 0001 -- 08 002 011 007 015 013 022 0081.41 0111.01 0001.36 | len(node.children) == 3
0014 0010 0013 0000 0000 0000 0001 -- 08 002 011 007 015 013 022 0081.41 0111.01 0001.36 | and node.children[0].type == token.LPAR
0014 0010 0013 0000 0000 0000 0001 -- 08 002 011 007 015 013 022 0081.41 0111.01 0001.36 | and node.children[2].type == token.RPAR
0014 0010 0013 0000 0000 0000 0001 -- 08 002 011 007 015 013 022 0081.41 0111.01 0001.36 | ):
0014 0010 0013 0000 0000 0000 0001 -- 08 002 011 007 015 013 022 0081.41 0111.01 0001.36 | return _contains_asexpr(node.children[1])
0014 0010 0013 0000 0000 0000 0001 -- 08 002 011 007 015 013 022 0081.41 0111.01 0001.36 | elif node.type == syms.testlist_gexp:
0014 0010 0013 0000 0000 0000 0001 -- 08 002 011 007 015 013 022 0081.41 0111.01 0001.36 | return any(_contains_asexpr(child) for child in node.children)
0014 0010 0013 0000 0000 0000 0001 -- 08 002 011 007 015 013 022 0081.41 0111.01 0001.36 | return False
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0008 0004 0007 0000 0000 0000 0001 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def detect_target_versions(
0008 0004 0007 0000 0000 0000 0001 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | node: Node, *, future_imports: Optional[Set[str]] = None
0008 0004 0007 0000 0000 0000 0001 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ) -> Set[TargetVersion]:
0008 0004 0007 0000 0000 0000 0001 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Detect the version to target based on the nodes used."""
0008 0004 0007 0000 0000 0000 0001 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | features = get_features_used(node, future_imports=future_imports)
0008 0004 0007 0000 0000 0000 0001 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return {
0008 0004 0007 0000 0000 0000 0001 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | version for version in TargetVersion if features <= VERSION_TO_FEATURES[version]
0008 0004 0007 0000 0000 0000 0001 -- -- 001 002 001 002 003 003 0004.75 0002.38 0000.50 | }
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | def get_future_imports(node: Node) -> Set[str]:
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | """Return a set of __future__ imports in the file."""
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | imports: Set[str] = set()
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 |
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | def get_imports_from_children(children: List[LN]) -> Generator[str, None, None]:
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | for child in children:
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | if isinstance(child, Leaf):
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | if child.type == token.NAME:
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | yield child.value
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 |
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | elif child.type == syms.import_as_name:
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | orig_name = child.children[0]
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | assert isinstance(orig_name, Leaf), "Invalid syntax parsing imports"
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | assert orig_name.type == token.NAME, "Invalid syntax parsing imports"
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | yield orig_name.value
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 |
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | elif child.type == syms.import_as_names:
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | yield from get_imports_from_children(child.children)
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 |
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | else:
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | raise AssertionError("Invalid syntax parsing imports")
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 |
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | for child in node.children:
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | if child.type != syms.simple_stmt:
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | break
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 |
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | first_child = child.children[0]
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | if isinstance(first_child, Leaf):
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | # Continue looking if we see a docstring; otherwise stop.
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | if (
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | len(child.children) == 2
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | and first_child.type == token.STRING
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | and child.children[1].type == token.NEWLINE
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | ):
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | continue
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 |
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | break
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 |
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | elif first_child.type == syms.import_from:
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | module_name = first_child.children[1]
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | if not isinstance(module_name, Leaf) or module_name.value != "__future__":
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | break
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 |
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | imports |= set(get_imports_from_children(first_child.children[3:]))
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | else:
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | break
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 |
0048 0035 0036 0001 0000 0010 0002 -- 10 006 021 014 028 027 042 0199.71 0798.82 0004.00 | return imports
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def assert_equivalent(src: str, dst: str) -> None:
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Raise AssertionError if `src` and `dst` aren't equivalent."""
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | try:
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | src_ast = parse_ast(src)
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | except Exception as exc:
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | raise AssertionError(
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | "cannot use --safe with this file; failed to parse source file AST: "
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | f"{exc}\n"
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | "This could be caused by running Black with an older Python version "
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | "that does not support new syntax used in your source file."
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ) from exc
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | try:
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | dst_ast = parse_ast(dst)
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | except Exception as exc:
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | log = dump_to_file("".join(traceback.format_tb(exc.__traceback__)), dst)
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | raise AssertionError(
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | f"INTERNAL ERROR: Black produced invalid code: {exc}. "
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | "Please report a bug on https://github.com/psf/black/issues. "
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | f"This invalid output might be helpful: {log}"
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ) from None
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | src_ast_str = "\n".join(stringify_ast(src_ast))
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | dst_ast_str = "\n".join(stringify_ast(dst_ast))
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | if src_ast_str != dst_ast_str:
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | log = dump_to_file(diff(src_ast_str, dst_ast_str, "src", "dst"))
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | raise AssertionError(
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | "INTERNAL ERROR: Black produced code that is not equivalent to the"
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | " source. Please report a bug on "
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | f"https://github.com/psf/black/issues. This diff might be helpful: {log}"
0031 0016 0028 0000 0000 0002 0001 -- 04 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ) from None
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def assert_stable(
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | src: str, dst: str, mode: Mode, *, lines: Collection[Tuple[int, int]] = ()
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ) -> None:
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Raise AssertionError if `dst` reformats differently the second time."""
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | if lines:
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # Formatting specified lines requires `adjusted_lines` to map original lines
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # to the formatted lines before re-formatting the previously formatted result.
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # Due to less-ideal diff algorithm, some edge cases produce incorrect new line
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # ranges. Hence for now, we skip the stable check.
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # See https://github.com/psf/black/issues/4033 for context.
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # We shouldn't call format_str() here, because that formats the string
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # twice and may hide a bug where we bounce back and forth between two
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # versions.
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | newdst = _format_str_once(dst, mode=mode, lines=lines)
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | if dst != newdst:
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | log = dump_to_file(
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | str(mode),
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | diff(src, dst, "source", "first pass"),
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | diff(dst, newdst, "first pass", "second pass"),
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | )
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | raise AssertionError(
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | "INTERNAL ERROR: Black produced different code on the second pass of the"
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | " formatter. Please report a bug on https://github.com/psf/black/issues."
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | f" This diff might be helpful: {log}"
0026 0008 0017 0008 0000 0000 0009 -- 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | ) from None
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @contextmanager
0007 0004 0003 0000 0003 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def nullcontext() -> Iterator[None]:
0007 0004 0003 0000 0003 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """Return an empty context manager.
0007 0004 0003 0000 0003 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0007 0004 0003 0000 0003 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | To be used like `nullcontext` in Python 3.7.
0007 0004 0003 0000 0003 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """
0007 0004 0003 0000 0003 0001 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | yield
0007 0004 0003 0000 0003 0001 0000 -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0009 0005 0005 0002 0000 0002 0002 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def patched_main() -> None:
0009 0005 0005 0002 0000 0002 0002 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | # PyInstaller patches multiprocessing to need freeze_support() even in non-Windows
0009 0005 0005 0002 0000 0002 0002 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | # environments so just assume we always need to call it if frozen.
0009 0005 0005 0002 0000 0002 0002 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | if getattr(sys, "frozen", False):
0009 0005 0005 0002 0000 0002 0002 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | from multiprocessing import freeze_support
0009 0005 0005 0002 0000 0002 0002 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0009 0005 0005 0002 0000 0002 0002 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | freeze_support()
0009 0005 0005 0002 0000 0002 0002 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0009 0005 0005 0002 0000 0002 0002 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | main()
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | if __name__ == "__main__":
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | patched_main()