Skip to content

Fix --sort-reexports crash when __all__ is immediately followed by another assignment#2473

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-isort-parsing-error
Draft

Fix --sort-reexports crash when __all__ is immediately followed by another assignment#2473
Copilot wants to merge 2 commits intomainfrom
copilot/fix-isort-parsing-error

Conversation

Copy link
Contributor

Copilot AI commented Mar 10, 2026

When sort_reexports=True, a multi-line __all__ followed immediately by another assignment (e.g. __version__) without a blank line caused a ValueError: too many values to unpack because the collector kept consuming lines past the end of the __all__ block.

__all__ = [
    "FooType",
    "BarType",
]
__version__ = importlib.metadata.version("my-package")  # was incorrectly absorbed into __all__ section

Changes

  • isort/core.py: Added _net_open_brackets(line) helper that counts net open brackets while correctly ignoring brackets inside string literals and comments. Tracks reexport_bracket_depth across collected lines; when depth reaches 0 the section is processed immediately rather than waiting for a blank line or EOF. Also fixed split("=", 1) in __all__ detection to handle values containing =.
  • isort/literal.py: Defensive fix — code.split("=", 1) instead of code.split("=").
  • tests/unit/test_isort.py: Added tests for both multi-line and single-line __all__ immediately followed by another assignment with no blank line.
Original prompt

This section details on the original issue you should resolve

<issue_title>--sort-reexports results in isort not being able to parse file</issue_title>
<issue_description>Python version 3.12
isort version: 5.13.2

Test file test.py:

#!/usr/bin/env python
import importlib.metadata

__all__ = [
    "FooType",
    "BarType",
    "some_method",
]
__version__ = importlib.metadata.version("my-package")

Calling isort --srx test.py results in:

isort/main.py:105: UserWarning: Unable to parse file test.py due to too many values to unpack (expected 2)

The exception is actually triggered here - somehow, both assignments to __all__ and __version__ end up in code.

It works if an empty line between both assignments is introduced (or any other code, for that matter).</issue_description>

Comments on the Issue (you are @copilot in this section)

@DanielNoord Traceback is as follows: ``` Traceback (most recent call last): File "/home/daniel-vannoord/Documents/github/isort/.venv/bin/isort", line 10, in sys.exit(main()) ~~~~^^ File "/home/daniel-vannoord/Documents/github/isort/isort/main.py", line 1236, in main for sort_attempt in attempt_iterator: ^^^^^^^^^^^^^^^^ File "/home/daniel-vannoord/Documents/github/isort/isort/main.py", line 1220, in sort_imports( # type: ignore ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^ file_name, ^^^^^^^^^^ ...<6 lines>... config_trie=config_trie, ^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "/home/daniel-vannoord/Documents/github/isort/isort/main.py", line 96, in sort_imports incorrectly_sorted = not api.sort_file( ~~~~~~~~~~~~~^ file_name, ^^^^^^^^^^ ...<3 lines>... **kwargs, ^^^^^^^^^ ) ^ File "/home/daniel-vannoord/Documents/github/isort/isort/api.py", line 435, in sort_file changed = sort_stream( input_stream=source_file.stream, ...<4 lines>... extension=extension, ) File "/home/daniel-vannoord/Documents/github/isort/isort/api.py", line 212, in sort_stream changed = core.process( input_stream, ...<3 lines>... raise_on_skip=raise_on_skip, ) File "/home/daniel-vannoord/Documents/github/isort/isort/core.py", line 142, in process isort.literal.assignment( ~~~~~~~~~~~~~~~~~~~~~~~~^ code_sorting_section, ^^^^^^^^^^^^^^^^^^^^^ ...<2 lines>... config=_indented_config(config, indent), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ), ^ File "/home/daniel-vannoord/Documents/github/isort/isort/literal.py", line 51, in assignment variable_name, literal = code.split("=") ^^^^^^^^^^^^^^^^^^^^^^ ValueError: too many values to unpack (expected 2, got 3) ```

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…assignment without blank line

Co-authored-by: DanielNoord <13665637+DanielNoord@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix isort parsing issue with __all__ and __version__ Fix --sort-reexports crash when __all__ is immediately followed by another assignment Mar 10, 2026
@codecov
Copy link

codecov bot commented Mar 11, 2026

Codecov Report

❌ Patch coverage is 83.33333% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.03%. Comparing base (ad2dfe9) to head (acfc59d).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2473      +/-   ##
==========================================
- Coverage   99.25%   99.03%   -0.22%     
==========================================
  Files          40       40              
  Lines        3079     3117      +38     
  Branches      671      684      +13     
==========================================
+ Hits         3056     3087      +31     
- Misses         13       17       +4     
- Partials       10       13       +3     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--sort-reexports results in isort not being able to parse file

2 participants