Open jaraco opened 3 years ago
Looks like this issue goes back to 2013 (python/mypy#175).
Yeah, the plugin doesn't do anything special about module lookups (and probably shouldn't do).
Instead of setting PYTHONPATH, you can try setting mypy_path=libs
in your mypy.ini
. See https://mypy.readthedocs.io/en/stable/config_file.html#confval-mypy_path
Adding mypy_path
doesn't seem to help:
draft $ cat mypy.ini
[mypy]
plugins=mypy_zope:plugin
mypy_path=libs
draft $ env PYTHONPATH=libs venv/bin/python -m mypy interface.py
interface.py:1: error: Skipping analyzing 'zope.interface.interface': found module but no type hints or library stubs
interface.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)
Omitting PYTHONPATH altogether as suggested only results in the plugin failing to load:
draft $ venv/bin/python -m mypy interface.py
Found 1 error in 1 file (checked 1 source file)
mypy.ini:2: error: Error importing plugin 'mypy_zope': No module named 'mypy_zope'
I even tried using an absolute path to libs
in the mypy_path
directive, but that produced the same results.
I also tried setting MYPY_PATH=libs
in the environment, but that was no help either.
Hm, I'm not sure what's your problem now. Stubs for zope.interface
and zope.schema
are included in mypy-zope
, so you should not provide them yourself. Why are you trying to include them?
I'm not trying to provide stubs, only trying to consume them from mypy-zope when mypy-zope is not installed in system-site-packages (only available on sys.path). I'm pretty sure it's an upstream issue with mypy itself failing to load stubs that aren't on some blessed path. I'm trying to include the stubs because the mypy checks fail without the stubs. Maybe I don't understand the question.
Is there a simple way for me to repro the issue?
Would you like a Dockerfile that implements the repro in the OP?
That would work
Here's the Dockerfile:
from jaraco/multipy-tox
RUN echo "[mypy]\nplugins=mypy_zope:plugin" > mypy.ini
RUN echo "from zope.interface.interface import Interface\nclass X(Interface):\n pass" > interface.py
RUN pip install mypy-zope
RUN pip uninstall -y mypy-zope
RUN pip install -t libs --no-deps mypy-zope
CMD env PYTHONPATH=libs mypy interface.py
If you use docker build
on that file, then docker run
the build, you should see the reported error:
test $ docker run @($(docker build -q .).strip()) # xonsh syntax
interface.py:1: error: Skipping analyzing 'zope.interface.interface': found module but no type hints or library stubs
interface.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)
That's xonsh syntax, but assuming you're using a bash-like shell, you can probably do:
$ docker run $(docker build -q .)
The docker example doesn't use any virtualenvs but just installs everything (except mypy-zope) in the system site-packages. You should be able to docker run -it $(docker build -q .) bash
to inspect and interact with the environment.
Oh, I see. I'll try to play with it tomorrow.
So, a bit later than I would've liked, but I did look into this. Apparently mypy only looks for stub dirs in site-packages dir, i.e. ones that are returned by site.getsitepackages()
and site.getusersitepackages()
. And these do not honor PYTHONPATH
.
If you want to dig deeper, the third parameter to SearchPaths
is used for stub lookup https://github.com/python/mypy/blob/master/mypy/modulefinder.py#L626-L629
Probably an upstream issue, but first encountered here, so reporting the details. Running a check on a file importing
zope.interface
will result in an error "Skipping analyzing zope.interface".Even though the stubs are there:
Installing the plugin directly into the virtualenv seems to work fine.