| 1 |
from isort import Config, parse |
| 2 |
|
| 3 |
from .utils import isort_test |
| 4 |
|
| 5 |
|
| 6 |
class TestParsing: |
| 7 |
"""Verify that ``parse.file_contents`` correctly identifies lazy imports.""" |
| 8 |
|
| 9 |
def test_lazy_straight_import_is_stored_in_lazy_straight_bucket(self) -> None: |
| 10 |
result = parse.file_contents("lazy import ast\n", Config()) |
| 11 |
assert "ast" in result.imports["STDLIB"]["lazy_straight"] |
| 12 |
assert "ast" not in result.imports["STDLIB"]["straight"] |
| 13 |
|
| 14 |
def test_lazy_from_import_is_stored_in_lazy_from_bucket(self) -> None: |
| 15 |
result = parse.file_contents("lazy from dataclasses import dataclass\n", Config()) |
| 16 |
assert "dataclasses" in result.imports["STDLIB"]["lazy_from"] |
| 17 |
assert "dataclasses" not in result.imports["STDLIB"]["from"] |
| 18 |
|
| 19 |
def test_eager_imports_are_stored_in_regular_buckets(self) -> None: |
| 20 |
result = parse.file_contents("import os\nfrom pathlib import Path\n", Config()) |
| 21 |
assert "os" in result.imports["STDLIB"]["straight"] |
| 22 |
assert "pathlib" in result.imports["STDLIB"]["from"] |
| 23 |
|
| 24 |
def test_lazy_imports_are_placed_in_correct_section(self) -> None: |
| 25 |
"""Lazy imports must be placed in the same section as their eager counterparts.""" |
| 26 |
result = parse.file_contents( |
| 27 |
"lazy import ast\nlazy import requests\n", |
| 28 |
Config(known_third_party=["requests"]), |
| 29 |
) |
| 30 |
assert "ast" in result.imports["STDLIB"]["lazy_straight"] |
| 31 |
assert "requests" in result.imports["THIRDPARTY"]["lazy_straight"] |
| 32 |
|
| 33 |
|
| 34 |
def test_lazy_straight_imports_come_after_eager() -> None: |
| 35 |
"""lazy import lines follow all eager import lines within the section.""" |
| 36 |
isort_test("lazy import ast\nimport os\n", "import os\nlazy import ast\n") |
| 37 |
|
| 38 |
|
| 39 |
def test_lazy_from_imports_come_after_eager() -> None: |
| 40 |
"""lazy from ... import lines follow all eager import lines within the section.""" |
| 41 |
isort_test( |
| 42 |
"lazy from pathlib import Path\nfrom collections import defaultdict\n", |
| 43 |
"from collections import defaultdict\nlazy from pathlib import Path\n", |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
def test_lazy_straight_sorted_alphabetically() -> None: |
| 48 |
"""Multiple lazy straight imports are sorted alphabetically.""" |
| 49 |
isort_test("lazy import shutil\nlazy import ast\n", "lazy import ast\nlazy import shutil\n") |
| 50 |
|
| 51 |
|
| 52 |
def test_lazy_from_sorted_alphabetically() -> None: |
| 53 |
"""Multiple lazy from imports are sorted alphabetically by module name.""" |
| 54 |
isort_test( |
| 55 |
"lazy from pathlib import Path\nlazy from dataclasses import dataclass\n", |
| 56 |
"lazy from dataclasses import dataclass\nlazy from pathlib import Path\n", |
| 57 |
) |
| 58 |
|
| 59 |
|
| 60 |
def test_ruff_reference_example() -> None: |
| 61 |
"""Reproduce the canonical example from the ruff issue tracker. |
| 62 |
|
| 63 |
See https://github.com/astral-sh/ruff/issues/21305. |
| 64 |
""" |
| 65 |
unsorted = ( |
| 66 |
"lazy from dataclasses import dataclass\n" |
| 67 |
"import json\n" |
| 68 |
"import os\n" |
| 69 |
"import subprocess\n" |
| 70 |
"from collections import defaultdict\n" |
| 71 |
"from pathlib import Path\n" |
| 72 |
"from typing import Final\n" |
| 73 |
"lazy import ast\n" |
| 74 |
"lazy import shutil\n" |
| 75 |
) |
| 76 |
expected = ( |
| 77 |
"import json\n" |
| 78 |
"import os\n" |
| 79 |
"import subprocess\n" |
| 80 |
"from collections import defaultdict\n" |
| 81 |
"from pathlib import Path\n" |
| 82 |
"from typing import Final\n" |
| 83 |
"lazy import ast\n" |
| 84 |
"lazy import shutil\n" |
| 85 |
"lazy from dataclasses import dataclass\n" |
| 86 |
) |
| 87 |
isort_test(unsorted, expected) |
| 88 |
|
| 89 |
|
| 90 |
def test_lazy_imports_appear_after_eager_in_each_section_independently() -> None: |
| 91 |
"""Each section gets its own eager-first / lazy-last grouping.""" |
| 92 |
unsorted = "lazy import requests\nlazy import ast\nimport os\nimport requests\n" |
| 93 |
expected = "import os\nlazy import ast\n\nimport requests\nlazy import requests\n" |
| 94 |
isort_test(unsorted, expected, known_third_party=["requests"]) |
| 95 |
|
| 96 |
|
| 97 |
def test_lazy_import_with_alias() -> None: |
| 98 |
"""``lazy import X as Y`` is supported and sorted correctly.""" |
| 99 |
isort_test( |
| 100 |
"import os\nlazy import numpy as np\n", |
| 101 |
"import os\n\nlazy import numpy as np\n", |
| 102 |
known_third_party=["numpy"], |
| 103 |
) |
| 104 |
|
| 105 |
|
| 106 |
def test_lazy_from_import_multiple_names() -> None: |
| 107 |
"""``lazy from X import a, b`` is supported and names are sorted alphabetically.""" |
| 108 |
isort_test("lazy from typing import List, Dict\n", "lazy from typing import Dict, List\n") |
| 109 |
|
| 110 |
|
| 111 |
def test_no_sections_mode_with_lazy_imports() -> None: |
| 112 |
"""lazy imports are supported in no_sections mode.""" |
| 113 |
isort_test( |
| 114 |
"lazy import ast\nimport os\n", |
| 115 |
"import os\nlazy import ast\n", |
| 116 |
no_sections=True, |
| 117 |
) |
| 118 |
|
| 119 |
|
| 120 |
def test_from_first_option_respected_for_lazy() -> None: |
| 121 |
"""When from_first=True, lazy from imports precede lazy straight but appear after eager.""" |
| 122 |
isort_test( |
| 123 |
"import pathlib\nlazy import ast\nlazy from dataclasses import dataclass\n", |
| 124 |
"import pathlib\nlazy from dataclasses import dataclass\nlazy import ast\n", |
| 125 |
from_first=True, |
| 126 |
) |
| 127 |
|
| 128 |
|
| 129 |
def test_force_sort_within_sections_applies_to_lazy() -> None: |
| 130 |
"""force_sort_within_sections toggles lazy import ordering behavior.""" |
| 131 |
isort_test( |
| 132 |
"lazy import zlib\nlazy from ast import parse\n", |
| 133 |
"lazy import zlib\nlazy from ast import parse\n", |
| 134 |
force_sort_within_sections=False, |
| 135 |
) |
| 136 |
isort_test( |
| 137 |
"lazy import zlib\nlazy from ast import parse\n", |
| 138 |
"lazy from ast import parse\nlazy import zlib\n", |
| 139 |
force_sort_within_sections=True, |
| 140 |
) |