1- import isort
21from isort import Config , parse
32
43from .utils import isort_test
@@ -95,19 +94,6 @@ def test_lazy_imports_appear_after_eager_in_each_section_independently():
9594 isort_test (unsorted , expected , known_third_party = ["requests" ])
9695
9796
98- def test_only_lazy_imports_in_section ():
99- """A section that contains *only* lazy imports is formatted correctly."""
100- isort_test ("lazy import ast\n lazy import os\n " , "lazy import ast\n lazy import os\n " )
101-
102-
103- def test_only_lazy_from_imports ():
104- """A section with only lazy from-imports is formatted correctly."""
105- isort_test (
106- "lazy from dataclasses import dataclass\n lazy from pathlib import Path\n " ,
107- "lazy from dataclasses import dataclass\n lazy from pathlib import Path\n " ,
108- )
109-
110-
11197def test_lazy_import_with_alias ():
11298 """``lazy import X as Y`` is supported and sorted correctly."""
11399 isort_test (
@@ -122,47 +108,6 @@ def test_lazy_from_import_multiple_names():
122108 isort_test ("lazy from typing import List, Dict\n " , "lazy from typing import Dict, List\n " )
123109
124110
125- def test_idempotency_mixed_eager_and_lazy ():
126- """Running isort twice on sorted output must not change anything."""
127- isort_test (
128- "lazy from dataclasses import dataclass\n import json\n lazy import ast\n import os\n " ,
129- "import json\n import os\n lazy import ast\n lazy from dataclasses import dataclass\n " ,
130- )
131-
132-
133- def test_idempotency_only_lazy ():
134- """Idempotency with a file that contains only lazy imports."""
135- isort_test ("lazy import shutil\n lazy import ast\n " , "lazy import ast\n lazy import shutil\n " )
136-
137-
138- def test_check_code_sorted ():
139- """check_code returns True when lazy imports are already sorted."""
140- assert isort .check_code (
141- "import os\n lazy import ast\n " ,
142- show_diff = False ,
143- )
144-
145-
146- def test_check_code_unsorted ():
147- """check_code returns False when lazy imports are not sorted."""
148- assert not isort .check_code (
149- "lazy import ast\n import os\n " ,
150- show_diff = False ,
151- )
152-
153-
154- def test_lazy_and_eager_from_same_module_in_section ():
155- """Both eager and lazy imports from the same module are retained."""
156- # This is an unusual pattern but should not cause errors.
157- result = isort .code ("from pathlib import Path\n lazy from pathlib import PurePath\n " )
158- assert "from pathlib import Path" in result
159- assert "lazy from pathlib import PurePath" in result
160- # eager must come before lazy
161- assert result .index ("from pathlib import Path" ) < result .index (
162- "lazy from pathlib import PurePath"
163- )
164-
165-
166111def test_no_sections_mode_with_lazy_imports ():
167112 """lazy imports are supported in no_sections mode."""
168113 isort_test (
@@ -173,23 +118,21 @@ def test_no_sections_mode_with_lazy_imports():
173118
174119
175120def test_from_first_option_respected_for_lazy ():
176- """When from_first=True, lazy from imports precede lazy straight imports."""
121+ """When from_first=True, lazy from imports precede lazy straight imports but appear before eager imports ."""
177122 isort_test (
178- "lazy import ast\n lazy from dataclasses import dataclass\n " ,
179- "lazy from dataclasses import dataclass\n lazy import ast\n " ,
123+ "import pathlib \n lazy import ast\n lazy from dataclasses import dataclass\n " ,
124+ "import pathlib \n lazy from dataclasses import dataclass\n lazy import ast\n " ,
180125 from_first = True ,
181126 )
182127
183128
184129def test_force_sort_within_sections_applies_to_lazy ():
185- """With force_sort_within_sections=True, lazy straight and from imports are interleaved
186- and sorted together by module name instead of straight-before-from."""
187- # Without force_sort_within_sections the default is straight-before-from:
188- # lazy import zlib
189- # lazy from ast import parse
190- # With force_sort_within_sections they are sorted together by module name:
191- # lazy from ast import parse (key: "lazy from ast ...")
192- # lazy import zlib (key: "lazy import zlib")
130+ """force_sort_within_sections toggles lazy import ordering behavior."""
131+ isort_test (
132+ "lazy import zlib\n lazy from ast import parse\n " ,
133+ "lazy import zlib\n lazy from ast import parse\n " ,
134+ force_sort_within_sections = False ,
135+ )
193136 isort_test (
194137 "lazy import zlib\n lazy from ast import parse\n " ,
195138 "lazy from ast import parse\n lazy import zlib\n " ,
0 commit comments