A flake8 plugin that flags imports exclusively used for type annotations
Project description
flake8-typing-only-imports
flake8 plugin that flags imports which are exclusively used for type hinting.
Installation
pip install flake8-typing-only-imports
Codes
Code | Description |
---|---|
TYO100 | Local import '{module}' only used for type hinting |
TYO101 | Remote 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 solves the issue of figuring out which imports to put inside your type-checking blocks 🚀
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
File details
Details for the file flake8-typing-only-imports-0.1.4.tar.gz
.
File metadata
- Download URL: flake8-typing-only-imports-0.1.4.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1039-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f856d4f0acea9a2461a307dceeee20f30fbc7ef08f1b021f6a62677fa6150d52 |
|
MD5 | c26a73f4765b159a0a02e59310684c80 |
|
BLAKE2b-256 | b3f2b3380891850769e71e51a1037b38780f963abacc69502db245d6d8a0b915 |
File details
Details for the file flake8_typing_only_imports-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: flake8_typing_only_imports-0.1.4-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1039-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c336d5d6c5366bfdf2fa53422462351de9aefb6106dc8eabde5072306775e36a |
|
MD5 | 975c92dcf67a344121e79251cdea47c1 |
|
BLAKE2b-256 | 01afe9c7e0ad47838b665e3497a17231b1caad27d45a6a1f5cda31fd129a7bff |