I have a question about `mypy` and how `pants` is ...
# general
r
I have a question about
mypy
and how
pants
is using it. We are using
mypy
with protobufs (with
mypy-protobuf
, so it generates
pyi
files). But
pants check
, using
mypy
will actually output errors about the
pyi
files, which is not something we want. The issue is that
mypy
follows files, and it follows any imports that it can find in
MYPYPATH
. Which the generated protobuf files have to be in, otherwise other errors happen. But anything in
MYPYPATH
is considered fair game for following and checking. But if I understand how
pants
is working, it is just running
mypy
individually on each target, so I think there's no need to follow anything - essentially
pants
takes care of making sure our codebase is covered instead. So we can solve the problem of
mypy
analyzing
pyi
files by passing in
--follow-imports=skip
, but that's strongly discouraged by
mypy
documentation. However, in this case, with
pants
, I think it's the right thing to do. Is this way of thinking reasonable?
h
I think so. I mean, does it work if you do?
@hundreds-father-404 is the expert here
r
It does work, yes (at least when I reproduce on the command line). Thanks for confirming
Actually, surprisingly enough, even though
--follow-imports=skip
works on the command line, it does not work when I add it to my mypy args in
pants.toml
. But I'm not sure why the mypy runner is running over the
pyi
file in the first place.
Theory: the
python_sources
build rule include "*.pyi" as a glob. When it is setting up its runtime directory, the protobuf generated pyi files match the glob, even though they really aren't part of the source.
h
Well the generated files aren't ever materialized in your repo, so the glob is immaterial
the generated files get materialized in the sandbox along with the sources that were globbed
And I am very surprised to see a difference between setting an option on the command line vs in pants.toml, unless you're also setting that option as an env var or in some other config file?
The priority is CLI flags > env vars > Config
r
@happy-kitchen-89482, what controls what files Pants is feeding to
mypy
for processing? I was expecting it to be controlled by the glob, but as you say, maybe by the time we're in the sandbox, the glob has already done its work and is not useful anymore. There may not be a difference between the command-line vs not. I'm testing the command-line of a file that uses the protobufs that have the
pyi
files, and my command-line run for that file is successful. However, my success means nothing if Pants is actually also calling
mypy
on the
pyi
files (or doing the equivalent via the
mypy
daemon).
h
What controls the files Pants feeds to mypy is what you specify on the command line. The
globs
in BUILD files just group files together for the purpose of applying target metadata to them (and if you add an explicit dependency on the target then that's as if you added one for each of the files in the target)
But you can mostly ignore that for this purpose, i think
To debug what's going on, run with
--keep-sandboxes=always
, which will log the locations of each sandbox
Find the mypy process sandbox, and look inside it to see what all the files are
And the command line Pants constructed
This should contain:
A) The transitive deps of whatever you specified on the cmd line (you can see those with
pants dependencies --transitive <command line specs>
) B) Any generated code
It's certainly possible there's a bug here
r
Thank you, will definitely do that and report back!
That helped convince me this is a mypy bug, which I've reported at https://github.com/python/mypy/issues/17298.
Wow, they closed it. They don't plan to fix the issue, and their workarounds are, IMHO, unreasonable.
What I think this means for pants users is that you really can't use
mypy
and
mypy-protobuf
together. I believe if you do, you'll be getting errors in
pyi
files, without any easy way to configure
mypy
to ignore those files.
h
Ooof
"change your codegen" LOL
ðŸĪŠ 2
I'm sure Google will be happy to accept a patch to protoc... 🙄
🙃 1
That was pretty dismissive
So I understand the issue precisely: I assume we need the .pyi files in there to provide symbols downstream, but we don't want to typecheck the .pyi files themselves (as they are not type-annotated) but mypy is doing so anyway?
r
@happy-kitchen-89482 exactly!
h
I feel like Pants could work around that somehow
If you feel like taking a closer look at that
r
I'll see what I can figure out; it isn't obvious right now what Pants could be doing here. My only thought is maybe autogenerating the per-module mypy configs that were one of the workarounds, but that seems dangerous. Or, more in the realm of pure hacks, inserting that mypy ignore-errors bit manually for every
pyi
file
mypy-protobuf
generates.
h
We run mypy by passing it a list of newline-separated input files with
@__files.txt
. I have a feeling that we put those generated files in that list, and we shouldn't.
Did you see them there in your sandbox?
r
Yes, I did see those, but it looked correct. It did not include pyi files.
So I think some Pants solution here would probably be a bit hacky. I'm still thinking about it.
h
Huh, OK, so Pants is doing the right thing
Damn
I mean, good, but also damn