[ty] Preserve blank lines between comments and imports in add-import action#24066
Merged
MichaReiser merged 3 commits intoastral-sh:mainfrom Mar 20, 2026
Conversation
…code action When the add-import code action inserts a new import after a comment block (e.g., PEP 723 script metadata), it dropped the blank line between the comment and the existing imports. This caused the comment to visually merge with the import block after formatting. Fix this by tracking blank lines encountered after the last comment in the start-of-file insertion logic. When blank lines exist between the final comment and the first statement, advance the insertion point past them to preserve the gap. Closes astral-sh/ty#2990
|
Contributor
Author
|
Thanks for the merge! |
carljm
added a commit
that referenced
this pull request
Mar 25, 2026
* main: [`flake8-bandit`] Check tuple arguments for partial paths in `S607` (#24080) [ty] Update Salsa (#24081) Update Rust toolchain to 1.94 and MSRV to 1.92 (#24076) [ty] Move ruffen-docs formatting config to a `ruff.toml` config file (#24074) [ty] `reveal_type` diagnostics in unreachable code (#24070) [ty] Improve keyword argument narrowing for nested dictionaries (#24010) [ty] Preserve blank lines between comments and imports in add-import action (#24066) [ty] Add diagnostic hint for invalid assignments involving invariant generics (#24032) Clarify `extend-ignore` and `extend-select` settings documentation (#24064) [ty] Batch changes to watched paths (#24045) replace deprecated `std::f64::EPSILON` with `f64::EPSILON` (#24067) [ty] Fix untracked reads in Salsa queries that can lead to backdating panics (#24051) [ty] Unions/intersections of gradual types should be assignable to `Never` (#24056) Fix incorrect path for ty_python_semantic in fuzzer (#24052) Bump 0.15.7 (#24049) [ty] ecosystem-analyzer: Fail on newly panicking projects (#24043) Don't show noqa hover for non-Python documents (#24040)
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.
Summary
Fixes astral-sh/ty#2990
When the add-import code action inserts a new import after a comment block (e.g., PEP 723 script metadata), it dropped the blank line between the comment and the existing imports. After a formatter runs, this caused the comment to visually merge with the import block.
Before (blank line lost after inserting
from anyio import AsyncFile):After this fix (blank line preserved):
The fix tracks blank lines encountered after the last comment in the
start_of_fileinsertion logic. When blank lines exist between the final comment and the first statement, the insertion point advances past them to preserve the gap. Blank lines that appear before any comments are not affected.Test plan
start_of_filetests: one with a blank line between comments and imports (verifying the gap is preserved), and one without (verifying no regression)cargo clippyandcargo fmtclean