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?
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.
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.
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!
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.
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....
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With