Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make flake8 check for type hints in the source

I'm starting a new python project (python 3.5+) and I would like to enforce type hints in the entire codebase. Is there a way to do that with flake8 or any other tool?

like image 338
Darko Cerdic Avatar asked Apr 03 '17 14:04

Darko Cerdic


People also ask

What is flake8 and how to use it?

Subscribe to our YouTube Channel! Flake8 is a Python library that wraps PyFlakes, pycodestyle and Ned Batchelder’s McCabe script. It is a great toolkit for checking your code base against coding style (PEP8), programming errors (like “library imported but unused” and “Undefined name”) and to check cyclomatic complexity.

How do I increase the verbosity of a flake8 code?

check source files for errors. check source code against PEP8 conventions. perform McCabe complexity check. usage: flake8 [options] file file ... -v, --verbose Print more information about what is happening in flake8. This option is repeatable and will increase verbosity each time it is repeated.

How do I run flake8 checks in Python?

See the full list of options. Just add # noqa in the end of the line. The example below is a .travis.yml file from a project of mine. You can run flake8 checks very easily just by adding the commands inside the script list. That’s it!

What is flake8 delegated check?

In Flake8 2.x, Flake8 delegated check running to pep8. In 3.0 Flake8 takes on that responsibility. This has allowed for simpler handling of the --jobs parameter (using multiprocessing) and simplified our fallback if something goes awry with concurrency. At the lowest level we have a FileChecker.


2 Answers

Take a look at mypy.

From the website:

Mypy is an experimental optional static type checker for Python that aims to combine the benefits of dynamic (or "duck") typing and static typing. Mypy combines the expressive power and convenience of Python with a powerful type system and compile-time type checking. Mypy type checks standard Python programs; run them using any Python VM with basically no runtime overhead.

EDIT

Actually mypy is a type checker, so by default it only checks if there are errors related to types that are hinted or inferred. To also make it report non-hinted types, you can use additional command line switches as documented here.

--disallow-untyped-defs reports an error whenever it encounters a function definition without type annotations.

--check-untyped-defs is less severe than the previous option – it type checks the body of every function, regardless of whether it has type annotations. (By default the bodies of functions without annotations are not type checked.) It will assume all arguments have type Any and always infer Any as the return type.

--disallow-untyped-calls reports an error whenever a function with type annotations calls a function defined without annotations.

...

like image 193
Felix Avatar answered Oct 04 '22 18:10

Felix


There is a plugin for flake8 called flake8-annotations

You can install it using

pip install flake8-annotations

So:

def test_function(x, y):
  pass

Will output:

./test.py:2:9: ANN001 Missing type annotation for function argument 'x'
./test.py:3:9: ANN001 Missing type annotation for function argument 'y'
./test.py:1:24: ANN201 Missing return type annotation for public function

And this is the correct version:

def test_function(x: int, y: int) -> None:
like image 27
Santiago Toscanini Avatar answered Oct 04 '22 18:10

Santiago Toscanini