Repository navigation
[pyupgrade] Mark fix unsafe when deleting comments (UP035) - #29413
Merged
Merged
Conversation
…ins comments (`UP035`) The fix regenerates the `from ... import ...` statement from the imported names, so comments inside a parenthesized import were deleted even though the fix was marked safe. Mark the fix unsafe when the import statement's range contains a comment, and document this under "Fix safety". Fixes astral-sh#29400
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| UP035 | 8 | 0 | 0 | 0 | 8 |
Linter (preview)
✅ ecosystem check detected no linter changes.
pyupgrade] Mark fix unsafe when deleting comments (UP035)
ntBre
pushed a commit
that referenced
this pull request
Oct 16, 2026
…120`) (#29414) Fixes #29395. Both fixes rewrote the `else:` line and silently dropped any comment after the colon. Following review, the fix no longer tries to move that comment. Instead, PLR5501 and PLW0120 now mark the fix as unsafe when the `else:` line has a comment that would be deleted, the same approach as UP035 (#29413). Without such a comment the fix stays safe. Inline bodies like `else: x = 1 # c` also stay safe, since only `else: ` is removed and the comment belongs to the statement. Both rules got a "Fix safety" section in their docs. The existing PLW0120 snapshots only gain the unsafe-fix note, because their fixtures use `else: # [useless-else-on-loop]`. New fixture cases cover the unsafe and safe paths for both rules. I used an AI assistant while working on this and reviewed the changes myself.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #29400.
UP035 rebuilds the whole
from typing import (...)statement from the names, so any comment inside it, like# noqaafter the paren or a trailing comment on a member, just disappears. The fix was still marked safe.I went with making the fix unsafe whenever a comment falls inside the import, using the same
comment_ranges().intersectscheck astimeout_error_alias. Actually keeping the comments would need tracking them per member, which felt like too much for this. Imports without comments, or with only a comment after the statement, stay safe. I also added a "Fix safety" section to the rule docs.Added four cases to
UP035.py, covering both repros from the issue, a partial move with an own-line comment, and a trailing comment after the statement.Heads up: #28394 touches the same files for a different UP035 case, so one of these will probably need a rebase.
I used an AI assistant while working on this and reviewed the changes myself.