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

A flake8 plugin to help you identify which imports to put into type-checking blocks.

Beyond this, it will also help you manage forward references however you would like to.

Installation

pip install flake8-typing-only-imports

Active codes

Code Description
TYO100 Import should be moved to a type-checking block

Deactivated (by default) codes

Code Description
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

If you wish to activate any of these checks, you need to pass them to flake8's select argument (docs).

TYO101 is deactivated by default, mostly because misplaced third party imports don't carry with it the same level of consequence that local imports can have - they will never lead to import circularity issues. Activating TYO101 will mostly help the initialization time of your app.

Please note, TYO200s and TYO300s are mutually exclusive. Don't activate both series. Read on for an in-depth explanation.

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 Python features:

  1. Type checking blocks


    The builtin typing library, as of Python 3.7, provides a TYPE_CHECKING block you can put type annotation imports into (see docs).

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

    When you've got unevaluated imports (in type checking block), or you try to reference not-yet-defined structures, forward references are the answer. They can be used, like this:

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

    And ever since PEP563 was implemented, you also have the option of doing this:

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

    See this excellent stackoverflow response explaining forward references, if you'd like more context.

With that said, the aim of this plugin is to automate the management of type annotation imports (type-checking block import management), and keeping track of the forward references that become necessary as a consequence.

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 👏

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

Uploaded Source

Built Distribution

File details

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

File metadata

  • Download URL: flake8-typing-only-imports-0.1.12.tar.gz
  • Upload date:
  • Size: 10.7 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.12.tar.gz
Algorithm Hash digest
SHA256 adcb86b32201501e2a490922220c18c1bd890ff4b4e8459cf48c0d67f6281a1d
MD5 526533a7f6d26015f7f1e4df061b9213
BLAKE2b-256 b20f808d114b5d5a05a0e7885b75559894c5ae480dfb1276802ae5b14993d5ad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flake8_typing_only_imports-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 87114134d6205794637aa75bea96efd9ab51df2ce1cd0707b8590672fc6af8dd
MD5 930a0a5754cd923850f3bb82e2167a82
BLAKE2b-256 2acc56ae140e8e06d7d23539c6387a779780fb4e2436e7ac4296d873ad0f9a49

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