Skip to content

[ty] Preserve blank lines between comments and imports in add-import action#24066

Merged
MichaReiser merged 3 commits intoastral-sh:mainfrom
mvanhorn:fix/ty-import-preserve-blank-lines-after-comments
Mar 20, 2026
Merged

[ty] Preserve blank lines between comments and imports in add-import action#24066
MichaReiser merged 3 commits intoastral-sh:mainfrom
mvanhorn:fix/ty-import-preserve-blank-lines-after-comments

Conversation

@mvanhorn
Copy link
Contributor

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):

# /// script
# dependencies = ["anyio"]
# ///
from anyio import AsyncFile

import datetime as dt

After this fix (blank line preserved):

# /// script
# dependencies = ["anyio"]
# ///

from anyio import AsyncFile
import datetime as dt

The fix tracks 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, the insertion point advances past them to preserve the gap. Blank lines that appear before any comments are not affected.

Test plan

  • Added two test cases to start_of_file tests: one with a blank line between comments and imports (verifying the gap is preserved), and one without (verifying no regression)
  • All 293 ty_ide import tests pass
  • All 3 ruff_python_importer tests pass
  • cargo clippy and cargo fmt clean

…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
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Thank you

@MichaReiser MichaReiser added the fixes Related to suggested fixes for violations label Mar 20, 2026
@MichaReiser MichaReiser enabled auto-merge (squash) March 20, 2026 08:49
@astral-sh-bot
Copy link

astral-sh-bot bot commented Mar 20, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser MichaReiser merged commit fff4953 into astral-sh:main Mar 20, 2026
41 checks passed
@mvanhorn
Copy link
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fixes Related to suggested fixes for violations ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add import: Preserve blank lines between comments and import statements

3 participants