blokeley
blokeley

Reputation: 7045

mypy cannot find module 'dropbox'

I have a small codebase to back up Dropbox Business files, and am trying to use mypy to help me use the rather unpythonic Dropbox Python SDK.

I have installed mypy, and it is working.

However, mypy raises a warning for the following line:

import dropbox

The warning is "Cannot find module named 'dropbox'".

It seems that Dropbox's SDK generator, called Stone, should generate compatible stub files (which in this case would be called dropbox.pyi).

But there is no dropbox.pyi in site-packages/dropbox, where mypy would look for it.

How can I get type checking for the dropbox package working?

Thanks in advance.

Versions:

Upvotes: 4

Views: 2164

Answers (2)

Greg
Greg

Reputation: 16930

The Dropbox Python SDK doesn't include the .pyi files as part of the Dropbox Python SDK, so you'd have to build them, and set MYPYPATH.

To do so from the Dropbox API spec:

# We first need the Dropbox stone and public stone specs:
git clone [email protected]:dropbox/stone.git
git clone [email protected]:dropbox/dropbox-api-spec.git

# Next we need to install ply (used for running stone)
pip install ply

# Use stone to build the type stubs
PYTHONPATH=stone python -m stone.cli python_type_stubs mypy_stubs/dropbox dropbox-api-spec/*.stone

# include __init__.py files
touch mypy_stubs/__init__.py mypy_stubs/dropbox/__init__.py

# now mypy succeeds when using MYPYPATH to reference new .pyi files
MYPYPATH=mypy_stubs mypy project.py

Alternatively, you can build the stubs from the Dropbox Python SDK:

# This assumes the following python modules are already installed: six, ply
git clone [email protected]:dropbox/dropbox-sdk-python.git
cd dropbox-sdk-python/
git submodule init
git submodule update
PYTHONPATH=./stone python -m stone.cli python_type_stubs mypy_stubs/dropbox spec/*.stone

Upvotes: 1

Michael0x2a
Michael0x2a

Reputation: 63998

I'm unfamiliar with the Dropbox API, but the impression I'm getting is that the user is responsible for running that file you found if they want stubs.

Once you do so, you have two options, as of time of writing:

  1. Copy the stubs you generated to some folder and set the MYPYPATH environment variable to point to that folder. (It would probably be a bad idea to store the stubs in site-packages + set MYPYPATH to point to your entire site-packages folder since you would get lots of spurious error messages when mypy attempts to parse packages that are completely untyped.)

    My recommendation is to version-control the generated stubs within your project and set up your build process to automatically set that environment variable if necessary.

  2. Submit a pull request containing those stubs to typeshed, the repository of type stubs used by various type checkers, including mypy. You would first need to get permission from the developers of the Dropbox API to include the stubs.

    (I suspect they'll probably say yes, unless the API is still in alpha/beta status and is prone to change).

Upvotes: 1

Related Questions