Skip to content

E501/W505/formatter: Exclude nested pragma comments from line width calculation #24071

Merged
MichaReiser merged 2 commits intoastral-sh:mainfrom
seroperson:i18470-nested-pragma-e501
Mar 24, 2026
Merged

E501/W505/formatter: Exclude nested pragma comments from line width calculation #24071
MichaReiser merged 2 commits intoastral-sh:mainfrom
seroperson:i18470-nested-pragma-e501

Conversation

@seroperson
Copy link
Copy Markdown
Contributor

Closes #18470

Summary

  • Linter (E501/W505): when a pragma (# noqa, # type: ignore, etc) follows another comment on the same line (like # comment # noqa: F401 or ## noqa), only the pragma suffix is stripped before measuring line length, so the pragma no longer causes false line-too-long violations.
  • Formatter: reserved width for trailing comments now accounts for nested pragmas - only the non-pragma prefix counts toward line-breaking decisions.
  • Core: added find_trailing_pragma_offset() to ruff_python_trivia that finds the byte offset where a trailing pragma starts within a comment. is_pragma_comment() left unchanged.

Changes to both linter and formatter are gated behind preview via new is_trailing_pragma_in_line_length_enabled check.

Test Plan

  • Added linter test fixture E501_5.py with preview test cases for nested pragmas (# comment #noqa, ## noqa, # comment # type: ignore)
  • Added formatter test fixture trailing_pragma_nested.py demonstrating stable vs preview reserved width behavior
  • Doctests for find_trailing_pragma_offset in pragmas.rs

@seroperson seroperson requested a review from MichaReiser as a code owner March 20, 2026 10:08
@astral-sh-bot astral-sh-bot bot requested a review from amyreese March 20, 2026 10:08
@seroperson seroperson force-pushed the i18470-nested-pragma-e501 branch from bb536e4 to c0ec5a7 Compare March 20, 2026 10:11
@seroperson seroperson force-pushed the i18470-nested-pragma-e501 branch from c0ec5a7 to da0d934 Compare March 23, 2026 06:30
@MichaReiser MichaReiser added formatter Related to the formatter preview Related to preview mode features labels Mar 24, 2026
@MichaReiser MichaReiser changed the title E501 false positive for nested pragma comments E501/W505/formatter: Exclude nested pragma comments from line width calculation Mar 24, 2026
@MichaReiser MichaReiser enabled auto-merge (squash) March 24, 2026 10:05
@MichaReiser MichaReiser merged commit 9d2b160 into astral-sh:main Mar 24, 2026
45 checks passed
carljm added a commit that referenced this pull request Mar 25, 2026
* main:
  [ty] Avoid eager TypedDict diagnostics in `TypedDict | dict` unions (#24151)
  `F507`: Fix false negative for non-tuple RHS in `%`-formatting (#24142)
  [ty] Update `SpecializationBuilder` hook to get both lower/upper bounds (#23848)
  Fix `%foo?` parsing in IPython assignment expressions (#24152)
  `E501`/`W505`/formatter: Exclude nested pragma comments from line width calculation  (#24071)
  [ty] Fix Salsa panic propagation (#24141)
  [ty] Support `type:ignore[ty:code]` suppressions (#24096)
  [ty] Support narrowing for extended walrus targets (#24129)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

formatter Related to the formatter preview Related to preview mode features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stacking # noqa with other ignore comments causes an E501 (line too long) violation

3 participants