-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
Bug Report
Running Mypy on a certain set of files (see below) that import things from both the google-cloud-logging and google-cloud-pubsub packages with some # type: ignore comments applied results in different type checking outcomes (failure vs success) depending on whether the cache has been populated or not.
To Reproduce
❗ Note: There is a bash script at the end of this section to make reproducing this more convenient!
- Create a package
bugwith an__init__.pyand these two files:
foo.py:
import google.cloud.logging
client = google.cloud.logging.Client() # type: ignore[no-untyped-call]bar.py:
from google.cloud.pubsub_v1 import PublisherClient # type: ignore[import-untyped]
publisher: PublisherClient = PublisherClient()-
Create a venv with
mypy,google-cloud-logging, andgoogle-cloud-pubsubinstalled. -
Ensure there is no
.mypy_cachedir present. -
Run
mypy --strict bug/twice.
❗ Script to perform all these steps automatically
Needs uv to be installed.
#!/bin/bash
# Prepare directory:
rm -rf bug/ .mypy_cache/
mkdir bug
touch bug/__init__.py
cat > bug/foo.py <<HEREDOC
import google.cloud.logging
client = google.cloud.logging.Client() # type: ignore[no-untyped-call]
HEREDOC
cat > bug/bar.py <<HEREDOC
from google.cloud.pubsub_v1 import PublisherClient # type: ignore[import-untyped]
publisher: PublisherClient = PublisherClient()
HEREDOC
# Run Mypy:
MYPY="uv run --with google-cloud-logging,google-cloud-pubsub,mypy mypy"
echo "***** Version info: *****"
$MYPY --version
echo "***** First invocation: *****"
$MYPY --strict bug/
echo "***** Second invocation: *****"
$MYPY --strict bug/Expected Behavior
Both mypy invocations result in the same outcome.
Actual Behavior
The first invocation reports some errors:
$ mypy --strict bug/
bug/foo.py:1: error: Skipping analyzing "google.cloud": module is installed, but missing library stubs or py.typed marker [import-untyped]
bug/foo.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
bug/foo.py:1: error: Skipping analyzing "google": module is installed, but missing library stubs or py.typed marker [import-untyped]
bug/foo.py:3: error: Unused "type: ignore" comment [unused-ignore]
Found 3 errors in 1 file (checked 3 source files)While the second invocation reports success:
$ mypy --strict bug/
Success: no issues found in 3 source filesYour Environment
- Mypy version used: 1.15.0, but I also tried with the current
masterand it's the same - Mypy command-line flags:
--strictto get 1 more error that differs between invocations, but the basic issue also appears without it - Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.13.2
Additional Context
I'm actually trying to create a minimal reproducible example for a slightly different, more severe issue involving these two packages, in which there is no possible configuration of Never mind, putting type: ignore comments (or lack thereof) that satisfies Mypy, but this is proving much more difficult to boil down (only seems to happen in our production repo). My hope is that this issue here is "related enough" to it that fixing one will fix both 🤞# type: ignore on all of the imports but none of the usages did satisfy Mypy. But this issue here still made it needlessly difficult to arrive at that solution by trial and error, and it hints at perhaps more general caching bugs, so it should still be fixed IMHO.
Related Issues
- importing google.cloud.logging causes untyped modules in the namespace to be considered missing #10360
- Also involves
google-cloud-loggingweirdness.
- Also involves