GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Excessive whitespace trimming with comment formatting by EkriirkE · Pull Request #26455 · astral-sh/ruff

Conversation

@EkriirkE

@EkriirkE EkriirkE commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

When formatting comments to ensure a whitespace between the hash and content, tabular indentation(s) are stripped. This should not be the case - to fall in line with how Black handles it, it treats any leading tab as a non-space and injects a space between the hash and tab.

Write code with beautiful tabs. Comment out a few lines of code that include indentation. Also make general normal comments, lazily without proper spaces. Inspect formatted output that previous reformats are intact, and no tabs in new format are removed.

If understand the code correctly, it already right-trims the comment first thing. Then it checks for special cases where spaces should not be injected (including a space already there), Then a special NBSP replacement. Finally, a left-trim - which as far as I can tell only serves to trim tabs at this point as all other whitespace occurrences are handled/ignored.

Compiled locally, binaries replaced. Code with the above comment scenarios were reformatted as expected (aligning with black).

Closes #25019

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Erik Johnson and others added 5 commits May 18, 2026 09:42
Erik Johnson
Full trim serves nothing but to remove tabs
a088a79
Erik Johnson
Black comparison
c7f1c62
@EkriirkE
Merge branch 'astral-sh:main' into hotfix/Excessive-Comment-Trimming
d841a5a
@EkriirkE
Merge branch 'astral-sh:main' into hotfix/Excessive-Comment-Trimming
a2e6692
@EkriirkE
Merge branch 'astral-sh:main' into hotfix/Excessive-Comment-Trimming
2522e5a
@EkriirkE
EkriirkE requested a review from MichaReiser as a code owner June 29, 2026 11:52
@astral-sh-bot

astral-sh-bot Bot commented Jun 29, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@MichaReiser MichaReiser added the formatter Related to the Ruff formatter label Jun 29, 2026
MichaReiser
MichaReiser requested changes Jul 6, 2026
View reviewed changes
Comment thread crates/ruff_python_formatter/src/comments/format.rs
}
} else {
Ok(Cow::Owned(std::format!("# {}", content.trim_start())))
Ok(Cow::Owned(std::format!("# {content}")))

@MichaReiser MichaReiser Jul 6, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I think we need to preview gate this change, because it can change existing formatted code.

To do so, add a new function similar to

ruff/crates/ruff_python_formatter/src/preview.rs

Lines 24 to 32 in 895e965

/// Returns `true` if the
/// [trailing pragma width handling in comments](https://github.com/astral-sh/ruff/pull/24071)
/// is enabled.
/// When enabled, comments like `# text # noqa: F401` only reserve width for
/// the non-pragma prefix (`# text`), not the trailing pragma.
/// Make sure to stabilize the corresponding linter preview behavior when stabilizing this preview style.
pub(crate) const fn is_trailing_pragma_in_comment_width_enabled(context: &PyFormatContext) -> bool {
context.is_preview()
}

then check the preview state. Doing this is a bit annoying here, because it requires threading through the &PyFormatContext, but that's fine.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@EkriirkE EkriirkE Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to preview gate this change, because it can change existing formatted code.

It should not modify anything already formatted because it will have stripped any non-space-whitespace (tabs) for a space previously and anything with a space does not get this far into the logic (491: they are unaffected by this change).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@MichaReiser MichaReiser Jul 6, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, sorry, my mistake

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

EkriirkE reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@MichaReiser MichaReiser added the preview Related to preview-mode features label Jul 6, 2026
@MichaReiser
MichaReiser self-requested a review July 6, 2026 09:15
@MichaReiser
MichaReiser enabled auto-merge (squash) July 6, 2026 09:15
MichaReiser
MichaReiser approved these changes Jul 6, 2026
View reviewed changes

@MichaReiser MichaReiser left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

EkriirkE reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@MichaReiser
MichaReiser merged commit 40a62bc into astral-sh:main Jul 6, 2026
47 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@BrewTestBot BrewTestBot mentioned this pull request Jul 9, 2026
ruff 0.15.21 Homebrew/homebrew-core#292272
Merged
@github-actions github-actions Bot mentioned this pull request Jul 14, 2026
chore(deps): update ⬆️ mise-packages scottames/dots#978
Merged
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@MichaReiser MichaReiser MichaReiser approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

formatter Related to the Ruff formatter preview Related to preview-mode features

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Comment space normalization dedents commented out code when using tabs

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@EkriirkE @MichaReiser
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.