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

Feature Request: Please support isort add_imports option #1700

Closed
nefrob opened this issue Jan 6, 2023 · 8 comments · Fixed by #1762
Closed

Feature Request: Please support isort add_imports option #1700

nefrob opened this issue Jan 6, 2023 · 8 comments · Fixed by #1762
Assignees
Labels
isort Related to import sorting

Comments

@nefrob
Copy link
Contributor

nefrob commented Jan 6, 2023

With isort you can auto import things, i.e. in the pyproject.toml:

[tool.isort]
add_imports = "from __future__ import annotations"

This is a feature request for the same functionality in ruff

[tool.ruff.isort]
add_imports = "from __future__ import annotations"
@charliermarsh charliermarsh added the isort Related to import sorting label Jan 6, 2023
@danieleades
Copy link

see also https://pypi.org/project/flake8-future-annotations/

@charliermarsh
Copy link
Member

Makes sense. Are there valid use-cases here beyond future imports? Most of the examples I see are related to future imports.

(I don't know if this question really affects implementation, but I figured it was worth asking.)

@nefrob nefrob changed the title Please support isort add_imports option Feature Request: Please support isort add_imports option Jan 8, 2023
@nefrob
Copy link
Contributor Author

nefrob commented Jan 8, 2023

Makes sense. Are there valid use-cases here beyond future imports? Most of the examples I see are related to future imports.

(I don't know if this question really affects implementation, but I figured it was worth asking.)

Future imports seems like the most common use case. If ruff supports the flake --force-future-annotations option @danieleades mentioned above that would likely be sufficient as an alternative.

@charliermarsh
Copy link
Member

Cool. The only hard part here is figuring out where to put the import.

@danieleades
Copy link

Cool. The only hard part here is figuring out where to put the import.

Above all the other imports, but below any module-level docs. Right?

@charliermarsh
Copy link
Member

Yes! (The rules are clear, I more meant, from the code perspective, ensuring we put them in a reasonable place even if you don't have import-sorting enabled.)

@charliermarsh charliermarsh self-assigned this Jan 8, 2023
charliermarsh added a commit that referenced this issue Jan 10, 2023
In isort, this is called `add-imports`, but I prefer the declarative
name.

The idea is that by adding the following to your `pyproject.toml`, you
can ensure that the import is included in all files:

```toml
[tool.ruff.isort]
required-imports = ["from __future__ import annotations"]
```

I mostly reverse-engineered isort's logic for making decisions, though I
made some slight tweaks that I think are preferable. A few comments:

- Like isort, we don't enforce this on empty files (like empty
`__init__.py`).
- Like isort, we require that the import is at the top-level.
- isort will skip any docstrings, and any comments on the first three
lines (I think, based on testing). Ruff places the import after the last
docstring or comment in the file preamble (that is: after the last
docstring or comment that comes before the _first_ non-docstring and
non-comment).

Resolves #1700.
@nefrob
Copy link
Contributor Author

nefrob commented Jan 12, 2023

Thanks for tackling this so quickly!

@charliermarsh
Copy link
Member

Totally! Hope it's useful for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
isort Related to import sorting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants