Skip to content

Keep closing split directives with parenthesized imports - #2682

Open
hansu650 wants to merge 8 commits into
PyCQA:mainfrom
hansu650:issue/2680
Open

hansu650 wants to merge 8 commits into
PyCQA:mainfrom
hansu650:issue/2680

Conversation

@hansu650

@hansu650 hansu650 commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2680.

With float_to_top=True, a parenthesized import ending in ) # isort: split can leave a stray closing parenthesis in the output. Keep that closing line in the section being parsed, retain the marked import through the existing continuation collector, and use it as a boundary during normal processing. The marked import stays intact, and formatting the output again is stable.

Following @DanielNoord's request, this revision removes the added general continuation reader and float-to-top iterator. The production diff against f5b5649f is now 31 changed lines across core.py and parse.py (previously 106). Upstream's ordinary continuation and quote handling is retained. The existing float-to-top state updates are simplified without adding a new parser or helper. This also removes the two new semicolon/backslash and multiline-string regressions reported in review.

The scope is the original closing-parenthesis-line case. The previous broader handling of split directives at arbitrary positions inside imports is no longer proposed; those upstream edge cases remain outside this fix.

Validation on Windows / CPython 3.13.15 in the dedicated task environment:

  • Nineteen focused regression/control cases cover closing-line splits, surrounding imports/code, nesting, LF/CRLF, both modes and the reported review examples. 19 passed from source and the isolated installed wheel.
  • On unchanged upstream: 13 failed, 6 passed. On the preceding PR source: 4 failed, 15 passed, reproducing both newly reported regressions in both modes.
  • Complete unit suite: 793 passed, 6 existing skips. Existing local property/integration selection: 3 passed.
  • Overall statement/branch coverage: 99.41%; core.py and parse.py are 100%, including branches.
  • Configured import sorting (including examples), Ruff check/format, Flake8, Bandit, Linux-targeted mypy (85 files) and dependency checks pass.
  • Clean-export sdist/wheel builds and Twine pass. The export uses the explicit local validation version 0.0.0.dev0 because it lacks Git metadata; installed core/parse hashes match that export.

Compiled wheels, downstream repository clones, native Windows mypy and other Python/OS runs were not repeated locally. Updated remote CI is tracked separately.

AI assistance: revised and validated with OpenAI Codex.

Copilot AI previously approved these changes Sep 27, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approved

The implementation addresses both affected processing paths with comprehensive regression coverage and no unresolved issues found.

Review effort: Balanced
Findings: None

What changed in this PR

Fixes multiline isort: split handling so complete import statements remain intact and stable.

Changes:

  • Reads complete imports before applying split boundaries.
  • Preserves split-marked continuation lines verbatim.
  • Adds regression coverage for multiline, nested, CRLF, and float-to-top cases.
File Description
isort/​core.py Handles complete import statements during sorting and float-to-top preprocessing.
isort/​parse.py Preserves imports containing split directives on continuation lines.
tests/​unit/​test_regressions.py Adds comprehensive regressions for issue #2680.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@hansu650

Copy link
Copy Markdown
Contributor Author

Synced with main 43714728 in 147a248a and resolved the test conflict, preserving both the split-directive regressions and upstream's new type annotations. All 34 #2680/#2681 regression cases and all 3 local property/integration cases pass together.

Fresh full-unit result: 683 passed, 5 existing skips, and the same 3 Windows/environment failures independently reproduced on the unmodified current base. Coverage is 99.24%, with core/parse at 100% including branches. Ruff, formatting, flake8, Bandit, Linux-targeted mypy and clean-export package checks passed. The updated PR body lists the exact limitations; no cross-platform CI success is claimed.

Assisted by OpenAI Codex.

@codecov

codecov Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.40%. Comparing base (f5b5649) to head (f89456c).
⚠️ Report is 11 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2682   +/-   ##
=======================================
  Coverage   99.39%   99.40%           
=======================================
  Files          40       40           
  Lines        3317     3372   +55     
  Branches      716      726   +10     
=======================================
+ Hits         3297     3352   +55     
  Misses         12       12           
  Partials        8        8           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DanielNoord
DanielNoord requested a balanced review from Copilot November 15, 2026 14:17
Copilot AI dismissed their stale review, a newer Copilot review was requested November 15, 2026 14:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Continuation detection incorrectly treats parentheses in trailing string literals after semicolons as import syntax, causing valid Python to raise ExistingSyntaxErrors.

1 open issue

Review effort: Balanced

Comment thread isort/core.py Resolved

@DanielNoord DanielNoord left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please have a look at the AI review.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved continuation and quote-state defects can turn valid Python into invalid output or incorrectly raise ExistingSyntaxErrors.

2 open issues
1 resolved since last review

Review effort: Balanced

Comment thread isort/core.py Outdated
Comment on lines +81 to +82
if not part.lstrip().startswith(IMPORT_START_IDENTIFIERS):
break

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the added continuation reader as part of the smaller revision requested by Daniel. The existing upstream reader again keeps semicolon/backslash continuations together. Added the exact example as a regression in both modes: both failed on 5a2c9da, and both now produce valid, idempotent Python. The focused selection passes all 19 cases from source and the installed wheel.

Comment thread isort/core.py Outdated
in_quote = _scan_quotes(line, stripped_line, in_quote)
is_code = not (in_quote or was_in_quote)
if is_code and stripped_line.startswith(IMPORT_START_IDENTIFIERS):
line, _ = _read_import_statement(line, input_stream)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the added reader/iterator and restored upstream's ordinary continuation and quote-state handling. The exact multiline-string example is now covered with and without float_to_top; both failed on 5a2c9da and now pass without changing the string contents. The fix is scoped to the original closing-parenthesis split case, with all 19 focused regressions/controls passing from source and the installed wheel.

@DanielNoord

Copy link
Copy Markdown
Member

Similar to #2728 I am afraid this is getting a bit out of hand for the relative edge case we are fixing here. Isn't there a more minimal approach we can take here? Perhaps by not fixing all issues but at least making behaviour stable.

@hansu650 hansu650 changed the title Fix split directives on complete multiline imports Keep closing split directives with parenthesized imports Dec 3, 2026
@hansu650

hansu650 commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Agreed. I reduced this to the original #2680 closing-parenthesis-line case. The general continuation reader and float-to-top iterator are removed; the production diff against f5b5649 is now 31 changed lines (previously 106), using the existing parser. The existing float-to-top state updates are also simplified to satisfy the complexity check without adding a new parser or helper. The previous broader guarantees for split directives at arbitrary positions inside imports are no longer proposed.

The closing line stays with the section being parsed, the marked import is retained, and the normal processing pass treats it as a boundary. Both new review examples now produce valid, idempotent Python in both modes; they accounted for four failures on the preceding PR head.

Validation: 19 focused cases pass from source and the verified installed wheel; 793 unit tests and 3 local property/integration tests pass, with 6 existing skips. Core and parse retain 100% statement/branch coverage, and configured lint/type/package checks pass. The title and description now reflect the smaller scope.

This branch has not been deployed

No deployments
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.

float_to_top leaves a stray ')' when a paren import ends with isort: split

3 participants