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

'reportMissingModuleSource' warning for requests.packages.* #597

Closed
trallnag opened this issue Nov 11, 2020 · 13 comments
Closed

'reportMissingModuleSource' warning for requests.packages.* #597

trallnag opened this issue Nov 11, 2020 · 13 comments
Assignees

Comments

@trallnag
Copy link

Bug

Pylance shows a reportMissingModuleSource on an valid import statement. And in addition the autocomplete and hinting works, it is just the warning that is wrong. Interestingly all other import statements that import stuff from the same library (requests) work fine.

image

Environment data

  • Language Server version: 2020.11.0
  • OS and version: Linux, Fedora 32
  • Python version 3.9 with Pyenv + virtualenv

The only installed package in the environment is requests. I ensured that the venv is activated:

image

Here is the pip list output:

Package    Version
---------- ---------
certifi    2020.11.8
chardet    3.0.4
idna       2.10
pip        20.2.3
requests   2.24.0
setuptools 49.2.1
urllib3    1.25.11

How to recreate

You must have some kind of virtual environment tool, for example venv. I will use Pyenv and virutalenv in the example:

In shell:

pyenv virtualenv 3.9.0 test_pylance_bug
pyenv activate test_pylance_bug
pip install requests
code .

Ensure tht correct env is activated.

Create a file on VS Code with following content

from requests.packages.urllib3.util import Retry
from requests.adapters import HTTPAdapter

x = Retry()
a = HTTPAdapter()
@judej judej added the needs investigation Could be an issue - needs investigation label Nov 11, 2020
@github-actions github-actions bot removed the triage label Nov 11, 2020
@madewild
Copy link

madewild commented Feb 3, 2021

I can confirm the same issue in this environment:

  • Pylance version: 2021.1.3
  • OS and version: Arch Linux (Kernel: Linux 5.10.11-arch1-1)
  • Python version: 3.9.1 with virtualenv (but no Pyenv)

The command from requests.packages.urllib3.util import Retry is actually a shortcut for from requests.packages.urllib3.util.retry import Retry but both return a reportMissingModuleSource warning.

@heejaechang heejaechang self-assigned this Mar 12, 2021
@heejaechang
Copy link
Contributor

@trallnag Hi, thank you for the reporting.

this is happening since those modules are created dynamically.

requests\packages.py looks like this

import sys

# This code exists for backwards compatibility reasons.
# I don't like it either. Just look the other way. :)

for package in ('urllib3', 'idna', 'chardet'):
    locals()[package] = __import__(package)
    # This traversal is apparently necessary such that the identities are
    # preserved (requests.packages.urllib3.* is urllib3.*)
    for mod in list(sys.modules):
        if mod == package or mod.startswith(package + '.'):
            sys.modules['requests.packages.' + mod] = sys.modules[mod]

# Kinda cool, though, right?

but pyi in typeshed define these statically as expected. that's why you are seeing the warning (since typechecker can't verify py files exist for those modules in pyi)

now, how to solve this. unfortunately, it is not easy for us to solve any dynamic behaviors until we have some kinds of plug in mechanism to inject some dynamic behaviors per libraries.

that being said, if you are using default typecheckingmode (which is off), you can import urllib3 directly instead

from urllib3.util import Retry

since all actual library does is re-exporting urllib3 at runtime, it should work fine at runtime, and you should get auto-complete and etc at design/editing time.

unfortunately, if you use other typecheckingmode, you might get type errors since symbol from typeshed pyi and urllib3 will be considered as different types.

@jakebailey jakebailey changed the title 'reportMissingModuleSource' warning for a single package import 'reportMissingModuleSource' warning for requests.packages.* Mar 19, 2021
@alsitn
Copy link

alsitn commented Aug 20, 2021

To further expand what said before, this works and won't throw another warning for urllib3.disable_warnings(InsecureRequestWarning) not being a declared variable(?)

import requests
from requests.packages import urllib3
from urllib3.exceptions import InsecureRequestWarning
urllib3.disable_warnings(InsecureRequestWarning)

@Alex2218

This comment has been minimized.

@heejaechang
Copy link
Contributor

@alsitn I am not sure what you mean.

@binh-vu
Copy link

binh-vu commented Jun 19, 2022

I encounter this issue when developing a python extension modules (using Rust). As it is an extension created in another language, the source code is not available and hence I have that warning.

I don't think the warning in this case (extension + already provided pyi) can help much as there is no way to fix (no source code). Can we have a work around for this specific scenario?

@erictraut
Copy link
Contributor

If the extension is written in another language, the import should resolve to a binary library file (e.g. a ".so" file on Mac or Linux or a ".pyd" file on Windows). The warning is an indication that pylance would not resolve the import to either a python source (.py) file or a binary library file. Do you see such library files present?

@siggib007
Copy link

I'm getting this behavior while straight importing all of requests
import requests
which produces the following in the problem tab.
Import "requests" could not be resolved from source
The code works fine and I detect no actual problem other than this annoying false positive. Is there anyway to make this go away. I've got the right environment set and I've pip uninstalled /installed multiple times, I've tried running pip as a standalone command pip install requests as a module in py py -m pip install requests and with a fully qualified python.exe "c:\program files\python310\python.exe" -m pip install requests
I've tried to clear the pip cache and to put --force on the install. Always the same outcome. The install works fine, the script runs fine, just pylance complaining about not being able to resolve this.

@pcplusmac
Copy link

I am having this issue, too.
there is no complain in python cell, but when i do:
'import requests'
the error message of "import 'requests' could not be resolved from source pylance(missingModuleSource)"

@omidshojaee
Copy link

I have this issue with random modules.

Modules I've had this warning so far:

django.core.management.utils
django.db

Pylance v2022.8.21.

And yes the virtual environment is active.

joshuacwnewton added a commit to spinalcordtoolbox/spinalcordtoolbox that referenced this issue Oct 11, 2022
PyCharm's linting (and others) complain about this specific import
as though it were an error, because the `requests.urllib3` module
is created dynamically.

But, we can just import `Retry` from `urillib3` directly, as per:
microsoft/pylance-release#597 (comment)
@hitrust
Copy link

hitrust commented Mar 12, 2023

same issue
Pylance v2023.3.20

@heejaechang
Copy link
Contributor

@hitrust can you open a new issue with a log as described in the troubleshooting guide?

this issue is old one and won't be tracked.

@justineakehurst
Copy link

FWIW, I was able to get around this issue by using stubgen from Mypy:

stubgen --output ./typings -m requests.packages.urllib3.util.retry

And having vscode pylance look for the typings directory to override default typeshed .pyi references.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests