flake8 plugin which checks that typing imports are properly guarded

Overview

Build Status Azure DevOps coverage pre-commit.ci status

flake8-typing-imports

flake8 plugin which checks that typing imports are properly guarded

installation

pip install flake8-typing-imports

flake8 codes

Code Description
TYP001 guard import by TYPE_CHECKING
TYP002 @overload is broken in <3.5.2
TYP003 Union[Match, ...] or Union[Pattern, ...] must be quoted in <3.5.2
TYP004 NamedTuple does not support methods in 3.6.0
TYP005 NamedTuple does not support defaults in 3.6.0
TYP006 guard typing attribute by quoting

rationale

unfortunately, the typing module has been pretty unstable -- it has seen api changes in 3.5.0, 3.5.2, 3.5.3, 3.5.4, 3.6.0, 3.6.1, 3.6.2, 3.7.0, and 3.7.2!

depending on your supported version of python, you may need to guard your imports by if TYPE_CHECKING: (3.5.2+) or if False: if the things you are importing aren't available in all the pythons you support.

as it's pretty difficult to keep track of what version things changed and you can't always test against particular patch versions of python, this plugin helps you statically check this automatically!

# default / --min-python-version 3.5.0
from typing import Type  # TYP001
# default / --min-python-version 3.5.0
if False:
    from typing import Type  # OK!
# default / --min-python-version 3.5.0
from typing import overload  # TYP002
# default / --min-python-version 3.5.0
import sys
from typing import overload  # OK!
if sys.version_info < (3, 5, 2):
    def overload(f):
        return f
# default / --min-python-version 3.5.0
def foo(bar: Union[Match, str]) -> None: pass  # TYP003
def foo(bar: "Union[Match, str]") -> None: pass  # OK!

def foo(bar: Union[Pattern, str]) -> None: pass  # TYP003
def foo(bar: "Union[Pattern, str]") -> None: pass  # OK!
# --min-python-version 3.6.0
class NT(NamedTuple):
    x: int = 5  # TYP005

    def f(self) -> int:  # TYP004
        return self.x + 4
# --min-python-version 3.7.0
from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from typing import OrderedDict  # OK!

configuration

this plugin has a single configuration point (beyond those provided by flake8) which is the --min-python-version option.

by default, this option is 3.5.0. this includes all versions of python which have the typing module present.

you can also set this option in the flake8 configuration if you don't want to use the commandline:

[flake8]
min_python_version = 3.6.2

if a >= is set for python_requires in setup.cfg, that value will be used:

# setup.cfg setuptools metadata

[options]
python_requires = >=3.6

as a pre-commit hook

See pre-commit for instructions

Sample .pre-commit-config.yaml:

-   repo: https://github.com/pycqa/flake8
    rev: 3.7.7
    hooks:
    -   id: flake8
        additional_dependencies: [flake8-typing-imports==1.11.0]
Owner
Anthony Sottile
@pre-commit @pytest-dev @tox-dev
Anthony Sottile
A simple program which checks Python source files for errors

Pyflakes A simple program which checks Python source files for errors. Pyflakes analyzes programs and detects various errors. It works by parsing the

Python Code Quality Authority 1.2k Dec 30, 2022
Design by contract for Python. Write bug-free code. Add a few decorators, get static analysis and tests for free.

A Python library for design by contract (DbC) and checking values, exceptions, and side-effects. In a nutshell, deal empowers you to write bug-free co

Life4 473 Dec 28, 2022
❄️ A flake8 plugin to help you write better list/set/dict comprehensions.

flake8-comprehensions A flake8 plugin that helps you write better list/set/dict comprehensions. Requirements Python 3.6 to 3.9 supported. Installation

Adam Johnson 398 Dec 23, 2022
The mypy playground. Try mypy with your web browser.

mypy-playground The mypy playground provides Web UI to run mypy in the sandbox: Features Web UI and sandbox for running mypy eas

Yusuke Miyazaki 57 Jan 02, 2023
Flake8 plugin for managing type-checking imports & forward references

flake8-type-checking Lets you know which imports to put in type-checking blocks. For the imports you've already defined inside type-checking blocks, i

snok 67 Dec 16, 2022
OpenStack Hacking Style Checks. Mirror of code maintained at opendev.org.

Introduction hacking is a set of flake8 plugins that test and enforce the OpenStack StyleGuide Hacking pins its dependencies, as a new release of some

Mirrors of opendev.org/openstack 224 Jan 05, 2023
The official GitHub mirror of https://gitlab.com/pycqa/flake8

Flake8 Flake8 is a wrapper around these tools: PyFlakes pycodestyle Ned Batchelder's McCabe script Flake8 runs all the tools by launching the single f

Python Code Quality Authority 2.6k Jan 03, 2023
A plugin for Flake8 that checks pandas code

pandas-vet pandas-vet is a plugin for flake8 that provides opinionated linting for pandas code. It began as a project during the PyCascades 2019 sprin

Jacob Deppen 146 Dec 28, 2022
Flake8 Type Annotation Checking

flake8-annotations flake8-annotations is a plugin for Flake8 that detects the absence of PEP 3107-style function annotations and PEP 484-style type co

S. Co1 118 Jan 05, 2023
Static type checker for Python

Static type checker for Python Speed Pyright is a fast type checker meant for large Python source bases. It can run in a “watch” mode and performs fas

Microsoft 9.2k Jan 03, 2023
Flake8 extension for checking quotes in python

Flake8 Extension to lint for quotes. Major update in 2.0.0 We automatically encourage avoiding escaping quotes as per PEP 8. To disable this, use --no

Zachary Heller 157 Dec 13, 2022
flake8 plugin to run black for checking Python coding style

flake8-black Introduction This is an MIT licensed flake8 plugin for validating Python code style with the command line code formatting tool black. It

Peter Cock 146 Dec 15, 2022
Performant type-checking for python.

Pyre is a performant type checker for Python compliant with PEP 484. Pyre can analyze codebases with millions of lines of code incrementally – providi

Facebook 6.2k Jan 04, 2023
mypy plugin for loguru

loguru-mypy A fancy plugin to boost up your logging with loguru mypy compatibility logoru-mypy should be compatible with mypy=0.770. Currently there

Tomasz Trębski 13 Nov 02, 2022
PEP-484 typing stubs for SQLAlchemy 1.4 and SQLAlchemy 2.0

SQLAlchemy 2 Stubs These are PEP-484 typing stubs for SQLAlchemy 1.4 and 2.0. They are released concurrently along with a Mypy extension which is desi

SQLAlchemy 139 Dec 30, 2022
Pylint plugin to enforce some secure coding standards for Python.

Pylint Secure Coding Standard Plugin pylint plugin that enforces some secure coding standards. Installation pip install pylint-secure-coding-standard

Nguyen Damien 2 Jan 04, 2022
An enhanced version of the Python typing library.

typingplus An enhanced version of the Python typing library that always uses the latest version of typing available, regardless of which version of Py

Contains 6 Mar 26, 2021
Code audit tool for python.

Pylama Code audit tool for Python and JavaScript. Pylama wraps these tools: pycodestyle (formerly pep8) © 2012-2013, Florent Xicluna; pydocstyle (form

Kirill Klenov 967 Jan 07, 2023
Convert relative imports to absolute

absolufy-imports A tool and pre-commit hook to automatically convert relative imports to absolute. Installation $ pip install absolufy-imports Usage a

Marco Gorelli 130 Dec 30, 2022
Utilities for refactoring imports in python-like syntax.

aspy.refactor_imports Utilities for refactoring imports in python-like syntax. Installation pip install aspy.refactor_imports Examples aspy.refactor_i

Anthony Sottile 20 Nov 01, 2022