Skip to content
Latchkey

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.

python
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

  1. Confirm the package version mypy resolves matches what the code targets.
  2. Install the matching stub package if the library is untyped.
  3. Re-run mypy to confirm the attribute is now known.
Terminal
pip install "httpx==0.27.*" types-requests

Narrow 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.

app/client.py
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.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →