Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore-missing-import option does not work on false positive "Library stubs not installed for X" #10632

Closed
programmingwithalex opened this issue Jun 11, 2021 · 25 comments
Labels
bug mypy got something wrong

Comments

@programmingwithalex
Copy link

Bug Report

I was recommended to post a new issue that is very close to this one. I am specifically getting this error concerning the pyyaml package.

When running

mypy . --ignore-imports

I am getting an error saying

main.py:1: error: Library stubs not installed for "yaml" (or incompatible with Python 3.9)
main.py:1: note: Hint: "python3 -m pip install types-PyYAML"
main.py:1: note: (or run "mypy --install-types" to install all missing stub packages)
main.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

To Reproduce

I've created a repo demonstrating the issue. To reproduce the error:

  1. Go to the Actions tab
  2. Select the Python Windows mypy workflow
  3. Click Run Workflow and run it on the main branch

There are only three files:

  1. requirements.txt
pyyaml
  1. main.py
import yaml
  1. action.yml (GitHub Action workflow file)
    • builds a Docker container with windows-latest as the base image and runs mypy . --ignore-missing-imports on the top-level directory

Expected Behavior

  • --ignore-missing-imports should ignore the error

Actual Behavior

  • error is shown and there is no alternative flag to use to ignore it

Your Environment

  • Mypy version used: 0.902
  • Mypy command-line flags: --ignore-missing-imports
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.9.5
  • Operating system and version: Docker base image windows-latest
@programmingwithalex programmingwithalex added the bug mypy got something wrong label Jun 11, 2021
@anders-kiaer
Copy link

We faced the same issue in our CI. Ended up adding types-pyyaml to our test requirements after reading this blog post, more specifically:

Note: Even if you are using --ignore-missing-imports, mypy will ask to install stubs for packages that had bundled stubs in previous mypy releases. This helps you avoid losing type checking precision from missing stubs when upgrading to mypy 0.900.

@jeffmm
Copy link

jeffmm commented Jun 14, 2021

I thought the entire reason for this update was for consistent treatment of stubs for third party packages. So why was it decided to introduce a breaking change that causes --ignore-missing-imports to error inconsistently for third party packages?

@epwalsh
Copy link

epwalsh commented Jun 16, 2021

I have a similar issue, but I'm not sure if it's exactly the same. I'm using mypy==0.902 and I have both requests and types-requests installed. But I still get

error: Cannot find implementation or library stub for module named "requests"

when mypy is ran both with and without the --ignore-missing-imports option.

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 17, 2021

This error is a bit unfortunate, but it's by design. We made this error a little tricky to ignore, since otherwise many mypy users would silently lose type checking precision when upgrading to mypy 0.9xx: mypy would silently stop using various third-party library stubs that were previously bundled with mypy, resulting in a loss in type checking precision. By making the error hard to ignore, users at least will make an explicit decision to not install the type stubs.

If you don't want to install the types-* package, for some reason, the error can be ignored by having a module-level ignore_missing_imports setting in mypy.ini:

[mypy-yaml.*]
ignore_missing_imports = True

Usually I'd recommend against doing this, since type checking results would then be worse than with older mypy versions.

There's an idea about how to make installing type stubs easier: #10600 (comment) Maybe it would help with some of this? By using --install-types --non-interactive all these hard-to-ignore errors would then go away.

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 18, 2021

@epwalsh Your issue seems different. If you have types-requests installed, you shouldn't get that error. Can you open a separate issue about this?

@programmingwithalex
Copy link
Author

@JukkaL I am trying to use the flag --install-types but I am getting an error saying Error: no mypy cache directory (you must enable incremental mode). I have tried adding flags to both turn off incremental mode and disable the cache

mypy . --install-types --ignore-missing-imports --disallow-untyped-defs --disallow-incomplete-defs --show-error-codes --raise-exceptions --no-incremental --cache-dir=nul

but I am still getting the same error.

@JelleZijlstra
Copy link
Member

You have to first run mypy in incremental mode, then run --install-types. This probably can be documented better.

@programmingwithalex
Copy link
Author

@JelleZijlstra If I'm running a CI pipeline though with GitHub Actions, if I run mypy first in incremental mode it will fail saying it's missing type stubs. Could you share your approach on how you handle it in your CI pipeline?

@JelleZijlstra
Copy link
Member

I'm not running mypy in any pipeline like that, but if I were you I would first run mypy locally to get the list of stub packages I need, then put those in my requirements list (requirements.txt, setup.py, or whatever else you're using) so that they're available in CI.

@programmingwithalex
Copy link
Author

programmingwithalex commented Jun 18, 2021

That works fine if I'm running mypy directly in the workflow. However, if mypy is being run as part of GitHub Action, it will fail to find the stub packages installed.

It would be very beneficial to run --install-types all in one step, instead of having to do a two-step process.

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 18, 2021

#10669 will support installing types and type checking in one step, by using mypy --install-types --non-interactive src/.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Oct 31, 2021

If there are further changes mypy should make, please open a new issue. Fwiw, I recommend adding stubs to your test requirements files.

@paulaceccon
Copy link

paulaceccon commented Jan 19, 2022

I have a similar issue, but I'm not sure if it's exactly the same. I'm using mypy==0.902 and I have both requests and types-requests installed. But I still get

error: Cannot find implementation or library stub for module named "requests"

when mypy is ran both with and without the --ignore-missing-imports option.

I have the same issue and can't find a workaround or understand why I'm getting it.

project/lp_recognition.py:11: error: Library stubs not installed for "requests.exceptions" (or incompatible with Python 3.9)  [import]
Found 2 errors in 1 file (checked 7 source files)

> mypy --install-types
Installing missing stub packages:
/mnt/28a8e77e-d1f1-438f-928e-82a52440db09/eyecue_projects/eyecue-tracker/venv/bin/python -m pip install types-requests

Install? [yN] y

Requirement already satisfied: types-requests in /mnt/28a8e77e-d1f1-438f-928e-82a52440db09/eyecue_projects/eyecue-tracker/venv/lib/python3.9/site-packages (2.26.2)

@hauntsaninja
Copy link
Collaborator

I'm not able to reproduce, could you try upgrading to the latest version of types-requests (or possibly upgrading to mypy 0.931)?

I agree that this whole situation is pretty unfortunate and that --ignore-missing-imports would ideally just make this go away.

@paulaceccon
Copy link

paulaceccon commented Jan 20, 2022

Neither worked:

-   repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.931
    hooks:
    -   id: mypy
        verbose: true
        args: [--show-error-codes]

[INFO] Initializing environment for https://github.com/pre-commit/mirrors-mypy.
[INFO] Installing environment for https://github.com/pre-commit/mirrors-mypy.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...

python3 -m pip install --upgrade types-requests

project/lp_recognition.py:8: error: Library stubs not installed for "requests" (or incompatible with Python 3.9)  [import]
project/lp_recognition.py:8: note: Hint: "python3 -m pip install types-requests"
project/lp_recognition.py:8: note: (or run "mypy --install-types" to install all missing stub packages)
project/lp_recognition.py:8: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
project/lp_recognition.py:10: error: Library stubs not installed for "requests.exceptions" (or incompatible with Python 3.9)  [import]
Found 2 errors in 1 file (checked 10 source files)

Only was to:

import requests  # type: ignore[import]
from loguru import logger
from requests.exceptions import HTTPError  # type: ignore[import]

Which kinda starts polluting the code.

@hauntsaninja
Copy link
Collaborator

Are you sure you're installing types-requests in the right environment? I think you need to specify something like: https://pre-commit.com/#config-additional_dependencies
(I personally don't really recommend using pre-commit for mypy, since there are a number of pitfalls, but I get the desire to)

@paulaceccon
Copy link

Perfect, my bad @hauntsaninja. This solved it!

-   repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.931
    hooks:
    -   id: mypy
        verbose: true
        args: [--show-error-codes]
        additional_dependencies: ['types-requests']

@martimors
Copy link

martimors commented Mar 11, 2022

Still having this error. The solution using additional_dependencies by @paulaceccon changed nothing.

My config looks like this:

  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.931
    hooks:
      - id: mypy
        args: [--ignore-missing-imports, --show-error-codes]

... yet, when running the mypy hook, the following output is seen:

mypy.....................................................................Failed
- hook id: mypy
- exit code: 1

app/log.py:5: error: Library stubs not installed for "yaml" (or incompatible with Python 3.8)  [import]
app/log.py:5: note: Hint: "python3 -m pip install types-PyYAML"
app/log.py:5: note: (or run "mypy --install-types" to install all missing stub packages)
app/log.py:5: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 32 source files)


The package types-PyYAML is installed in the environment.

My theory is that there is a bug, because this is only the case for the import errors where the "note" lines are logged. If I remove --ignore-missing-imports, I get many more errors, but not the one with the suggestion to install a package.

Anyone else facing this and if so any idea how to work around it?

EDIT:

By adding the command args --install-types and --non-interactive, the output changes to the following:

mypy.....................................................................Failed
- hook id: mypy
- exit code: 1

Installing missing stub packages:
/home/uid/.cache/pre-commit/repo9jhvdgoh/py_env-python3.8/bin/python -m pip install types-PyYAML

Requirement already satisfied: types-PyYAML in ./__pypackages__/3.8/lib (6.0.4)

app/log.py:5: error: Library stubs not installed for "yaml" (or incompatible with Python 3.8)  [import]
app/log.py:5: note: Hint: "python3 -m pip install types-PyYAML"
app/log.py:5: note: (or run "mypy --install-types" to install all missing stub packages)
app/log.py:5: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 32 source files)

So the requirement is definitely met, but somehow mypy doesn't see that. I should also mention that I don't have this issue for requests and types-requests, which I see others had issues with in this discussion...

EDIT 2:

Now I've also confirmed that the issue is not related to pre-commit, because I have the same issue when running mypy from the commandline directly.

This is a desperate cry for help: Help!

@hauntsaninja
Copy link
Collaborator

additional_dependencies: ['types-PyYAML'] should work.

There's a really shitty situation where some stub packages are special cased to ignore ignore-missing-imports for various reasons. #10619

If you can't get types-PyYAML to be installed in the right environment, you can always try:

[mypy-yaml.*]
ignore_missing_imports = True

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Mar 11, 2022

Also I see mention of __pypackages__ in your output. I'm not sure where that's coming from, but PEP 582 is not accepted and mypy doesn't have support for it yet.

@martimors
Copy link

additional_dependencies: ['types-PyYAML'] should work.

There's a really shitty situation where some stub packages are special cased to ignore ignore-missing-imports for various reasons. #10619

If you can't get types-PyYAML to be installed in the right environment, you can always try:

[mypy-yaml.*]
ignore_missing_imports = True

That's it! Thank you 🙏

@elsiehupp
Copy link

I found my way here because mypy couldn't find types-requests when being invoked as a pre-commit hook. (types-requests was installed in both the user Python environment and the Poetry virtual environment, so this shouldn't have been happening, right?)

Anyway, this is the section from my .pre-commit-config.yaml that ended up working:

-   repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.942
    hooks:
    -   id: mypy
        args: ["--install-types", "--non-interactive", "--ignore-missing-imports"]

I hope this is helpful!

@Garrett-R
Copy link

you can always try:

[mypy-yaml.*]
ignore_missing_imports = True

I believe this is for using the mypy.ini file. For those using pyproject.toml, you can do this:

[[tool.mypy.overrides]]
module = "yaml"
ignore_missing_imports = true

where module can also be a comma-separated list.

@Garrett-R
Copy link

I'm also suggesting here that this special-casing (ignoring the ignore_missing_imports for certain packages) be removed.

@krishnans2006
Copy link

If you don't want to install the types-* package, for some reason, the error can be ignored by having a module-level ignore_missing_imports setting in mypy.ini:

I tried this, but it didn't work? Is the mypy.ini file not recognized by a pre-commit hook?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests