Skip to content

Fix NOQA not added to long imports in several output paths#2568

Open
urayoru113 wants to merge 1 commit into
PyCQA:mainfrom
urayoru113:issue/2093
Open

Fix NOQA not added to long imports in several output paths#2568
urayoru113 wants to merge 1 commit into
PyCQA:mainfrom
urayoru113:issue/2093

Conversation

@urayoru113

@urayoru113 urayoru113 commented Jun 27, 2026

Copy link
Copy Markdown

Fix NOQA not added to long imports in several output paths

In NOQA wrap mode (multi_line_output=7) isort appends `# NOQA` to
imports that cannot fit on one line. Several import output paths
bypassed `wrap.line()` or called it before comments were attached,
so the line-length check never saw the full line and `# NOQA` was
not added.

Four paths were affected:

1. force_single_line as-imports (L404-432): `with_comments()` wrapped
   `wrap.line(...)` instead of the reverse, so comments were attached
   after the length check. Swap the order to `wrap.line(with_comments(...))`.

   Before (line_length=40):
     from m import func as alias  # type: ignore
   After:
     from m import func as alias  # type: ignore  # NOQA

2. as-imports with opening-line comments + use_parentheses (L523-528):
   `wrap.line()` ran on the import without opening comments, then
   comments were appended afterward. Re-run `wrap.line()` in NOQA mode
   after the comment is attached.

   Before (line_length=40):
     from m import func as alias  # type: ignore  # pylint: disable=...
   After:
     from m import func as alias  # type: ignore  # pylint: disable=...  # NOQA

3. combine_straight_imports (L725-736): the combined line was appended
   directly without going through `wrap.line()`. Route it through
   `wrap.line()`.

   Before (line_length=40):
     import a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p
   After:
     import a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p  # NOQA

4. straight imports (L755-767): each import was emitted via a generator
   that only called `with_comments()`, never `wrap.line()`. Convert to
   an explicit loop and wrap each line with `wrap.line()` only in NOQA
   mode, to avoid changing wrapping behaviour in other modes.

   Before (line_length=40):
     import aaaa...long...module as bbbb...long...alias
   After:
     import aaaa...long...module as bbbb...long...alias  # NOQA

Fixes #2093.

@codecov

codecov Bot commented Jun 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.09%. Comparing base (0740d0c) to head (627bf42).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2568      +/-   ##
==========================================
- Coverage   99.16%   99.09%   -0.07%     
==========================================
  Files          41       41              
  Lines        3101     3106       +5     
  Branches      671      673       +2     
==========================================
+ Hits         3075     3078       +3     
- Misses         14       15       +1     
- Partials       12       13       +1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DanielNoord

Copy link
Copy Markdown
Member

@urayoru113 Could you fix CI please?

In NOQA wrap mode (multi_line_output=7) isort appends `# NOQA` to
imports that cannot fit on one line. Several import output paths
bypassed `wrap.line()` or called it before comments were attached,
so the line-length check never saw the full line and `# NOQA` was
not added.

Four paths were affected:

1. force_single_line as-imports (L404-432): `with_comments()` wrapped
   `wrap.line(...)` instead of the reverse, so comments were attached
   after the length check. Swap the order to `wrap.line(with_comments(...))`.

   Before (line_length=40):
     from m import func as alias  # type: ignore
   After:
     from m import func as alias  # type: ignore  # NOQA

2. as-imports with opening-line comments + use_parentheses (L523-528):
   `wrap.line()` ran on the import without opening comments, then
   comments were appended afterward. Re-run `wrap.line()` in NOQA mode
   after the comment is attached.

   Before (line_length=40):
     from m import func as alias  # type: ignore  # pylint: disable=...
   After:
     from m import func as alias  # type: ignore  # pylint: disable=...  # NOQA

3. combine_straight_imports (L725-736): the combined line was appended
   directly without going through `wrap.line()`. Route it through
   `wrap.line()`.

   Before (line_length=40):
     import a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p
   After:
     import a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p  # NOQA

4. straight imports (L755-767): each import was emitted via a generator
   that only called `with_comments()`, never `wrap.line()`. Convert to
   an explicit loop and wrap each line with `wrap.line()` only in NOQA
   mode, to avoid changing wrapping behaviour in other modes.

   Before (line_length=40):
     import aaaa...long...module as bbbb...long...alias
   After:
     import aaaa...long...module as bbbb...long...alias  # NOQA

Fixes PyCQA#2093.
@urayoru113

Copy link
Copy Markdown
Author

@DanielNoord I've already fix the CI problem. Ready for another review. Thanks!

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.

2 participants