Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is mypy checking files that I've excluded?

Tags:

python

mypy

I'm trying to set up mypy type checking for a project. I want to exclude a bunch of files/directories to start so that we can at least enforce that type checking passes on new code, then we can burn down the exclude list over time. Unfortunately mypy is ignoring my exclude configuration and I can't figure out why.

I've created a mypy.ini config file with the following content:

[mypy]
python_version = 3.8
exclude = /examples/

But when I run mypy --verbose ., it still discovers and errors on files in that directory. The log messages tell me it is seeing my exclude configuration but apparently ignoring it:

LOG:  Mypy Version:           0.812
LOG:  Config File:            mypy.ini
LOG:  Configured Executable:  /Library/Developer/CommandLineTools/usr/bin/python
3
LOG:  Current Executable:     /Library/Developer/CommandLineTools/usr/bin/python
3
LOG:  Cache Dir:              .mypy_cache
LOG:  Compiled:               True
LOG:  Exclude:                /examples/
<snipped>
LOG:  Found source:           BuildSource(path='./examples/fib.py', module='fib', has_text=False, base_dir='/Users/user/a/examples')
LOG:  Found source:           BuildSource(path='./examples/fib_iter.py', module='fib_iter', has_text=False, base_dir='/Users/user/a/examples')
<snipped>
examples/fib.py: error: Duplicate module named 'fib' (also at './examples/a/fib.py')
examples/fib.py: note: Are you missing an __init__.py? Alternatively, consider using --exclude to avoid checking one of them.
Found 1 error in 1 file (errors prevented further checking)

Why is my exclude configuration not working?

like image 251
Eddie Aftandilian Avatar asked Apr 16 '21 19:04

Eddie Aftandilian


2 Answers

You should not use /examples/, instead you should use either examples/ or examples, because the first one is asking to exclude a path at the root of the file system. Meanwhile the other ones declare a local path and mypy can treat folders as files so you can omit the / symbol if you want.

mypy.ini

[mypy]
python_version = 3.8
exclude = examples/

If you change your mypy.ini file to this one, it should work.

like image 72
Bernardo Duarte Avatar answered Sep 20 '22 21:09

Bernardo Duarte


for my case, even though I was correctly excluding the folder, mypy was still checking it because it was being imported in a separate package where mypy was enabled.

So let's say the folder I want to exclude (which is also a package) is called examples. To exclude it, I would need to add the following to the mypy.ini file

[mypy]
python_version = 3.8
exclude = examples/

But this wasn't enough to keep mypy from checking it because, I had a separate package (which mypy was allowed to check) importing the examples folder.

So to fix the issue I also had to set follow_imports = silent in the mypy.ini file like so:

[mypy-examples.*]
follow_imports = skip

This would tell mypy to skip the type check of the examples package whenever it is found imported anywhere else in the codebase.

like image 35
ifedapo olarewaju Avatar answered Sep 19 '22 21:09

ifedapo olarewaju