python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.18k stars 2.77k forks source link

--follow-imports does not have an effect on following stub files #17298

Closed ahyatt-continua closed 3 months ago

ahyatt-continua commented 3 months ago

Bug Report

The documentation states the --follow-imports=skip or --follow-imports=silent will suppress problems from imports. But this doesn't apply to pyi files, for reasons that aren't clear to me, and certainly make it impossible for us to use mypy. Our setup is that we use mypy with the pants build system, and are using protobufs. We generate stubs from the protobufs, which, when pants run, as colocated with the source files. However, try as I might, I cannot ever get a clean mypy run because the protobuf pyi files have issues that we cannot control, and do not care about.

A real-life example:

mypy --follow-imports=silent --explicit-package-bases velo/velo.py

velo/velo_admitter_pb2_grpc.pyi:8: error: Skipping analyzing "grpc": module is installed, but missing library stubs or py.typed marker [import-untyped] velo/velo_admitter_pb2_grpc.pyi:9: error: Skipping analyzing "grpc.aio": module is installed, but missing library stubs or py.typed marker [import-untyped] velo/velo_admitter_pb2_grpc.pyi:9: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

As you can see, I'm really only expecting errors on velo.py here. I believe it is an error to get errors about anything else.

To Reproduce

#!/bin/bash

# Run this from an empty directory you can delete later.

cat > repro.py <<EOF
import importlib
EOF

cat > importlib.pyi <<EOF
import nonexistent_module
EOF

mypy --follow-imports=silent repro.py

Expected Behavior

No errors from pyi files should be shown when --follow-imports=silent (or skip)

Actual Behavior

Errors from pyi files are shown when --follow-imports=silent (or skip)

Your Environment

hauntsaninja commented 3 months ago

Use a per-module glob config with follow_imports and follow_imports_for_stubs. Or change your codegen to add # mypy: ignore-errors at the top of the file.