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

Uploaded Source

Built Distribution

File details

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

File metadata

File hashes

Hashes for flake8-typing-only-imports-0.1.10.tar.gz
Algorithm Hash digest
SHA256 54b0aaadf980d5928949d4a4c7c9c2e26d252f7a7f887faa18f04556241da6ba
MD5 7c389a3e7697099c507bc76cc10ba744
BLAKE2b-256 f3b1c6478cbd35ab1facf89935737e985d4a6e5e308b173fc4f88eed979f9f0e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flake8_typing_only_imports-0.1.10-py3-none-any.whl
Algorithm Hash digest
SHA256 aa35d99f1c249326222ebb7ac85d1da404c1b0433cd9ca87a29e1b55ac5ff737
MD5 27686090b7211aee95ed7ad25e5cc92f
BLAKE2b-256 4e1580a276ebe3b0ffec8f125db8b04d2a2bb7e4fb1ae7d7980bb881a8cac1e2

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