Skip to content

Avoid leading newline with float-to-top#2583

Open
sakshichitnis27 wants to merge 1 commit into
PyCQA:mainfrom
sakshichitnis27:Issue/1935-lines-before-docstring
Open

Avoid leading newline with float-to-top#2583
sakshichitnis27 wants to merge 1 commit into
PyCQA:mainfrom
sakshichitnis27:Issue/1935-lines-before-docstring

Conversation

@sakshichitnis27

Copy link
Copy Markdown
Contributor

Fixes #1935

Summary

  • Avoid applying lines_before_imports during the preliminary float_to_top pass.
  • Let the normal sorting pass apply the configured spacing in the correct location.
  • Add a regression test ensuring module docstrings remain at the beginning of the file.

Testing

  • Reproduced the original issue using lines_before_imports = 1 and float_to_top = true.
  • Verified that the module docstring remains on the first line after the fix.
  • pytest -q — 621 passed, 2 skipped
  • pytest -q tests/unit/test_regressions.py -k "float_to_top or lines_before_imports"
  • ruff check isort/core.py tests/unit/test_regressions.py
  • mypy isort/core.py tests/unit/test_regressions.py
  • flake8 isort/core.py tests/unit/test_regressions.py
  • isort --check-only isort/core.py tests/unit/test_regressions.py

@codecov

codecov Bot commented Jul 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.39%. Comparing base (a8c0004) to head (d955593).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2583   +/-   ##
=======================================
  Coverage   99.39%   99.39%           
=======================================
  Files          41       41           
  Lines        3137     3137           
  Branches      680      680           
=======================================
  Hits         3118     3118           
  Misses         11       11           
  Partials        8        8           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DanielNoord DanielNoord left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the fix!

It works, but I don't think this is how we'd like to solve this. I'd expect the fix to actually be in sorted_imports instead of overwriting a configuration value on the fly. That could have unintended side effects. Do you think you can fix this without doing this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

lines_before_import adds newlines above module's docstring when used with float_to_top

2 participants