-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Comments
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 |
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? |
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.) |
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.
Thanks for tackling this so quickly! |
Totally! Hope it's useful for you! |
With
isort
you can auto import things, i.e. in thepyproject.toml
:This is a feature request for the same functionality in
ruff
The text was updated successfully, but these errors were encountered: