Skip to main content
Package version Code coverage Test status Supported Python versions Checked with mypy

flake8-typing-only-imports

Plugin is still a work in progress

Tells you which imports to put inside type-checking blocks.

Codes

Code Description
TYO100 Move import into a type-checking block
TYO101 Move third-party import into a type-checking block
TYO102 Found multiple type checking blocks
TYO200 Add 'from __future__ import annotations' import
TYO201 Annotation does not need to be a string literal
TYO300 Annotation needs to be made into a string literal
TYO301 Annotation does not need to be a string literal

Rationale

TYO100 guards against import cycles . TYO101 applies the same logic, for venv or stdlib imports.

Remaining error codes are there to help manage forward references, either by telling your to use string literals where needed, or by enabling postponed evaluation of annotations. The error code series TYO2XX and TYO3XX should therefore be considered mutually exclusive, as they represent two different ways of managing forward references.

See this excellent stackoverflow answer for a quick explanation of forward references.

Installation

pip install flake8-typing-only-imports

Suggested use

Only enable TYO101 if you're after micro performance gains on start-up.

TYO2XX and TYO3XX are reserved for error codes to help manage forward references. It does not make sense to enable both series, and they should be considered mutually exclusive.

If you're adding this to your project, we would recommend something like this:

select = TYO100, TYO200, TYO200  # or TYO300 and TYO301

ignore = TYO101, TYO300, TYO301  # or TYO200 and TYO201

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): ...

Which will first result in these errors

>> a.py: TYO101: Move third-party import 'models.b.B' into a type-checking block
>> b.py: TYO101: 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: TYO300: Annotation 'B' needs to be made into a string literal
>> b.py: TYO300: 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

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-typing-only-imports ]

Supporting the project

Leave a ⭐️  if this project helped you!

Contributions are always welcome 👏

Metadata

Release files for flake8-typing-only-imports 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flake8-typing-only-imports 0.2.2
File Size Uploaded
flake8-typing-only-imports-0.2.2.tar.gz 10.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flake8-typing-only-imports 0.2.2
File Interpreter ABI Platform
flake8_typing_only_imports-0.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 20.9 kB

Release files / flake8-typing-only-imports-0.2.2.tar.gz

Download URL flake8-typing-only-imports-0.2.2.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0306fa7cf2da81d9cac409453e4f1c3400edcffbda1460c8d9887e501d4e1da6
BLAKE2b-256 checksum
How to use checksums
2730c8c4f778cd4d4b88f67f01d257c02c62ce8b4a3beb7dff4703e246e7e76a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.5 CPython/3.9.2 Linux/5.4.0-1040-azure

Release files / flake8_typing_only_imports-0.2.2-py3-none-any.whl

Download URL flake8_typing_only_imports-0.2.2-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ef6d080a81f0ae5b3c891dc36378ddae9a7ea84192f6a01644d490ba4593da49
BLAKE2b-256 checksum
How to use checksums
2cf9105cffd4ac293ff82c339a21e4cfddc3c1182208656e73fccf20aa79576d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.5 CPython/3.9.2 Linux/5.4.0-1040-azure

Release history Release notifications | RSS feed

This release

0.2.2 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.12

2 release files

0.1.9

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page