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

Plugin is still a work in progress

flake8 plugin that helps identify which imports to put into type-checking blocks, and how to adjust your type annotations once imports are moved.

Installation

pip install flake8-typing-only-imports

Codes

Code Description
TYO100 Import should be moved to a type-checking block
TYO101 Third-party import should be moved to a type-checking block
TYO200 Missing 'from __future__ import annotations' import
TYO201 Annotation is wrapped in unnecessary quotes
TYO300 Annotation should be wrapped in quotes
TYO301 Annotation is wrapped in unnecessary quotes

TYO101 is disabled by default because third-party imports usually aren't a real concern with respect to import circularity issues.

TYO2XX and TYO3XX are mutually exclusive as they represent two different ways of solving the same problem. Make sure to ignore or enable just one of the series.

Motivation

Two common issues when annotating large code bases are:

  1. Import circularity issues
  2. Annotating not-yet-defined structures

These problems are largely solved by two features:

  1. Type checking blocks

    from typing import TYPE_CHECKING
    
    if TYPE_CHECKING:
        # this code is not evaluated at runtime
        from foo import bar
    
  2. Forward references

    Which can be used, like this

    class Foo:
        def bar(self) -> 'Foo':
            return Foo()
    

    or since PEP563 was implemented, like this:

    from __future__ import annotations
    
    class Foo:
        def bar(self) -> Foo:
            return Foo()
    

    See this excellent stackoverflow response explaining forward references, for more context.

The aim of this plugin is to automate the management of type annotation imports, and the forward references that then become necessary.

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]

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-typing-only-imports-0.1.11.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

File details

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

File metadata

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

File hashes

Hashes for flake8-typing-only-imports-0.1.11.tar.gz
Algorithm Hash digest
SHA256 2b9359202f56cb84ad729a355361a6c6f8bd8a73910f9c77805a0f848142dbab
MD5 ef9fb72b0b6fe0eae75a2041f4c323bb
BLAKE2b-256 57b33205e9fd1baa8e216819e8d58130ac1501c2967fe8c340d8b09594a77c98

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flake8_typing_only_imports-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 45d9406e86797beebb3c5158786b15bea4bb2b6cb398295500d038295955e8f1
MD5 9fb644217c72282bfc7625c9ad738d17
BLAKE2b-256 4b29a37b8cf49400be9a27ff978b2c48bcc51591b13e8c81a4bdb853dd527e2f

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