mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 10:22:24 +00:00
Implement autofix for relative imports (TID252) (#2739)
This commit is contained in:
parent
dadbfea497
commit
e83ed0ecba
8 changed files with 619 additions and 83 deletions
40
docs/rules/relative-imports.md
Normal file
40
docs/rules/relative-imports.md
Normal file
|
@ -0,0 +1,40 @@
|
|||
# relative-imports (TID252)
|
||||
|
||||
Derived from the **flake8-tidy-imports** linter.
|
||||
|
||||
Autofix is sometimes available.
|
||||
|
||||
## What it does
|
||||
Checks for relative imports.
|
||||
|
||||
## Why is this bad?
|
||||
Absolute imports, or relative imports from siblings, are recommended by [PEP 8](https://peps.python.org/pep-0008/#imports):
|
||||
|
||||
> Absolute imports are recommended, as they are usually more readable and tend to be better behaved...
|
||||
> ```python
|
||||
> import mypkg.sibling
|
||||
> from mypkg import sibling
|
||||
> from mypkg.sibling import example
|
||||
> ```
|
||||
> However, explicit relative imports are an acceptable alternative to absolute imports,
|
||||
> especially when dealing with complex package layouts where using absolute imports would be
|
||||
> unnecessarily verbose:
|
||||
> ```python
|
||||
> from . import sibling
|
||||
> from .sibling import example
|
||||
> ```
|
||||
|
||||
Note that degree of strictness packages can be specified via the
|
||||
[`strictness`](https://github.com/charliermarsh/ruff#strictness)
|
||||
configuration option, which allows banning all relative imports (`strictness = "all"`)
|
||||
or only those that extend into the parent module or beyond (`strictness = "parents"`).
|
||||
|
||||
## Example
|
||||
```python
|
||||
from .. import foo
|
||||
```
|
||||
|
||||
Use instead:
|
||||
```python
|
||||
from mypkg import foo
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue