Hey experts, I’m trying to get mypy running in my ...
# general
s
Hey experts, I’m trying to get mypy running in my pants workspace, but for some reason running
./pants check path/to/dir
just gives me
error: Cannot find implementation or library stub for module named
errors for all my imports. Running the actual
./pants run
seems to work just fine. Do I need to setup mypy somehow to correctly understand the structure of my pants workspace? For example explicitly putting the root or something in the options?
w
Not sure without more information about the structure of your repo (e.g. do you have namespace packages? whats your mypy config?) I ran into this with the pants repo proper, but this might point out some ideas? https://sureshjoshi.com/development/pants-plugin-code-quality#typechecking
s
I did see that blog actually, and I indeed don’t have init files: The problem tends to be a missing
__init__.py
But I’m not under the impression I need those in pants generally
Does mypy require them anyway?
w
Pants tries/tends not to impose its will on your code.
mypy
can do whatever it want, however. So, if it's complaining about something, it's probably init or config (using namespace packages, for example). You can try it out by manually pip installing the same mypy version you're using in pants, and running directly. It should give you a similar result
Note that mypy 0.991 enabled namespace packages by default
s
I guess I’m not fully familiar with how Pants sets up the structure under the hood — I know Pants doesn’t require init files when executing, nor the standard package layout that a typical Python package might use by default. I’d assumed mypy in Pants would automatically be configured to use the structure of my pants repo, but it sounds like that’s not true?
Here is a minimal reproducible example: https://github.com/nharada1/pants-mypy-test
Copy code
$ ./pants run src/project/main.py
5
$ ./pants check ::
14:15:27.14 [ERROR] Completed: Typecheck using MyPy - mypy failed (exit code 1).
src/project/main.py:1: error: Cannot find implementation or library stub for module named "project.lib"
src/project/main.py:1: note: See <https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports>
Found 1 error in 1 file (checked 2 source files)



✕ mypy failed.
running the code with pants run works fine, but mypy isn’t able to follow
project.lib
, maybe because the root of the repo is
src
?
w
Creating a /src/project/__init_ .py works
s
hmmm I see, so for a larger project does this mean I must explicitly have init files for all my subdirectories? Do you know if there’s a way to specify to either mypy or pants that the tool should be run with whatever magic pants uses to not have those init files?
I’m sorry, I think this is a case of “pants normally works like magic but now that something is wrong I have no idea what’s happening under the hood”
w
So, if you install
mypy
directly and run
mypy .
- you get the same error.
Copy code
(.venv) ⏺ scratch/pants-mypy-test % mypy .                                                                                                                                                               ⎇ main*
src/project/main.py:1: error: Cannot find implementation or library stub for module named "project.lib"  [import]
src/project/main.py:1: note: See <https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports>
In this situation, Pants is just passing through the error it gets from the underlying tools. if you run
./pants check :: --keep-sandboxes=always
you can hop into exactly what's being run in the
__run.sh
script
For your case of using init files vs (other options), you'll want to dig into the
mypy
configuration I think, to see what you can do to get it passing.
s
Got it, yeah adding the init files does seem to fix it, just wondering if there’s a way to avoid having to restructure the whole repo. Thanks for your help!
w
I -think- you're going to want to look at implicit vs explicit namespace stuff in mypy (or the associated PEP). I'll be the first to say I don't understand much of that, since that's not typically how I structure my repos. https://peps.python.org/pep-0420/ https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-no-namespace-packages https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-explicit-package-bases