Implement autofix for relative imports (TID252) (#2739)

This commit is contained in:
Simon Brugman 2023-02-11 04:05:47 +01:00 committed by GitHub
parent dadbfea497
commit e83ed0ecba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 619 additions and 83 deletions

View 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
```