`type: ignore` comments on imports of external package lead to `[unused-ignore]` error codes every other run
Nobody has claimed this yet.
- 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)
- Run
pip install opentelemetry-instrumentation-asgi opentelemetry-distro - Add the code snippet below to
file.py - Run
mypy --warn-unused-ignore --no-namespace-packages --warn-unused-ignores --ignore-missing-imports file.py: Observer no error - Run
mypy --warn-unused-ignore --no-namespace-packages --warn-unused-ignores --ignore-missing-imports file.py: Observe an[unused-ignore]error - Run
mypy --warn-unused-ignore --no-namespace-packages --warn-unused-ignores --ignore-missing-imports file.py: Observer no error - 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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