A flake8 plugin that flags imports exclusively used for type annotations
Project description
flake8-typing-only-imports
Plugin is still a work in progress
flake8 plugin that helps identify which imports to put into type-checking blocks, and how to adjust your type annotations once imports are moved.
Installation
pip install flake8-typing-only-imports
Codes
Code | Description |
---|---|
TYO100 | Import should be moved to a type-checking block |
TYO101 | Third-party import should be moved to a type-checking block |
TYO200 | Missing 'from __future__ import annotations' import |
TYO201 | Annotation is wrapped in unnecessary quotes |
TYO300 | Annotation should be wrapped in quotes |
TYO301 | Annotation is wrapped in unnecessary quotes |
TYO101
is disabled by default because third-party imports usually
aren't a real concern with respect to import circularity issues.
TYO2XX
and TYO3XX
are mutually exclusive as they represent
two different ways of solving the same problem. Make sure to ignore or enable just one of the series.
Motivation
Two common issues when annotating large code bases are:
- Import circularity issues
- Annotating not-yet-defined structures
These problems are largely solved by two features:
-
Type checking blocks
from typing import TYPE_CHECKING if TYPE_CHECKING: # this code is not evaluated at runtime from foo import bar
-
Forward references
Which can be used, like thisclass Foo: def bar(self) -> 'Foo': return Foo()
or since PEP563 was implemented, like this:
from __future__ import annotations class Foo: def bar(self) -> Foo: return Foo()
See this excellent stackoverflow response explaining forward references, for more context.
The aim of this plugin is to automate the management of type annotation imports, and the forward references that then become necessary.
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]
Supporting the project
Leave a ⭐️ if this project helped you!
Contributions are always welcome 👏
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.9.tar.gz
.
File metadata
- Download URL: flake8-typing-only-imports-0.1.9.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.2 Linux/5.4.0-1040-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17ab2d5ef97f50a29a22bf44840b791d74507e5d1b459998e434de0f70d9bf22 |
|
MD5 | 851bee3c8f0a1f105457f62b766d3f0d |
|
BLAKE2b-256 | 5d4cde27bb21eed0b3c2fabe6ea483c58a4ffd1418e730be02a00181114a7de2 |
File details
Details for the file flake8_typing_only_imports-0.1.9-py3-none-any.whl
.
File metadata
- Download URL: flake8_typing_only_imports-0.1.9-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.2 Linux/5.4.0-1040-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48be9e52d706d60cad5aba56ceae715b887d98f55d0c56c822882f2d8e0ee53d |
|
MD5 | 0b409322030a494edacd3f1d627b7e2e |
|
BLAKE2b-256 | ea267f76ab1a622220c500bc122b8441b7b439cb9e0c499551d156cf5824b209 |