Skip to main content

Flake8 Type Annotation Checks

Project description

flake8-annotations

PyPI - Python Version PyPI Build Status Discord

flake8-annotations is a plugin for Flake8 that detects the absence of PEP 3107-style function annotations and PEP 484-style type comments (see: Caveats).

What this won't do: Check variable annotations (see: PEP 526), respect stub files, or replace mypy's static type checking.

Installation

Install from PyPi with your favorite pip invocation:

$ pip install flake8-annotations

It will then be run automatically as part of Flake8.

You can verify it's being picked up by invoking the following in your shell:

$ flake8 --version
3.7.8 (flake8-annotations: 1.1.1, mccabe: 0.6.1, pycodestyle: 2.5.0, pyflakes: 2.1.1) CPython 3.7.4 on Darwin

Table of Warnings

Function Annotations

ID Description
TYP001 Missing type annotation for function argument
TYP002 Missing type annotation for *args
TYP003 Missing type annotation for **kwargs

Method Annotations

ID Description
TYP101 Missing type annotation for self in method
TYP102 Missing type annotation for cls in classmethod

Return Annotations

ID Description
TYP201 Missing return type annotation for public function
TYP202 Missing return type annotation for protected function
TYP203 Missing return type annotation for secret function
TYP204 Missing return type annotation for special method
TYP205 Missing return type annotation for staticmethod
TYP206 Missing return type annotation for classmethod

Type Comments

ID Description
TYP301 PEP 484 disallows both type annotations and type comments

Caveats for PEP 484-style Type Comments

Function type comments

Function type comments are assumed to contain both argument and return type hints

Yes:

# type: (int, int) -> bool

No:

# type: (int, int)

Python cannot parse the latter and will raise SyntaxError: unexpected EOF while parsing

Mixing argument type comments and function type comments

Support is provided for mixing argument and function type comments, provided that the function type comment use an Ellipsis for the arguments.

def foo(
    arg1,  # type: bool
    arg2,  # type: bool
):  # type: (...) -> bool
    pass

Ellipes are ignored by flake8-annotations parser.

Note: If present, function type comments will override any argument type comments.

Partial type comments

Partially type hinted functions are supported

For example:

def foo(arg1, arg2):
    # type: (bool) -> bool
    pass

Will show arg2 as missing a type hint.

def foo(arg1, arg2):
    # type: (..., bool) -> bool
    pass

Will show arg1 as missing a type hint.

Contributing

Please take some time to read through our contributing guidelines before helping us with this project.

Development Environment

This project uses Pipenv to manage dependencies. With your fork cloned to your local machine, you can create a developer environment using:

$ pipenv sync --dev

Note: flake8-annotations is included in the Pipfile as an editable dependency so it will be included when flake8 is invoked in your developer environment.

A pre-commit installation script and configuration is also provided to create a pre-commit hook so linting errors aren't committed:

$ pipenv run precommit

Testing

A pytest suite is provided for testing across multiple Python environments via tox:

$ pipenv run test

Coverage

Test coverage is provided by pytest-cov via a pipenv script:

$ pipenv run coverage

When running via pipenv, details on missing coverage is provided in the report to allow the user to generate additional tests for full coverage.

e.g.

----------- coverage: platform win32, python 3.7.4-final-0 -----------
Name                                Stmts   Miss Branch BrPart  Cover   Missing
-------------------------------------------------------------------------------
flake8_annotations\__init__.py        108      0     38      0    99%   164
flake8_annotations\checker.py          57      0     30      0   100%
flake8_annotations\enums.py            15      0      0      0   100%
flake8_annotations\error_codes.py      85      0      0      0   100%
-------------------------------------------------------------------------------
TOTAL                                 265      0     68      0    99%

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-annotations-1.1.1.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flake8_annotations-1.1.1-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file flake8-annotations-1.1.1.tar.gz.

File metadata

  • Download URL: flake8-annotations-1.1.1.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.7.5

File hashes

Hashes for flake8-annotations-1.1.1.tar.gz
Algorithm Hash digest
SHA256 c7e5059bd62ff6636fe716b2c97bbe31ab35316da4ba69b57e285fb166ffe015
MD5 074a0e14597abc40d78db71999936b70
BLAKE2b-256 71ffb679aea4ad1c1de498492703825b24b95f27c5897fc18688832c24e3a56e

See more details on using hashes here.

File details

Details for the file flake8_annotations-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: flake8_annotations-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.7.5

File hashes

Hashes for flake8_annotations-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5e0228cc4645931b574cdc73d855916a9fa9ea1d40dbda7b2c20d0b04d004f17
MD5 cf5cf0bc56470d32e75301d9ee103ba9
BLAKE2b-256 c38542379890f7d7cc9b8412fddf0f87bcea0569a303c6b43e0016167a2d8927

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page