python / python/mypy

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

Open
#20,105 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-configuration topic-incremental
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the alternating result with file.py using the listed mypy command and the opentelemetry import, then trace the cache-related handling of unused-ignore state. Add a regression test for repeated cached runs; done means the command consistently reports no spurious [unused-ignore] error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.