Skip to content

Add optional wrapping for long from-import module paths - #2803

Open
wozhenbang2004 wants to merge 4 commits into
PyCQA:mainfrom
wozhenbang2004:issue/1531-wrap-long-from-imports
Open

wozhenbang2004 wants to merge 4 commits into
PyCQA:mainfrom
wozhenbang2004:issue/1531-wrap-long-from-imports

Conversation

@wozhenbang2004

Copy link
Copy Markdown

Closes #1531.

When the module path itself is long, wrapping the imported names still leaves an overlong from line. The example in #1531 produces a 134-character line with line_length=120.

Add the opt-in wrap_long_from_imports setting and --wrap-long-from-imports CLI flag. They split the module path after dots using backslashes, while keeping identifiers and leading relative-import dots together. The setting defaults to false. Existing wrapping modes still format the imported names, and NOQA mode keeps its existing behavior.

The module-path pass runs after comments are placed, so it does not append comments after continuation backslashes. Small parser changes retain star-import comments on repeated runs and normalize CRLF module continuations correctly. Documentation covers the option and the limit for individual long identifiers.

Validation on Python 3.12:

  • Added 38 cases covering the reported example, all wrapping modes, aliases and comments, star imports, relative and indented imports, CRLF, configuration files, and the CLI. Python cases check AST equivalence and repeated-run stability.
  • ./scripts/test.sh: 816 passed, 2 skipped; 99% total coverage, 100% for isort/wrap.py.
  • uv run tox -e lint: passed, including mypy, isort, Flake8, Ruff, and Bandit.

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.

Long absolute imports not getting wrapped

1 participant