ruff
ruff copied to clipboard
Limit `isort.lines-after-imports` to 1 for stub files
Summary
Setting the isort.lines-after-imports setting to a value larger than 1 leads to formatter incompatibilities when formatting typing stub files because
the formatter enforces at most one blank line for stub files according to typeshed's style guide.
This PR changes our isort implementation to limit lines-after-imports to 1 for stub files the same as isort does for black (commit, code).
Fixes https://github.com/astral-sh/ruff/issues/9353
Breaking Change?
This is a breaking change and we may need to wait for 0.3 to merge. The only "excuse" not to consider this a breaking change is that we say our isort configuration is intended to be black/ruff formatter compatible by default (isort profile black). In this case, this is a bug fix.
I did a code search on GitHub for lines-after-imports=2 (I didn't find any usages with values > 2). Most repositories don't have pyi files and are unaffected by the change. The following repositories contain pyi files and use lines-after-imports=2
- https://github.com/Ayan-Bandyopadhyay/BentoML: Fork of https://github.com/bentoml/BentoML The
lines-after-importssetting was removed upstream in https://github.com/bentoml/BentoML/pull/3864 -> No longer an issue - https://github.com/python-poetry/poetry/ The pyi files are excluded from linting. Running this branch does not raise any new lint errors.
- https://github.com/conda/conda-lock Pyi file locations are excluded.
- https://github.com/pytest-dev/pytest/ All
pyifiles are empty__init__.pyifiles (contain no imports) - https://github.com/sdss/lvmgort They have
__init__.pyifiles that contain imports. They added two blank lines when migrating to ruff ;( https://github.com/sdss/lvmgort/commit/ca8206a68107c6ee689e427f7a13d1ec2f016cff - https://github.com/bugbakery/whispercpp They exclude the typing directory
- https://github.com/sdss/yao Use two blank lines after imports in typing files. They don't use the formatter
From that it seems safe to conclude that using lines-after-imports=2 with typing files isn't common, but at least two projects would see new errors after upgrading.
Test Plan
Added test
ruff-ecosystem results
Linter (stable)
✅ ecosystem check detected no linter changes.
Linter (preview)
✅ ecosystem check detected no linter changes.
To confirm my understanding: if a project today doesn't set lines-after-imports, we do use 1 as a default in .pyi files, right?
To confirm my understanding: if a project today doesn't set
lines-after-imports, we do use 1 as a default in.pyifiles, right?
Is this a very friendly hint that my implementation is wrong because it is wrong ;) It uses -1 which uses 2 empty lines if the following statement is a class or function. I have to add a test and fix the implementation.
To confirm my understanding: if a project today doesn't set
lines-after-imports, we do use 1 as a default in.pyifiles, right?
I think I know understand why you asked this question... Yes, the change isn't just a breaking change when using lines-after-imports=2 but also when using the default configuration if the imports are directly followed by a class or function declaration (which I assume is fairly common).
I think that's reason enough for us to wait with merging until 0.3
I asked because my guess was that it wasn't a breaking change for files that don't set lines-after-imports, because I thought we already limited to 1 in stub files. But it sounds like my guess was wrong :D