-
-
Notifications
You must be signed in to change notification settings - Fork 597
/
Copy pathtest_isort.py
5720 lines (4953 loc) · 182 KB
/
test_isort.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
"""Tests all major functionality of the isort library
Should be ran using py.test by simply running py.test in the isort project directory
"""
import os
import os.path
import subprocess
import sys
from io import StringIO
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Set, Tuple
import py
import pytest
import isort
from isort import api, files, sections
from isort.exceptions import ExistingSyntaxErrors, FileSkipped, MissingSection
from isort.settings import Config
from isort.utils import exists_case_sensitive
from .utils import UnreadableStream, as_stream
if TYPE_CHECKING:
WrapModes: Any
else:
from isort.wrap_modes import WrapModes
TEST_DEFAULT_CONFIG = """
[*.{py,pyi}]
max_line_length = 120
indent_style = space
indent_size = 4
known_first_party = isort
known_third_party = kate
known_something_else = something_entirely_different
sections = FUTURE, STDLIB, THIRDPARTY, FIRSTPARTY, LOCALFOLDER, SOMETHING_ELSE
ignore_frosted_errors = E103
skip = build,.tox,venv
balanced_wrapping = true
"""
SHORT_IMPORT = "from third_party import lib1, lib2, lib3, lib4"
SINGLE_FROM_IMPORT = "from third_party import lib1"
SINGLE_LINE_LONG_IMPORT = "from third_party import lib1, lib2, lib3, lib4, lib5, lib5ab"
REALLY_LONG_IMPORT = (
"from third_party import lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9, lib10, lib11,"
"lib12, lib13, lib14, lib15, lib16, lib17, lib18, lib20, lib21, lib22"
)
REALLY_LONG_IMPORT_WITH_COMMENT = (
"from third_party import lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9, "
"lib10, lib11, lib12, lib13, lib14, lib15, lib16, lib17, lib18, lib20, lib21, lib22"
" # comment"
)
@pytest.fixture(scope="session", autouse=True)
def default_settings_path(tmpdir_factory) -> Iterator[str]:
config_dir = tmpdir_factory.mktemp("config")
config_file = config_dir.join(".editorconfig").strpath
with open(config_file, "w") as editorconfig:
editorconfig.write(TEST_DEFAULT_CONFIG)
assert Config(config_file).known_other
with config_dir.as_cwd():
yield config_dir.strpath
def test_happy_path() -> None:
"""Test the most basic use case, straight imports no code, simply not organized by category."""
test_input = "import sys\nimport os\nimport myproject.test\nimport django.settings"
test_output = isort.code(test_input, known_first_party=["myproject"])
assert test_output == (
"import os\n" "import sys\n" "\n" "import django.settings\n" "\n" "import myproject.test\n"
)
def test_code_intermixed() -> None:
"""Defines what should happen when isort encounters imports intermixed with
code.
(it should pull them all to the top)
"""
test_input = (
"import sys\n"
"print('yo')\n"
"print('I like to put code between imports cause I want stuff to break')\n"
"import myproject.test\n"
)
test_output = isort.code(test_input)
assert test_output == (
"import sys\n"
"\n"
"print('yo')\n"
"print('I like to put code between imports cause I want stuff to break')\n"
"import myproject.test\n"
)
def test_correct_space_between_imports() -> None:
"""Ensure after imports a correct amount of space (in newlines) is
enforced.
(2 for method, class, or decorator definitions 1 for anything else)
"""
test_input_method = "import sys\ndef my_method():\n print('hello world')\n"
test_output_method = isort.code(test_input_method)
assert test_output_method == ("import sys\n\n\ndef my_method():\n print('hello world')\n")
test_input_decorator = (
"import sys\n" "@my_decorator\n" "def my_method():\n" " print('hello world')\n"
)
test_output_decorator = isort.code(test_input_decorator)
assert test_output_decorator == (
"import sys\n" "\n" "\n" "@my_decorator\n" "def my_method():\n" " print('hello world')\n"
)
test_input_class = "import sys\nclass MyClass(object):\n pass\n"
test_output_class = isort.code(test_input_class)
assert test_output_class == "import sys\n\n\nclass MyClass(object):\n pass\n"
test_input_other = "import sys\nprint('yo')\n"
test_output_other = isort.code(test_input_other)
assert test_output_other == "import sys\n\nprint('yo')\n"
test_input_inquotes = (
"import sys\n"
"@my_decorator('''hello\nworld''')\n"
"def my_method():\n"
" print('hello world')\n"
)
test_output_inquotes = api.sort_code_string(test_input_inquotes)
assert (
test_output_inquotes == "import sys\n"
"\n\n"
"@my_decorator('''hello\nworld''')\n"
"def my_method():\n"
" print('hello world')\n"
)
test_input_assign = "import sys\nVAR = 1\n"
test_output_assign = api.sort_code_string(test_input_assign)
assert test_output_assign == "import sys\n\nVAR = 1\n"
test_input_assign = "import sys\nVAR = 1\ndef y():\n"
test_output_assign = api.sort_code_string(test_input_assign)
assert test_output_assign == "import sys\n\nVAR = 1\ndef y():\n"
test_input = """
import os
x = "hi"
def x():
pass
"""
assert isort.code(test_input) == test_input
def test_sort_on_number() -> None:
"""Ensure numbers get sorted logically (10 > 9 not the other way around)"""
test_input = "import lib10\nimport lib9\n"
test_output = isort.code(test_input)
assert test_output == "import lib9\nimport lib10\n"
def test_line_length() -> None:
"""Ensure isort enforces the set line_length."""
assert len(isort.code(REALLY_LONG_IMPORT, line_length=80).split("\n")[0]) <= 80
assert len(isort.code(REALLY_LONG_IMPORT, line_length=120).split("\n")[0]) <= 120
test_output = isort.code(REALLY_LONG_IMPORT, line_length=42)
assert test_output == (
"from third_party import (lib1, lib2, lib3,\n"
" lib4, lib5, lib6,\n"
" lib7, lib8, lib9,\n"
" lib10, lib11,\n"
" lib12, lib13,\n"
" lib14, lib15,\n"
" lib16, lib17,\n"
" lib18, lib20,\n"
" lib21, lib22)\n"
)
test_input = (
"from django.contrib.gis.gdal.field import (\n"
" OFTDate, OFTDateTime, OFTInteger, OFTInteger64, OFTReal, OFTString,\n"
" OFTTime,\n"
")\n"
) # Test case described in issue #654
assert (
isort.code(
code=test_input,
include_trailing_comma=True,
line_length=79,
multi_line_output=WrapModes.VERTICAL_GRID_GROUPED,
balanced_wrapping=False,
)
== test_input
)
test_output = isort.code(code=REALLY_LONG_IMPORT, line_length=42, wrap_length=32)
assert test_output == (
"from third_party import (lib1,\n"
" lib2,\n"
" lib3,\n"
" lib4,\n"
" lib5,\n"
" lib6,\n"
" lib7,\n"
" lib8,\n"
" lib9,\n"
" lib10,\n"
" lib11,\n"
" lib12,\n"
" lib13,\n"
" lib14,\n"
" lib15,\n"
" lib16,\n"
" lib17,\n"
" lib18,\n"
" lib20,\n"
" lib21,\n"
" lib22)\n"
)
test_input = (
"from .test import a_very_long_function_name_that_exceeds_the_normal_pep8_line_length\n"
)
with pytest.raises(ValueError):
test_output = isort.code(code=REALLY_LONG_IMPORT, line_length=80, wrap_length=99)
assert (
isort.code(REALLY_LONG_IMPORT, line_length=100, wrap_length=99)
== """
from third_party import (lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9, lib10, lib11, lib12,
lib13, lib14, lib15, lib16, lib17, lib18, lib20, lib21, lib22)
""".lstrip()
)
# Test Case described in issue #1015
test_output = isort.code(
REALLY_LONG_IMPORT, line_length=25, multi_line_output=WrapModes.HANGING_INDENT
)
assert test_output == (
"from third_party import \\\n"
" lib1, lib2, lib3, \\\n"
" lib4, lib5, lib6, \\\n"
" lib7, lib8, lib9, \\\n"
" lib10, lib11, \\\n"
" lib12, lib13, \\\n"
" lib14, lib15, \\\n"
" lib16, lib17, \\\n"
" lib18, lib20, \\\n"
" lib21, lib22\n"
)
def test_output_modes() -> None:
"""Test setting isort to use various output modes works as expected"""
test_output_grid = isort.code(
code=REALLY_LONG_IMPORT, multi_line_output=WrapModes.GRID, line_length=40
)
assert test_output_grid == (
"from third_party import (lib1, lib2,\n"
" lib3, lib4,\n"
" lib5, lib6,\n"
" lib7, lib8,\n"
" lib9, lib10,\n"
" lib11, lib12,\n"
" lib13, lib14,\n"
" lib15, lib16,\n"
" lib17, lib18,\n"
" lib20, lib21,\n"
" lib22)\n"
)
test_output_vertical = isort.code(
code=REALLY_LONG_IMPORT, multi_line_output=WrapModes.VERTICAL, line_length=40
)
assert test_output_vertical == (
"from third_party import (lib1,\n"
" lib2,\n"
" lib3,\n"
" lib4,\n"
" lib5,\n"
" lib6,\n"
" lib7,\n"
" lib8,\n"
" lib9,\n"
" lib10,\n"
" lib11,\n"
" lib12,\n"
" lib13,\n"
" lib14,\n"
" lib15,\n"
" lib16,\n"
" lib17,\n"
" lib18,\n"
" lib20,\n"
" lib21,\n"
" lib22)\n"
)
comment_output_vertical = isort.code(
code=REALLY_LONG_IMPORT_WITH_COMMENT, multi_line_output=WrapModes.VERTICAL, line_length=40
)
assert comment_output_vertical == (
"from third_party import (lib1, # comment\n"
" lib2,\n"
" lib3,\n"
" lib4,\n"
" lib5,\n"
" lib6,\n"
" lib7,\n"
" lib8,\n"
" lib9,\n"
" lib10,\n"
" lib11,\n"
" lib12,\n"
" lib13,\n"
" lib14,\n"
" lib15,\n"
" lib16,\n"
" lib17,\n"
" lib18,\n"
" lib20,\n"
" lib21,\n"
" lib22)\n"
)
test_output_hanging_indent = isort.code(
code=REALLY_LONG_IMPORT,
multi_line_output=WrapModes.HANGING_INDENT,
line_length=40,
indent=" ",
)
assert test_output_hanging_indent == (
"from third_party import lib1, lib2, \\\n"
" lib3, lib4, lib5, lib6, lib7, \\\n"
" lib8, lib9, lib10, lib11, lib12, \\\n"
" lib13, lib14, lib15, lib16, lib17, \\\n"
" lib18, lib20, lib21, lib22\n"
)
comment_output_hanging_indent = isort.code(
code=REALLY_LONG_IMPORT_WITH_COMMENT,
multi_line_output=WrapModes.HANGING_INDENT,
line_length=40,
indent=" ",
)
assert comment_output_hanging_indent == (
"from third_party import lib1, lib2, \\\n"
" lib3, lib4, lib5, lib6, lib7, \\\n"
" lib8, lib9, lib10, lib11, lib12, \\\n"
" lib13, lib14, lib15, lib16, lib17, \\\n"
" lib18, lib20, lib21, lib22 \\\n"
" # comment\n"
)
test_output_vertical_indent = isort.code(
code=REALLY_LONG_IMPORT,
multi_line_output=WrapModes.VERTICAL_HANGING_INDENT,
line_length=40,
indent=" ",
)
assert test_output_vertical_indent == (
"from third_party import (\n"
" lib1,\n"
" lib2,\n"
" lib3,\n"
" lib4,\n"
" lib5,\n"
" lib6,\n"
" lib7,\n"
" lib8,\n"
" lib9,\n"
" lib10,\n"
" lib11,\n"
" lib12,\n"
" lib13,\n"
" lib14,\n"
" lib15,\n"
" lib16,\n"
" lib17,\n"
" lib18,\n"
" lib20,\n"
" lib21,\n"
" lib22\n"
")\n"
)
comment_output_vertical_indent = isort.code(
code=REALLY_LONG_IMPORT_WITH_COMMENT,
multi_line_output=WrapModes.VERTICAL_HANGING_INDENT,
line_length=40,
indent=" ",
)
assert comment_output_vertical_indent == (
"from third_party import ( # comment\n"
" lib1,\n"
" lib2,\n"
" lib3,\n"
" lib4,\n"
" lib5,\n"
" lib6,\n"
" lib7,\n"
" lib8,\n"
" lib9,\n"
" lib10,\n"
" lib11,\n"
" lib12,\n"
" lib13,\n"
" lib14,\n"
" lib15,\n"
" lib16,\n"
" lib17,\n"
" lib18,\n"
" lib20,\n"
" lib21,\n"
" lib22\n"
")\n"
)
test_output_vertical_grid = isort.code(
code=REALLY_LONG_IMPORT,
multi_line_output=WrapModes.VERTICAL_GRID,
line_length=40,
indent=" ",
)
assert test_output_vertical_grid == (
"from third_party import (\n"
" lib1, lib2, lib3, lib4, lib5, lib6,\n"
" lib7, lib8, lib9, lib10, lib11,\n"
" lib12, lib13, lib14, lib15, lib16,\n"
" lib17, lib18, lib20, lib21, lib22)\n"
)
comment_output_vertical_grid = isort.code(
code=REALLY_LONG_IMPORT_WITH_COMMENT,
multi_line_output=WrapModes.VERTICAL_GRID,
line_length=40,
indent=" ",
)
assert comment_output_vertical_grid == (
"from third_party import ( # comment\n"
" lib1, lib2, lib3, lib4, lib5, lib6,\n"
" lib7, lib8, lib9, lib10, lib11,\n"
" lib12, lib13, lib14, lib15, lib16,\n"
" lib17, lib18, lib20, lib21, lib22)\n"
)
test_output_vertical_grid_grouped = isort.code(
code=REALLY_LONG_IMPORT,
multi_line_output=WrapModes.VERTICAL_GRID_GROUPED,
line_length=40,
indent=" ",
)
assert test_output_vertical_grid_grouped == (
"from third_party import (\n"
" lib1, lib2, lib3, lib4, lib5, lib6,\n"
" lib7, lib8, lib9, lib10, lib11,\n"
" lib12, lib13, lib14, lib15, lib16,\n"
" lib17, lib18, lib20, lib21, lib22\n"
")\n"
)
comment_output_vertical_grid_grouped = isort.code(
code=REALLY_LONG_IMPORT_WITH_COMMENT,
multi_line_output=WrapModes.VERTICAL_GRID_GROUPED,
line_length=40,
indent=" ",
)
assert comment_output_vertical_grid_grouped == (
"from third_party import ( # comment\n"
" lib1, lib2, lib3, lib4, lib5, lib6,\n"
" lib7, lib8, lib9, lib10, lib11,\n"
" lib12, lib13, lib14, lib15, lib16,\n"
" lib17, lib18, lib20, lib21, lib22\n"
")\n"
)
output_noqa = isort.code(code=REALLY_LONG_IMPORT_WITH_COMMENT, multi_line_output=WrapModes.NOQA)
assert output_noqa == (
"from third_party import lib1, lib2, lib3, lib4, lib5, lib6, lib7,"
" lib8, lib9, lib10, lib11,"
" lib12, lib13, lib14, lib15, lib16, lib17, lib18, lib20, lib21, lib22 "
"# NOQA comment\n"
)
test_case = isort.code(
code=SINGLE_LINE_LONG_IMPORT,
multi_line_output=WrapModes.VERTICAL_GRID_GROUPED,
line_length=40,
indent=" ",
)
test_output_vertical_grid_grouped_doesnt_wrap_early = test_case
assert test_output_vertical_grid_grouped_doesnt_wrap_early == (
"from third_party import (\n lib1, lib2, lib3, lib4, lib5, lib5ab\n)\n"
)
test_output_prefix_from_module = isort.code(
code=REALLY_LONG_IMPORT,
multi_line_output=WrapModes.VERTICAL_PREFIX_FROM_MODULE_IMPORT,
line_length=40,
)
assert test_output_prefix_from_module == (
"from third_party import lib1, lib2\n"
"from third_party import lib3, lib4\n"
"from third_party import lib5, lib6\n"
"from third_party import lib7, lib8\n"
"from third_party import lib9, lib10\n"
"from third_party import lib11, lib12\n"
"from third_party import lib13, lib14\n"
"from third_party import lib15, lib16\n"
"from third_party import lib17, lib18\n"
"from third_party import lib20, lib21\n"
"from third_party import lib22\n"
)
test_output_prefix_from_module_with_comment = isort.code(
code=REALLY_LONG_IMPORT_WITH_COMMENT,
multi_line_output=WrapModes.VERTICAL_PREFIX_FROM_MODULE_IMPORT,
line_length=40,
indent=" ",
)
assert test_output_prefix_from_module_with_comment == (
"from third_party import lib1 # comment\n"
"from third_party import lib2, lib3\n"
"from third_party import lib4, lib5\n"
"from third_party import lib6, lib7\n"
"from third_party import lib8, lib9\n"
"from third_party import lib10, lib11\n"
"from third_party import lib12, lib13\n"
"from third_party import lib14, lib15\n"
"from third_party import lib16, lib17\n"
"from third_party import lib18, lib20\n"
"from third_party import lib21, lib22\n"
)
test_output_hanging_indent_with_parentheses = isort.code(
code=REALLY_LONG_IMPORT,
multi_line_output=WrapModes.HANGING_INDENT_WITH_PARENTHESES,
line_length=40,
indent=" ",
)
assert test_output_hanging_indent_with_parentheses == (
"from third_party import (lib1, lib2,\n"
" lib3, lib4, lib5, lib6, lib7, lib8,\n"
" lib9, lib10, lib11, lib12, lib13,\n"
" lib14, lib15, lib16, lib17, lib18,\n"
" lib20, lib21, lib22)\n"
)
comment_output_hanging_indent_with_parentheses = isort.code(
code=REALLY_LONG_IMPORT_WITH_COMMENT,
multi_line_output=WrapModes.HANGING_INDENT_WITH_PARENTHESES,
line_length=40,
indent=" ",
)
assert comment_output_hanging_indent_with_parentheses == (
"from third_party import (lib1, # comment\n"
" lib2, lib3, lib4, lib5, lib6, lib7,\n"
" lib8, lib9, lib10, lib11, lib12,\n"
" lib13, lib14, lib15, lib16, lib17,\n"
" lib18, lib20, lib21, lib22)\n"
)
test_input = (
"def a():\n"
" from allennlp.modules.text_field_embedders.basic_text_field_embedder"
" import BasicTextFieldEmbedder"
)
test_output = isort.code(test_input, line_length=100)
assert test_output == (
"def a():\n"
" from allennlp.modules.text_field_embedders.basic_text_field_embedder import \\\n"
" BasicTextFieldEmbedder"
)
test_input = (
"class A:\n"
" def a():\n"
" from allennlp.common.registrable import Registrable"
" # import here to avoid circular imports\n"
"\n\n"
"class B:\n"
" def b():\n"
" from allennlp.common.registrable import Registrable"
" # import here to avoid circular imports\n"
)
test_output = isort.code(test_input, line_length=100)
assert test_output == test_input
def test_qa_comment_case() -> None:
test_input = "from veryveryveryveryveryveryveryveryveryveryvery import X # NOQA"
test_output = isort.code(code=test_input, line_length=40, multi_line_output=WrapModes.NOQA)
assert test_output == "from veryveryveryveryveryveryveryveryveryveryvery import X # NOQA\n"
test_input = "import veryveryveryveryveryveryveryveryveryveryvery # NOQA"
test_output = isort.code(code=test_input, line_length=40, multi_line_output=WrapModes.NOQA)
assert test_output == "import veryveryveryveryveryveryveryveryveryveryvery # NOQA\n"
def test_length_sort() -> None:
"""Test setting isort to sort on length instead of alphabetically."""
test_input = (
"import medium_sizeeeeeeeeeeeeee\n"
"import shortie\n"
"import looooooooooooooooooooooooooooooooooooooong\n"
"import medium_sizeeeeeeeeeeeeea\n"
)
test_output = isort.code(test_input, length_sort=True)
assert test_output == (
"import shortie\n"
"import medium_sizeeeeeeeeeeeeea\n"
"import medium_sizeeeeeeeeeeeeee\n"
"import looooooooooooooooooooooooooooooooooooooong\n"
)
def test_length_sort_straight() -> None:
"""Test setting isort to sort straight imports on length instead of alphabetically."""
test_input = (
"import medium_sizeeeeeeeeeeeeee\n"
"import shortie\n"
"import looooooooooooooooooooooooooooooooooooooong\n"
"from medium_sizeeeeeeeeeeeeee import b\n"
"from shortie import c\n"
"from looooooooooooooooooooooooooooooooooooooong import a\n"
)
test_output = isort.code(test_input, length_sort_straight=True)
assert test_output == (
"import shortie\n"
"import medium_sizeeeeeeeeeeeeee\n"
"import looooooooooooooooooooooooooooooooooooooong\n"
"from looooooooooooooooooooooooooooooooooooooong import a\n"
"from medium_sizeeeeeeeeeeeeee import b\n"
"from shortie import c\n"
)
def test_length_sort_section() -> None:
"""Test setting isort to sort on length instead of alphabetically for a specific section."""
test_input = (
"import medium_sizeeeeeeeeeeeeee\n"
"import shortie\n"
"import datetime\n"
"import sys\n"
"import os\n"
"import looooooooooooooooooooooooooooooooooooooong\n"
"import medium_sizeeeeeeeeeeeeea\n"
)
test_output = isort.code(test_input, length_sort_sections=("stdlib",))
assert test_output == (
"import os\n"
"import sys\n"
"import datetime\n"
"\n"
"import looooooooooooooooooooooooooooooooooooooong\n"
"import medium_sizeeeeeeeeeeeeea\n"
"import medium_sizeeeeeeeeeeeeee\n"
"import shortie\n"
)
def test_convert_hanging() -> None:
"""Ensure that isort will convert hanging indents to correct indent
method."""
test_input = (
"from third_party import lib1, lib2, \\\n"
" lib3, lib4, lib5, lib6, lib7, \\\n"
" lib8, lib9, lib10, lib11, lib12, \\\n"
" lib13, lib14, lib15, lib16, lib17, \\\n"
" lib18, lib20, lib21, lib22\n"
)
test_output = isort.code(code=test_input, multi_line_output=WrapModes.GRID, line_length=40)
assert test_output == (
"from third_party import (lib1, lib2,\n"
" lib3, lib4,\n"
" lib5, lib6,\n"
" lib7, lib8,\n"
" lib9, lib10,\n"
" lib11, lib12,\n"
" lib13, lib14,\n"
" lib15, lib16,\n"
" lib17, lib18,\n"
" lib20, lib21,\n"
" lib22)\n"
)
def test_custom_indent() -> None:
"""Ensure setting a custom indent will work as expected."""
test_output = isort.code(
code=REALLY_LONG_IMPORT,
multi_line_output=WrapModes.HANGING_INDENT,
line_length=40,
indent=" ",
balanced_wrapping=False,
)
assert test_output == (
"from third_party import lib1, lib2, \\\n"
" lib3, lib4, lib5, lib6, lib7, lib8, \\\n"
" lib9, lib10, lib11, lib12, lib13, \\\n"
" lib14, lib15, lib16, lib17, lib18, \\\n"
" lib20, lib21, lib22\n"
)
test_output = isort.code(
code=REALLY_LONG_IMPORT,
multi_line_output=WrapModes.HANGING_INDENT,
line_length=40,
indent="' '",
balanced_wrapping=False,
)
assert test_output == (
"from third_party import lib1, lib2, \\\n"
" lib3, lib4, lib5, lib6, lib7, lib8, \\\n"
" lib9, lib10, lib11, lib12, lib13, \\\n"
" lib14, lib15, lib16, lib17, lib18, \\\n"
" lib20, lib21, lib22\n"
)
test_output = isort.code(
code=REALLY_LONG_IMPORT,
multi_line_output=WrapModes.HANGING_INDENT,
line_length=40,
indent="tab",
balanced_wrapping=False,
)
assert test_output == (
"from third_party import lib1, lib2, \\\n"
"\tlib3, lib4, lib5, lib6, lib7, lib8, \\\n"
"\tlib9, lib10, lib11, lib12, lib13, \\\n"
"\tlib14, lib15, lib16, lib17, lib18, \\\n"
"\tlib20, lib21, lib22\n"
)
test_output = isort.code(
code=REALLY_LONG_IMPORT,
multi_line_output=WrapModes.HANGING_INDENT,
line_length=40,
indent=2,
balanced_wrapping=False,
)
assert test_output == (
"from third_party import lib1, lib2, \\\n"
" lib3, lib4, lib5, lib6, lib7, lib8, \\\n"
" lib9, lib10, lib11, lib12, lib13, \\\n"
" lib14, lib15, lib16, lib17, lib18, \\\n"
" lib20, lib21, lib22\n"
)
def test_use_parentheses() -> None:
test_input = (
"from fooooooooooooooooooooooooo.baaaaaaaaaaaaaaaaaaarrrrrrr import "
" my_custom_function as my_special_function"
)
test_output = isort.code(test_input, line_length=79, use_parentheses=True)
assert test_output == (
"from fooooooooooooooooooooooooo.baaaaaaaaaaaaaaaaaaarrrrrrr import (\n"
" my_custom_function as my_special_function)\n"
)
test_output = isort.code(
code=test_input, line_length=79, use_parentheses=True, include_trailing_comma=True
)
assert test_output == (
"from fooooooooooooooooooooooooo.baaaaaaaaaaaaaaaaaaarrrrrrr import (\n"
" my_custom_function as my_special_function,)\n"
)
test_output = isort.code(
code=test_input,
line_length=79,
use_parentheses=True,
multi_line_output=WrapModes.VERTICAL_HANGING_INDENT,
)
assert test_output == (
"from fooooooooooooooooooooooooo.baaaaaaaaaaaaaaaaaaarrrrrrr import (\n"
" my_custom_function as my_special_function\n)\n"
)
test_output = isort.code(
code=test_input,
line_length=79,
use_parentheses=True,
multi_line_output=WrapModes.VERTICAL_GRID_GROUPED,
include_trailing_comma=True,
)
assert test_output == (
"from fooooooooooooooooooooooooo.baaaaaaaaaaaaaaaaaaarrrrrrr import (\n"
" my_custom_function as my_special_function,\n)\n"
)
def test_skip() -> None:
"""Ensure skipping a single import will work as expected."""
test_input = (
"import myproject\n"
"import django\n"
"print('hey')\n"
"import sys # isort: skip this import needs to be placed here\n\n\n\n\n\n\n"
)
test_output = isort.code(test_input, known_first_party=["myproject"])
assert test_output == (
"import django\n"
"\n"
"import myproject\n"
"\n"
"print('hey')\n"
"import sys # isort: skip this import needs to be placed here\n"
)
def test_skip_with_file_name() -> None:
"""Ensure skipping a file works even when file_contents is provided."""
test_input = "import django\nimport myproject\n"
with pytest.raises(FileSkipped):
isort.code(
file_path=Path("/baz.py"), code=test_input, settings_path=os.getcwd(), skip=["baz.py"]
)
def test_skip_within_file() -> None:
"""Ensure skipping a whole file works."""
test_input = "# isort: skip_file\nimport django\nimport myproject\n"
with pytest.raises(FileSkipped):
isort.code(test_input, known_third_party=["django"])
def test_force_to_top() -> None:
"""Ensure forcing a single import to the top of its category works as expected."""
test_input = "import lib6\nimport lib2\nimport lib5\nimport lib1\n"
test_output = isort.code(test_input, force_to_top=["lib5"])
assert test_output == "import lib5\nimport lib1\nimport lib2\nimport lib6\n"
def test_add_imports() -> None:
"""Ensures adding imports works as expected."""
test_input = "import lib6\nimport lib2\nimport lib5\nimport lib1\n\n"
test_output = isort.code(code=test_input, add_imports=["import lib4", "import lib7"])
assert test_output == (
"import lib1\n"
"import lib2\n"
"import lib4\n"
"import lib5\n"
"import lib6\n"
"import lib7\n"
)
# Using simplified syntax
test_input = "import lib6\nimport lib2\nimport lib5\nimport lib1\n\n"
test_output = isort.code(code=test_input, add_imports=["lib4", "lib7", "lib8.a"])
assert test_output == (
"import lib1\n"
"import lib2\n"
"import lib4\n"
"import lib5\n"
"import lib6\n"
"import lib7\n"
"from lib8 import a\n"
)
# On a file that has no pre-existing imports
test_input = '"""Module docstring"""\n' "class MyClass(object):\n pass\n"
test_output = isort.code(code=test_input, add_imports=["from __future__ import print_function"])
assert test_output == (
'"""Module docstring"""\n'
"from __future__ import print_function\n"
"\n"
"\n"
"class MyClass(object):\n"
" pass\n"
)
# On a file that has no pre-existing imports and a multiline docstring
test_input = (
'"""Module docstring\n\nWith a second line\n"""\n' "class MyClass(object):\n pass\n"
)
test_output = isort.code(code=test_input, add_imports=["from __future__ import print_function"])
assert test_output == (
'"""Module docstring\n'
"\n"
"With a second line\n"
'"""\n'
"from __future__ import print_function\n"
"\n"
"\n"
"class MyClass(object):\n"
" pass\n"
)
# On a file that has no pre-existing imports and a multiline docstring.
# In this example, the closing quotes for the docstring are on the final
# line rather than a separate one.
test_input = (
'"""Module docstring\n\nWith a second line"""\n' "class MyClass(object):\n pass\n"
)
test_output = isort.code(code=test_input, add_imports=["from __future__ import print_function"])
assert test_output == (
'"""Module docstring\n'
"\n"
'With a second line"""\n'
"from __future__ import print_function\n"
"\n"
"\n"
"class MyClass(object):\n"
" pass\n"
)
# On a file that has no pre-existing imports, and no doc-string
test_input = "class MyClass(object):\n pass\n"
test_output = isort.code(code=test_input, add_imports=["from __future__ import print_function"])
assert test_output == (
"from __future__ import print_function\n" "\n" "\n" "class MyClass(object):\n" " pass\n"
)
# On a file with no content what so ever
test_input = ""
test_output = isort.code(test_input, add_imports=["lib4"])
assert test_output == ("")
# On a file with no content what so ever, after force_adds is set to True
test_input = ""
test_output = isort.code(code=test_input, add_imports=["lib4"], force_adds=True)
assert test_output == ("import lib4\n")
def test_remove_imports() -> None:
"""Ensures removing imports works as expected."""
test_input = "import lib6\nimport lib2\nimport lib5\nimport lib1"
test_output = isort.code(test_input, remove_imports=["lib2", "lib6"])
assert test_output == "import lib1\nimport lib5\n"
# Using natural syntax
test_input = (
"import lib6\n" "import lib2\n" "import lib5\n" "import lib1\n" "from lib8 import a"
)
test_output = isort.code(
code=test_input, remove_imports=["import lib2", "import lib6", "from lib8 import a"]
)
assert test_output == "import lib1\nimport lib5\n"
# From imports
test_input = "from x import y"
test_output = isort.code(test_input, remove_imports=["x"])
assert test_output == ""
test_input = "from x import y"
test_output = isort.code(test_input, remove_imports=["x.y"])
assert test_output == ""
def test_comments_above():
"""Test to ensure comments above an import will stay in place"""
test_input = "import os\n\nfrom x import y\n\n# comment\nfrom z import __version__, api\n"
assert isort.code(test_input, ensure_newline_before_comments=True) == test_input
def test_explicitly_local_import() -> None:
"""Ensure that explicitly local imports are separated."""
test_input = "import lib1\nimport lib2\nimport .lib6\nfrom . import lib7"
assert isort.code(test_input) == (
"import lib1\nimport lib2\n\nimport .lib6\nfrom . import lib7\n"
)
assert isort.code(test_input, old_finders=True) == (
"import lib1\nimport lib2\n\nimport .lib6\nfrom . import lib7\n"
)
def test_quotes_in_file() -> None:
"""Ensure imports within triple quotes don't get imported."""
test_input = "import os\n\n" '"""\n' "Let us\nimport foo\nokay?\n" '"""\n'
assert isort.code(test_input) == test_input
test_input = "import os\n\n" '\'"""\'\n' "import foo\n"
assert isort.code(test_input) == test_input
test_input = "import os\n\n" '"""Let us"""\n' "import foo\n\n" '"""okay?"""\n'
assert isort.code(test_input) == test_input
test_input = "import os\n\n" '#"""\n' "import foo\n" '#"""'
assert isort.code(test_input) == ('import os\n\nimport foo\n\n#"""\n#"""\n')
test_input = "import os\n\n'\\\nimport foo'\n"
assert isort.code(test_input) == test_input
test_input = "import os\n\n'''\n\\'''\nimport junk\n'''\n"
assert isort.code(test_input) == test_input