Skip to content

Add an option to sort absolute imports before relative imports - #2804

Open
wozhenbang2004 wants to merge 6 commits into
PyCQA:mainfrom
wozhenbang2004:feat-absolute-imports-first
Open

wozhenbang2004 wants to merge 6 commits into
PyCQA:mainfrom
wozhenbang2004:feat-absolute-imports-first

Conversation

@wozhenbang2004

Copy link
Copy Markdown

Fixes #2146.

An absolute module placed in LOCALFOLDER with known_local_folder currently sorts after relative modules. Add the opt-in absolute_imports_first setting and --absolute-imports-first CLI flag so callers can keep those imports in one section and choose the requested order:

from some_package.foo import baz
from ..foo import bar
from .hoge import piyo

The option applies to the existing module sorting keys, including force_sort_within_sections and lazy import groups. Existing import-style grouping and explicit priorities remain in place; reverse_sort reverses this ordering as well. The default remains unchanged. The configuration reference describes these interactions.

On Python 3.12, the full unit suite passes with 799 tests and two skips, meeting the 99% coverage gate; uv run tox -e lint also passes. The new regression file gives 20 unsupported-option failures and one default control on the original base, then 21 passes on this branch. Actual API/CLI checks confirm repeat formatting, configuration loading, unchanged defaults and execution of a formatted package.

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.

Add option to place absolute import above relative import in the same section

1 participant