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 that flags imports which are exclusively used for type hinting.

Installation

pip install flake8-typing-only-imports

Codes

Code Description
TYO100 Local import '{module}' only used for type hinting
TYO101 Remote import '{module}' only used for type hinting
TYO200 Annotation '{annotation}' needs to be wrapped in quotes to be treated as a forward reference

TYO101 is disabled by default.

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 solves the issue of figuring out which imports to put inside your type-checking blocks 🚀

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.6.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file flake8-typing-only-imports-0.1.6.tar.gz.

File metadata

  • Download URL: flake8-typing-only-imports-0.1.6.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1039-azure

File hashes

Hashes for flake8-typing-only-imports-0.1.6.tar.gz
Algorithm Hash digest
SHA256 1bfa089df5df950e92008dbc1a528fd3da6b16c25abbdee326f2c57fade5eaf0
MD5 d50708680328e092afc4eb093c319684
BLAKE2b-256 7050b3f600138823530fbf3cac034d8f6006a3b0b1a573c44ebefa1f8db341a8

See more details on using hashes here.

File details

Details for the file flake8_typing_only_imports-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_typing_only_imports-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 1ef24ef54a96330d9adc5756661ec70ba6f4e2d456eabdfa768201fc22120a26
MD5 f8af56ffa7831616d5da8e37d854e027
BLAKE2b-256 88fb0dc78075ec38ca43aaa401537ff3fac62398e4dc28ac5ce3c2452422cac2

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