24

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.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Notbad
  • 5,936
  • 12
  • 54
  • 100

2 Answers2

23

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.

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
  • 4
    I must add that for this to work we need to add __init__.py where it is needed in order to make mypy scan a folder as a pakage. – Notbad Apr 16 '18 at 21:17
  • 1
    I tried `some_directory/**/*.py` and that looks recursively, but doesn't include *.py in some_directory. – Jeppe Dec 22 '20 at 12:51
3

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