Notbad
Notbad

Reputation: 6296

Run mypy on all Python files of a project

How can I run mypy on all .py files in a project? I have seen I can specify a module to run mypy on but not something to specify a file mask or something like this.

Upvotes: 29

Views: 34964

Answers (2)

Shinsuke Hamasho
Shinsuke Hamasho

Reputation: 71

In case you want to check all files, including not annotated functions, you have to specify --check-untyped-defs option.

Normally, mypy only checks type-annotated functions (functions which arguments or return type are type-annotated).

So if you have functions like those, mypy . only shows errors in foo().

# main.py
def foo() -> None:
    print(1 + "a")  # show error

def bar():
    print(1 + "a")  # **DON'T** show error
$ mypy .
main.py:6: error: Unsupported operand types for + ("int" and "str")
Found 1 error in 1 file (checked 1 source file)

But if you specify --check-untyped-defs, mypy shows all errors even if the functions are not annotated.

$ mypy --check-untyped-defs .
main.py:2: error: Unsupported operand types for + ("int" and "str")
main.py:6: error: Unsupported operand types for + ("int" and "str")
Found 2 errors in 1 file (checked 1 source file)

Official documentation: https://mypy.readthedocs.io/en/stable/common_issues.html#no-errors-reported-for-obviously-wrong-code

Upvotes: 3

Michael0x2a
Michael0x2a

Reputation: 64248

If all of your Python files live within a specific module or directory, then just running mypy folder_name is fine.

You can also pass in multiple paths if you prefer to target specific files or modules. For example: mypy a.py b.py some_directory.

The documentation has some more details and caveats.

Upvotes: 27

Related Questions