Fix init import_structure sorting (#20477)

* Fix init import_structure sorting

* Fix rebase
This commit is contained in:
Sylvain Gugger
2022-11-29 09:46:10 -05:00
committed by GitHub
parent 3b91f96fc9
commit bbcd5eea3b
3 changed files with 176 additions and 167 deletions

View File

@@ -200,9 +200,9 @@ def sort_imports(file, check_only=True):
indent = get_indent(block_lines[1])
# Slit the internal block into blocks of indent level 1.
internal_blocks = split_code_in_indented_blocks(internal_block_code, indent_level=indent)
# We have two categories of import key: list or _import_structu[key].append/extend
pattern = _re_direct_key if "_import_structure" in block_lines[0] else _re_indirect_key
# Grab the keys, but there is a trap: some lines are empty or jsut comments.
# We have two categories of import key: list or _import_structure[key].append/extend
pattern = _re_direct_key if "_import_structure = {" in block_lines[0] else _re_indirect_key
# Grab the keys, but there is a trap: some lines are empty or just comments.
keys = [(pattern.search(b).groups()[0] if pattern.search(b) is not None else None) for b in internal_blocks]
# We only sort the lines with a key.
keys_to_sort = [(i, key) for i, key in enumerate(keys) if key is not None]