Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a python linter that checks types according to type hints?

I'm looking for a Python linter, that can check types usage according to the type hints in the code.
The purpose is to run a single check that verifies style, logic, and type errors.
I need to run this on CI server, and as a file watcher during development.
For example, I need this code to output an error for passing the wrong type argument -

def double(x: int):
    return x * 2


result = double('hello')

I've checked the documentation of PyLint and flake8, and couldn't find any support for type checking.
With PyLint I also verified there are no errors when checking the above code.

like image 623
Gyro Avatar asked Oct 06 '19 13:10

Gyro


People also ask

Does Python check type hints?

Python does not enforce the type hints. You can still change types at will in Python because of this. However some integrated development environments, such as PyCharm, support type hinting and will highlight typing errors. You can also use a tool called Mypy to check your typing for you.

Does Python have type inference?

Python doesn't do static type inference, because it wants to let you do things that are impossible under such a scheme.

What library is widely used for type checking in Python?

One of the most widely used type checkers in use for Python is mypy, so I recommend that you install it before reading the rest of the article.


1 Answers

Yes, there is, it's called mypy

like image 54
Nicolas Quiroz Avatar answered Oct 13 '22 18:10

Nicolas Quiroz