Darko Cerdic
Darko Cerdic

Reputation: 973

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?

Upvotes: 21

Views: 16702

Answers (4)

Santiago Toscanini
Santiago Toscanini

Reputation: 354

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:

Upvotes: 11

alecxe
alecxe

Reputation: 473873

Other mentioned mypy which is used to check the type hints, but it does not enforce them. In order to be more strict about the type annotations, you need to enable the following configuration settings:

disallow_untyped_calls (Boolean, default False) disallows calling functions without type annotations from functions with type annotations.

disallow_untyped_defs (Boolean, default False) disallows defining functions without type annotations or with incomplete type annotations.

Or, you can set these settings as command-line arguments instead.


FYI, PyLint does not yet support forcing the type annotations, but there is a feature request. And, I don't see anything related on the flake8/pyflakes front - consider opening a feature request.

Upvotes: 4

Felix
Felix

Reputation: 6359

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.

...

Upvotes: 16

karlson
karlson

Reputation: 5433

mypy can be used to verify type hints for Python programs. The documentation can be found here and is quite comprehensive.

Upvotes: 2

Related Questions