Skip to content

type: ignore comments on imports of external package lead to [unused-ignore] error codes every other run #20105

@provinzkraut

Description

@provinzkraut

Bug Report

When running mypy with --warn-unused-ignore --no-namespace-packages --warn-unused-ignores --ignore-missing-imports, putting a type: ignore[attr-defined] on an import will have mypy report an Unused "type: ignore" comment [unused-ignore] every other run, unless the cache is being cleared between runs.

To Reproduce

from opentelemetry.instrumentation.asgi import (  # type: ignore[attr-defined]
    OpenTelemetryMiddleware,
)

(Since this only seems to work with external packages, some setup is required)

  1. Run pip install opentelemetry-instrumentation-asgi opentelemetry-distro
  2. Add the code snippet below to file.py
  3. Run mypy --warn-unused-ignore --no-namespace-packages --warn-unused-ignores --ignore-missing-imports file.py: Observer no error
  4. Run mypy --warn-unused-ignore --no-namespace-packages --warn-unused-ignores --ignore-missing-imports file.py: Observe an [unused-ignore] error
  5. Run mypy --warn-unused-ignore --no-namespace-packages --warn-unused-ignores --ignore-missing-imports file.py: Observer no error
  6. Repeat the cycle as many times as you like :)

When removing the cache between runs, all works well, so I suspect it has something to do with caching.

Your Environment

  • Mypy version used: 1.18.2, main
  • Mypy command-line flags: --warn-unused-ignore --no-namespace-packages --warn-unused-ignores --ignore-missing-imports
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.13.7

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions