Skip to main content

A flake8 plugin that flags imports exclusively used for type annotations

Project description

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

flake8-typing-only-imports

flake8 plugin which flags imports which are exclusively used for type hinting.

Installation

pip install flake8-typing-only-imports

Codes

Code Description
TYO100 Import '{module}' only used for type hinting

Rationale

A common trade-off for annotating large code bases is you will end up with a large number of extra imports. In some cases, this can lead to import circularity problems.

One (good) solution, as proposed in PEP484 is to use forward references and type checking blocks, like this:

from typing import TYPE_CHECKING


if TYPE_CHECKING:
    from app.models import foo


def bar() -> 'foo':
    ...

At the same time, this is often easier said than done, because in larger code bases you can be dealing with hundreds of lines of imports for thousands of lines of code.

This plugin eliminates that problem by flagging imports which can be put inside a type-checking block.

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]

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-typing-only-imports-0.1.1.tar.gz (5.4 kB view hashes)

Uploaded Source

Built Distribution

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