Skip to content

Only explode the statements that had a trailing comma when aliases split a module - #2727

Open
lprnmns wants to merge 1 commit into
PyCQA:mainfrom
lprnmns:split-on-trailing-comma-aliased-idempotent
Open

lprnmns wants to merge 1 commit into
PyCQA:mainfrom
lprnmns:split-on-trailing-comma-aliased-idempotent

Conversation

@lprnmns

@lprnmns lprnmns commented Oct 15, 2026

Copy link
Copy Markdown

With split_on_trailing_comma, isort isn't idempotent when an aliased import splits one module's names into several statements. Running --profile appnexus over the CPython 3.12 stdlib, csv.py and ssl.py change again on the second run. Minimal case:

from _csv import QUOTE_MINIMAL, QUOTE_NONNUMERIC, QUOTE_NONE, QUOTE_STRINGS, QUOTE_NOTNULL, __doc__
from _csv import Dialect as _Dialect

The first run gives from _csv import __doc__, the aliased import, and the remaining names wrapped with a trailing comma. On the second run, parse records the trailing comma for the whole _csv module, so _build_grouped_from_imports also explodes the single-name from _csv import __doc__ into from _csv import (\n __doc__,\n).

trailing_commas now maps each module to the names of the statements that had the trailing comma. A grouped statement is only exploded for that reason if it contains one of those names. When a module ends up as a single statement (the usual case) the result is the same as before.

Added a regression test with the case above. It fails on main and passes here. The unit tests give the same results as on main (two tests fail locally with FileNotFoundError on both). ruff, mypy and flake8 are clean. With the change, running every built-in profile twice over the 3.12 stdlib produces no further changes.

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.

1 participant