3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pylance(pyright)でfrom requests.packagesに対して「reportMissingModuleSource」の警告が出る

Last updated at Posted at 2021-02-20

環境

  • Language Server version: 2021.2.3
  • OS : Windows
  • Python version 3.9

内容

インポートは正常に機能しますがfrom requests.packages.urllib3.util.retry import Retryに対して「reportMissingModuleSource」の警告が出てしまう

解決方法

from requests.packages.urllib3.util.retry import Retry
from urllib3.util.retry import Retryに修正するだけ

再現方法

仮想環境を作成 requestsモジュールをインポート

python -m venv venv
.\venv\Scripts\activate.bat
pip install requests

コード

from requests.packages.urllib3.util.retry import Retry

r = Retry
print(r)

実行

>python test.py
<class 'urllib3.util.retry.Retry'>

インポートは正常にできていることがわかる

「reportMissingModuleSource」の警告

pylance(pyright)では以下のような警告が出てしまう
image.png

原因調査

ソースを見てみると
requests/packages/urllib3/util/retry.pyがあると期待してたが
実際にはrequests/packages.pyのみで以下の記述があった

packages.py
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?

これでrequests.packages.urllib3.urllib3.に読み替えているのでimportは正常におこなえてpylanceがrequests.packages.urllib3.util.retryの実際のソースを見つけることはが出来きずに警告が出てい理由が分かる

なので解決方法としてはどうせ読み替えられるのでfrom urllib3.util.retry import Retryでインポートするのがいいのかもしれません。

ほかに解決策があれば

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?