Skip to content

Commit

Permalink
Fix bugs in re-export sorter (#2037)
Browse files Browse the repository at this point in the history
Closes #2037.
  • Loading branch information
parafoxia committed Jan 1, 2023
1 parent b20228c commit afdc4f2
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions isort/core.py
Expand Up @@ -78,8 +78,8 @@ def process(
end_of_file: bool = False
verbose_output: List[str] = []
lines_before: List[str] = []
auto_reexporting: bool = False
line_index: int = 0
is_reexport: bool = False
sort_section_size: int = 0

if config.float_to_top:
new_input = ""
Expand Down Expand Up @@ -137,6 +137,8 @@ def process(
line_separator = "\n"

if code_sorting and code_sorting_section:
if is_reexport:
output_stream.seek(sort_section_size, 0)
sorted_code = textwrap.indent(
isort.literal.assignment(
code_sorting_section,
Expand All @@ -152,7 +154,7 @@ def process(
line_separator=line_separator,
ignore_whitespace=config.ignore_whitespace,
)
line_index += output_stream.write(sorted_code)
sort_section_size += output_stream.write(sorted_code)
else:
stripped_line = line.strip()
if stripped_line and not line_separator:
Expand Down Expand Up @@ -233,12 +235,13 @@ def process(
code_sorting_indent = line[: -len(line.lstrip())]
not_imports = True
elif config.sort_reexports and stripped_line.startswith("__all__"):
code_sorting = LITERAL_TYPE_MAPPING[stripped_line.split(" = ")[1][0]]
_, rhs = stripped_line.split("=")
code_sorting = LITERAL_TYPE_MAPPING.get(rhs.rstrip()[0], "tuple")
code_sorting_indent = line[: -len(line.lstrip())]
not_imports = True
code_sorting_section += line
auto_reexporting = True
line_index -= len(line) - 1
is_reexport = True
sort_section_size -= len(line)
elif code_sorting:
if not stripped_line:
sorted_code = textwrap.indent(
Expand All @@ -256,14 +259,14 @@ def process(
line_separator=line_separator,
ignore_whitespace=config.ignore_whitespace,
)
if auto_reexporting:
output_stream.seek(line_index, 0)
line_index += output_stream.write(sorted_code)
if is_reexport:
output_stream.seek(sort_section_size, 0)
sort_section_size += output_stream.write(sorted_code)
not_imports = True
code_sorting = False
code_sorting_section = ""
code_sorting_indent = ""
auto_reexporting = False
is_reexport = False
else:
code_sorting_section += line
line = ""
Expand Down Expand Up @@ -357,7 +360,7 @@ def process(
else:
not_imports = True

line_index += len(line)
sort_section_size += len(line)

if not_imports:

Expand Down

0 comments on commit afdc4f2

Please sign in to comment.