A flake8 plugin that flags imports exclusively used for type annotations
Project description
flake8-typing-only-imports
flake8 plugin which flags imports which are exclusively used for type hinting.
Installation
pip install flake8-typing-only-imports
Codes
Code | Description |
---|---|
TYO100 | Import '{module}' only used for type hinting |
Rationale
A common trade-off for annotating large code bases is you will end up with a large number of extra imports. In some cases, this can lead to import circularity problems.
One (good) solution, as proposed in PEP484 is to use forward references and type checking blocks, like this:
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from app.models import foo
def bar() -> 'foo':
...
At the same time, this is often easier said than done, because in larger code bases you can be dealing with hundreds of lines of imports for thousands of lines of code.
This plugin eliminates that problem by flagging imports which can be put inside a type-checking block.
As a pre-commit hook
See pre-commit for instructions
Sample .pre-commit-config.yaml
:
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.8
hooks:
- id: flake8
additional_dependencies: [flake8-typing-only-imports]
Release process
- Bump version in
setup.cfg
. - Add a commit "Release vX.Y.Z".
- Make sure checks still pass.
- Draft a new release with a tag name "X.Y.Z" and describe changes which involved in the release.
- Publish the release.
Flake8 Type Hinting Only Imports
This is flake8 hook flags all your imports that are being exclusively used for type hinting.
Running the hook on
import a
from b import c
def example(d: c):
return a.transform(d)
Will produce this
> ../file.py:3:1: TYO100: Import 'c' only used for type annotation
Motivation
- Circular imports
- Efficiency
- How are imports handled at compile time
- More type hinting -> exaggerated problem
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flake8-typing-only-imports-0.1.0.tar.gz
.
File metadata
- Download URL: flake8-typing-only-imports-0.1.0.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.0 Darwin/20.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | abc68b47d11f42c0fa41f987186b5f76e2fa66e73237b61049d3d28a618c2d2b |
|
MD5 | 164d0ef764f6b89b6c8c5ecd8400323c |
|
BLAKE2b-256 | a513b2e8b508cfebd384a02dd05f6d53672e1c5be882c0ce465684f855b4cf2e |
File details
Details for the file flake8_typing_only_imports-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: flake8_typing_only_imports-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.0 Darwin/20.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2aeb61aee8c426a42c86fd46c9caa9f6c212f4fe4a4f7c68db068fabd6c3fdef |
|
MD5 | 242bef31969b5151af6d22d3d3a288f3 |
|
BLAKE2b-256 | a3ec31f5f0988b24f63382e0e3d5b921620cf2a20a9a1640c900bb652a1bfe7c |