Alex Altair
Alex Altair

Reputation: 3715

Why is mypy finding "no type hints or library stubs" for any of my imports?

I'm working on a code base that has a lot of type hints written by a previous developer. At some point I noticed that these hints were not getting type-checked, and that I needed to add a step to the build if I wanted them checked. I'm comfortable in python but have never used type hints, so I did a lot of reading about them, and I still have a lot to understand. Eventually I conclude that mypy is the main program used to type-check these type hints. So I pip installed mypy and ran mypy .. I got a lot of errors like these;

error: Skipping analyzing 'setuptools': found module but no type hints or library stubs
error: Skipping analyzing 'numpy': found module but no type hints or library stubs
error: Skipping analyzing 'tensorflow.compat.v1': found module but no type hints or library stubs
error: Skipping analyzing 'tensorflow': found module but no type hints or library stubs

I was quite surprised that these were considered errors by default, because everything I have read about type hinting emphasizes that they are optional. So my first question is, why does mypy treat the above as show-stopping errors rather than simple warnings?

Then I google the error message and find this page of the mypy documentation. This is arguably pretty clear, but what confuses me is that it seems to indicate that this error is a big problem that should be resolved. It gives multiple options for resolving it of increasing effort levels before they finally tell you the CLI flag that will silence all the errors. Surely most projects would import libraries that don't use type hints, and would need to use this flag?

Because of these clues, I decide that I should probably keep trying, and look for the packages. The next thing that confused me is that it seems to be failing to find type hints for all of my imports. I don't expect more obscure libraries to have written type hints, but surely somebody has written them for numpy, or pytest? The documentation says I should look for the packages by appending types- to the main package name, so I run pip install types-numpy, but it doesn't find anything.

Now I'm starting to wonder if maybe my machine is failing to connect to some kind of service, like whatever "typeshed" is? Pip can find packages just fine, but maybe typeshed is a separate protocol that is currently down or something? Or does it have to do with the fact that I'm using an older version of python (3.6)? I decide to manually search for the packages here, but I don't see almost any that start with types-.

So my overall question is, is something weird going on, or should I just run mypy with the --ignore-missing-imports flag and move on?

Upvotes: 30

Views: 69151

Answers (1)

Coder94
Coder94

Reputation: 1045

This error is because mypy was not able to find the module you are trying to import, whether it comes bundled with type hints or not. Also, ensure that your imports doesn't have a typo.

You can resolve the issue by running the suggested pip command or commands. Alternatively, you can use --install-types to install all known missing stubs: mypy --install-types

Upvotes: 18

Related Questions