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.10.tar.gz
.
File metadata
- Download URL: flake8-typing-only-imports-0.1.10.tar.gz
- Upload date:
- Size: 9.8 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 | 54b0aaadf980d5928949d4a4c7c9c2e26d252f7a7f887faa18f04556241da6ba |
|
MD5 | 7c389a3e7697099c507bc76cc10ba744 |
|
BLAKE2b-256 | f3b1c6478cbd35ab1facf89935737e985d4a6e5e308b173fc4f88eed979f9f0e |
File details
Details for the file flake8_typing_only_imports-0.1.10-py3-none-any.whl
.
File metadata
- Download URL: flake8_typing_only_imports-0.1.10-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 | aa35d99f1c249326222ebb7ac85d1da404c1b0433cd9ca87a29e1b55ac5ff737 |
|
MD5 | 27686090b7211aee95ed7ad25e5cc92f |
|
BLAKE2b-256 | 4e1580a276ebe3b0ffec8f125db8b04d2a2bb7e4fb1ae7d7980bb881a8cac1e2 |