in usort/sorting.py [0:0]
def split_inplace(self, block: SortableBlock, overlap: Set[str]) -> SortableBlock:
"""
Split an existing block into two blocks after the last shadowed import.
Pre-sorts the block of imports, then finds the last import with shadowed names, and
splits after that import. Returns a new block containing all imports after the split
point, or empty otherwise.
"""
# best-effort pre-sorting before we split
for imp in block.imports:
imp.items.sort()
block.imports.sort()
# find index of last shadowed import, starting from the end of the block's imports
idx = len(block.imports)
while idx > 0:
idx -= 1
imp = block.imports[idx]
if any(item.fullname in overlap for item in imp.items):
break
count = idx + 1
if count >= len(block.imports):
# shadowed import is the last import in the block, so we can't split anything.
# return a new, empty block following pattern from sortable_blocks()
new = SortableBlock(block.end_idx, block.end_idx + 1)
else:
# Split the existing block after the shadowed import, creating a new block that
# starts after the shadowed import, update the old block's end index, and then
# move all the imports after that to the new block
new = SortableBlock(block.start_idx + count, block.end_idx)
block.end_idx = block.start_idx + count
new.imports = block.imports[count:]
block.imports[count:] = []
# move imported names metadata
for imp in new.imports:
for key in list(imp.imported_names):
new.imported_names[key] = block.imported_names.pop(key)
return new