Sort the imports defined in the `_import_structure` of a given init. Args: file (`str`): The path to the init to check/fix. check_only (`bool`, *optional*, defaults to `True`): Whether or not to just check (and not auto-fix) the init.
(file: str, check_only: bool = True)
| 233 | |
| 234 | |
| 235 | def sort_imports(file: str, check_only: bool = True): |
| 236 | """ |
| 237 | Sort the imports defined in the `_import_structure` of a given init. |
| 238 | |
| 239 | Args: |
| 240 | file (`str`): The path to the init to check/fix. |
| 241 | check_only (`bool`, *optional*, defaults to `True`): Whether or not to just check (and not auto-fix) the init. |
| 242 | """ |
| 243 | with open(file, encoding="utf-8") as f: |
| 244 | code = f.read() |
| 245 | |
| 246 | # If the file is not a custom init, there is nothing to do. |
| 247 | if "_import_structure" not in code: |
| 248 | return |
| 249 | |
| 250 | # Blocks of indent level 0 |
| 251 | main_blocks = split_code_in_indented_blocks( |
| 252 | code, start_prompt="_import_structure = {", end_prompt="if TYPE_CHECKING:" |
| 253 | ) |
| 254 | |
| 255 | # We ignore block 0 (everything untils start_prompt) and the last block (everything after end_prompt). |
| 256 | for block_idx in range(1, len(main_blocks) - 1): |
| 257 | # Check if the block contains some `_import_structure`s thingy to sort. |
| 258 | block = main_blocks[block_idx] |
| 259 | block_lines = block.split("\n") |
| 260 | |
| 261 | # Get to the start of the imports. |
| 262 | line_idx = 0 |
| 263 | while line_idx < len(block_lines) and "_import_structure" not in block_lines[line_idx]: |
| 264 | # Skip dummy import blocks |
| 265 | if "import dummy" in block_lines[line_idx]: |
| 266 | line_idx = len(block_lines) |
| 267 | else: |
| 268 | line_idx += 1 |
| 269 | if line_idx >= len(block_lines): |
| 270 | continue |
| 271 | |
| 272 | # Ignore beginning and last line: they don't contain anything. |
| 273 | internal_block_code = "\n".join(block_lines[line_idx:-1]) |
| 274 | indent = get_indent(block_lines[1]) |
| 275 | # Slit the internal block into blocks of indent level 1. |
| 276 | internal_blocks = split_code_in_indented_blocks(internal_block_code, indent_level=indent) |
| 277 | # We have two categories of import key: list or _import_structure[key].append/extend |
| 278 | pattern = _re_direct_key if "_import_structure = {" in block_lines[0] else _re_indirect_key |
| 279 | # Grab the keys, but there is a trap: some lines are empty or just comments. |
| 280 | keys = [(pattern.search(b).groups()[0] if pattern.search(b) is not None else None) for b in internal_blocks] |
| 281 | # We only sort the lines with a key. |
| 282 | keys_to_sort = [(i, key) for i, key in enumerate(keys) if key is not None] |
| 283 | sorted_indices = [x[0] for x in sorted(keys_to_sort, key=lambda x: x[1])] |
| 284 | |
| 285 | # We reorder the blocks by leaving empty lines/comments as they were and reorder the rest. |
| 286 | count = 0 |
| 287 | reordered_blocks = [] |
| 288 | for i in range(len(internal_blocks)): |
| 289 | if keys[i] is None: |
| 290 | reordered_blocks.append(internal_blocks[i]) |
| 291 | else: |
| 292 | block = sort_objects_in_import(internal_blocks[sorted_indices[count]]) |
no test coverage detected