Skip to content

Empty vs filled cache inconsistency for google-cloud-logging & google-cloud-pubsub together with type: ignore comments #19037

Closed
@sh-at-cs

Description

@sh-at-cs

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!

  1. Create a package bug with an __init__.py and 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()
  1. Create a venv with mypy, google-cloud-logging, and google-cloud-pubsub installed.

  2. Ensure there is no .mypy_cache dir present.

  3. 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 files

Your Environment

  • Mypy version used: 1.15.0, but I also tried with the current master and it's the same
  • Mypy command-line flags: --strict to 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 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 🤞 Never mind, putting # 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions