mypy "error: Module has no attribute" in CI
mypy resolved the module but its type information does not declare the attribute you accessed. The runtime may work, but the stubs or the installed version mypy sees do not expose the name, so it reports [attr-defined].
What this error means
mypy fails CI with "error: Module 'X' has no attribute 'Y' [attr-defined]" even though the code runs fine.
app/client.py:8: error: Module "httpx" has no attribute "AsyncClientX" [attr-defined]
Found 1 error in 1 file (checked 12 source files)Common causes
A version skew between runtime and what mypy resolved
mypy type-checks against the installed package; if CI installed a version where the name does not exist (or is not re-exported), it flags it.
Missing or incomplete type stubs
A package without inline types relies on stub packages; if the stub omits the attribute, mypy cannot see it.
How to fix it
Align versions and stubs
- Confirm the package version mypy resolves matches what the code targets.
- Install the matching stub package if the library is untyped.
- Re-run mypy to confirm the attribute is now known.
pip install "httpx==0.27.*" types-requestsNarrow the ignore if the runtime is correct
When the attribute genuinely exists at runtime but stubs lag, suppress just that line rather than disabling the check.
client = httpx.AsyncClientX() # type: ignore[attr-defined]How to prevent it
- Pin the same versions for runtime and type checking.
- Install stub packages for untyped dependencies.
- Prefer targeted
# type: ignore[code]over broad suppression.