A flake8 plugin for managing type-checking imports & forward references
Project description
flake8-type-checking
Lets you know which imports to put inside type-checking blocks.
Also helps you manage forward references.
Codes
Enabled by default
Code | Description |
---|---|
TC001 | Move import into a type-checking block |
TC002 | Move third-party import into a type-checking block |
TC003 | Found multiple type checking blocks |
TC004 | Move import out of type-checking block. Import is used for more than type hinting. |
Disabled by default
Codes related to forward reference management should probably be activated, but since there are two different ways of managing them, they are disbaled by default and you need to choose one of them.
TCH100
and TCH101
manage forward references by taking advantage of
postponed evaluation of annotations.
Code | Description |
---|---|
TC100 | Add 'from __future__ import annotations' import |
TC101 | Annotation does not need to be a string literal |
TCH200
and TCH201
manage forward references using string string literals
(wrapping the annotation in quotes).
Code | Description |
---|---|
TC200 | Annotation needs to be made into a string literal |
TC201 | Annotation does not need to be a string literal |
To enable them, just specify them in your flake8 config
[flake8]
max-line-length = 80
max-complexity = 12
...
ignore = E501
select = C,E,F,W,B,TC2 # or TC1
If you're not sure which to pick, see rationale or examples for a better explanation of the difference.
Rationale
We generally want to use TYPE_CHECKING
blocks for imports where we can, to guard
against import cycles.
An added bonus is that guarded imports are not loaded when you start your app, so
theoretically you should get a slight performance boost there as well.
Once imports are guarded, type hints should be treated as forward references. Remaining error codes are there to help manage that, either by telling your to use string literals where needed, or by enabling postponed evaluation of annotations.
The error code series TCH1
and TCH2
should therefore be considered
mutually exclusive, as they represent two different ways of solving the same problem.
See this excellent stackoverflow answer
for a quick explanation of the differences.
Installation
pip install flake8-type-checking
Examples
Bad code
models/a.py
from models.b import B
class A(Model):
def foo(self, b: B): ...
models/b.py
from models.a import A
class B(Model):
def bar(self, a: A): ...
Will result in these errors
>> a.py: TC002: Move third-party import 'models.b.B' into a type-checking block
>> b.py: TC002: Move third-party import 'models.a.A' into a type-checking block
and consequently trigger these errors if imports are purely moved into type-checking block, without proper forward reference handling
>> a.py: TC100: Add 'from __future__ import annotations' import
>> b.py: TC100: Add 'from __future__ import annotations' import
or
>> a.py: TC200: Annotation 'B' needs to be made into a string literal
>> b.py: TC200: Annotation 'A' needs to be made into a string literal
Good code
models/a.py
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from models.b import B
class A(Model):
def foo(self, b: 'B'): ...
models/b.py
# TCH1
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from models.a import A
class B(Model):
def bar(self, a: A): ...
or
# TC2
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from models.a import A
class B(Model):
def bar(self, a: 'A'): ...
As a pre-commit hook
You can run this flake8 plugin as a pre-commit hook:
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.8
hooks:
- id: flake8
additional_dependencies: [ flake8-type-checking ]
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
Hashes for flake8-type-checking-0.1.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85f9aae2ddb03f2ab9e7f3dafd5cfbaf4a320620f22b80d7a6c1bce145150431 |
|
MD5 | 6f15ba3cc7796868b6c88cc244c2ddbe |
|
BLAKE2b-256 | 9b0de2f18618809466f992d40b45a34befa61771a293734a30dcd9d974e5486e |
Hashes for flake8_type_checking-0.1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 052bffc74e56396461033a63d9cad24906dad58c82f11eb21a7ae964b463072e |
|
MD5 | 9fc5f3d1b8dbbecd77c6c74ad22f271e |
|
BLAKE2b-256 | 830f696e6aab0c1bcbfac17a9459c6db82be53d85cfc877a5ac4695d8112a6bb |