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]
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
Hashes for flake8-typing-only-imports-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b64afd98806c6d935496dc19a73fa30790948afb6dcacd6dc1361650eae74bee |
|
MD5 | be7f75518be107b47b0d28454f71c652 |
|
BLAKE2b-256 | 0858cc67cd7609939c6ec25dce0cc023d9634f01f6793d0d1fb68c2e6dc5017c |
Hashes for flake8_typing_only_imports-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a7cac0d45a57af88b158bc270abbb76e0e4810d4e0742909a0d4466de8746e3 |
|
MD5 | 183d9306c36051832cc6e212cfd8549a |
|
BLAKE2b-256 | 43e20313778ac8177512fc0ceefff3178386935c6071162f07e94029b2560d45 |