Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruff format insist on "single line after imports" for pyi files #9353

Closed
kkpattern opened this issue Jan 2, 2024 · 4 comments · Fixed by #9971
Closed

Ruff format insist on "single line after imports" for pyi files #9353

kkpattern opened this issue Jan 2, 2024 · 4 comments · Fixed by #9971
Assignees
Labels
configuration Related to settings and configuration

Comments

@kkpattern
Copy link

When formatting a pyi file, ruff format only keeps a single line after the imports. If ruff.lint.isort.lines-after-imports is configured to 2. ruff format will conflict with ruff check.

This doesn't happen to py files. ruff format will keep the two lines after the imports.

Example:

(.venv) ☁  ruff-demo  cat test.py
from typing import Any


class Foo:
    def bar(self, data: Any) -> None:
        pass
(.venv) ☁  ruff-demo  ruff format --check test.py
1 file already formatted


(.venv) ☁  ruff-demo  cat test.pyi
from typing import Any


class Foo:
    def bar(self, data: Any) -> None: ...
(.venv) ☁  ruff-demo  ruff format --check test.pyi
Would reformat: test.pyi
1 file would be reformatted
(.venv) ☁  ruff-demo

Ruff Version: 0.1.9
Ruff Command: ruff format

@charliermarsh
Copy link
Member

This feels like it's "working as intended" even though the option is conflicting. If anything, we may want to warn here, as we do for other incompatible options (2 is a permitted value, but I don't think we considered that it would conflict for .pyi files). The alternative would be to ignore lines-after-imports in .pyi files.

@charliermarsh charliermarsh added configuration Related to settings and configuration needs-decision Awaiting a decision from a maintainer labels Jan 2, 2024
@kkpattern
Copy link
Author

Ignore lines-after-imports in .pyi files would be nice. Currently, we're using both ruff check and ruff format --check in our CI pipeline. We do want two lines after the imports in py files. So, in order to make the CI green, we need to skip ruff check for all the pyi files for now.

@MichaReiser MichaReiser added this to the Formatter: Stable milestone Jan 8, 2024
@MichaReiser
Copy link
Member

Thank you for reporting this issue. That's something I haven't been aware when adding the warnings.

Warning when setting lines-after-imports to a value other than 1 might be noisy, except if we limit it only to warn when formatting a pyi file (which would be different from the other incompatible warnings). It would also not enable the use case mentioned by @kkpattern.

I think I would start by ignoring lines-after-imports in pyi files. But we may need to do run a GitHub code search first to find projects that use lines-after-imports with a value other than 1 and have pyi files to not break their setup.

@MichaReiser
Copy link
Member

isort fixes lines-after-imports and lines-before-imports to 1 when using the black profile

https://github.com/PyCQA/isort/blob/eed5d059a8ea5442266be892322f8815074800a5/isort/output.py#L211-L225

@MichaReiser MichaReiser removed the needs-decision Awaiting a decision from a maintainer label Feb 13, 2024
@MichaReiser MichaReiser self-assigned this Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
configuration Related to settings and configuration
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants