boatcoder
boatcoder

Reputation: 18117

Is it possible to reduce/eliminate MyPy errors for system level code with a command line option?

Running MyPy over my code, These errors seem impossible to disable with a command line option. I don't want to mark the code to be ignored so that when they do get type hints things get better. I just want to eliminate this from files in the site-packages folders, not in any of my code.

from django.db import connection
from django.conf import settings
from django.test import TestCase, TransactionTestCase
from django.utils import timezone

All of these suffer from this "error"

error: Skipping analyzing 'django.conf': found module but no type hints or library stubs

I should be clear that I don't want to ignore the non-existence of this code, just the non-existence of the type hints.

Upvotes: 0

Views: 2116

Answers (2)

Michael0x2a
Michael0x2a

Reputation: 64228

You can suppress all warnings from a 3rd party module by creating a mypy.ini config file that suppresses import errors on a per-module basis, like so:

[mypy]
# The [mypy] section is for any global mypy configs you want to set.
# Per-module configs are listed below.

[mypy-django.db.*]
ignore_missing_imports = True

[mypy-django.conf.*]
ignore_missing_imports = True

[mypy-django.test.*]
ignore_missing_imports = True

[mypy-django.utils.*]
ignore_missing_imports = True

This is kind of like running mypy --ignore-missing-module your_code, except that we only ignore the listed django modules (and their submodules).

You could just have a single section for [mypy-django.*] instead of listing out all of the above, of course, but this may end up accidentally hiding any misuses of django that the django type hint stubs mentioned in the comments above may have otherwise been able to catch.

For more details on the options you have for handling these "did not find type hints" errors, see https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-type-hints-for-third-party-library.

Upvotes: 1

dstromberg
dstromberg

Reputation: 7187

This is what I do:

mypy --disallow-untyped-calls --ignore-missing-imports file1.py file2.py

Upvotes: 1

Related Questions