Skip to main content

A flake8 plugin for managing type-checking imports & forward references

Project description

Package version Code coverage Test status Supported Python versions Checked with mypy

flake8-type-checking

Lets you know which imports to put inside type-checking blocks.

Also helps you manage forward references.

Codes

Primary codes

Code Description
TCH001 Move import into a type-checking block
TCH101 Move third-party import into a type-checking block
TCH102 Found multiple type checking blocks

Secondary codes

Choose TCHA or TCHB as they are mutually exclusive.

Code Description
TCHA001 Add 'from __future__ import annotations' import
TCHA002 Annotation does not need to be a string literal
Code Description
TCHB001 Annotation needs to be made into a string literal
TCHB002 Annotation does not need to be a string literal

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 TCHA and TCHB 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 forward references.

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: TCH101: Move third-party import 'models.b.B' into a type-checking block
>> b.py: TCH101: 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: TCHA001: Add 'from __future__ import annotations' import
>> b.py: TCHA001: Add 'from __future__ import annotations' import

or

>> a.py: TCHB001: Annotation 'B' needs to be made into a string literal
>> b.py: TCHB001: 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

# TCHA
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

# TCHB
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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8-type-checking-0.1.1.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

flake8_type_checking-0.1.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file flake8-type-checking-0.1.1.tar.gz.

File metadata

  • Download URL: flake8-type-checking-0.1.1.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.9.2 Linux/5.4.0-1043-azure

File hashes

Hashes for flake8-type-checking-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5cfd4335fc4d6784184a3078adc8e4a2e42078efe3bee13d4d324cac4dc1b13d
MD5 df82626222abda609b5526d5908b7ea4
BLAKE2b-256 4b705fe95985e03c09eca86446c2d03590e2c4d53831538dc66be9887cb2dbd3

See more details on using hashes here.

File details

Details for the file flake8_type_checking-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_type_checking-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dcab46b539a1f163430f6454154aebfcd955562cc6bbaf872e046722d9ed59dc
MD5 0db00d8cd91bb0c681725719b948afcb
BLAKE2b-256 5cc84907c4832808c550e5c79fc728b89e05e2171a56b9c63e7bebb0b3e254c7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page