Files
ruff/crates/ty_python_semantic/resources/lint_docs/invalid-module-getattr-call.md
Charlie Marsh 0e8636cc47 [ty] Diagnose invalid module-level __getattr__ calls (#27507)
## Summary

Previously, an invalid module-level `__getattr__` function caused us to
report that the requested attribute was missing, even though Python
actually invokes the function and raises `TypeError`:

```python
# example.py
def __getattr__() -> str:
    return "fallback"

# main.py
import example

example.missing  # error: [invalid-attribute-access]
from example import missing  # error: [invalid-module-getattr-call]
```

We now propagate failed module-level `__getattr__` calls through member
lookup, preserve the function's return type for error recovery, and
point diagnostics at its definition. Direct attribute access uses
`invalid-attribute-access`, while failed `from` imports use the
dedicated `invalid-module-getattr-call` rule. Defined module attributes
and real submodules continue to take precedence.
2026-08-14 14:12:08 -04:00

546 B

What it does

Checks for imports that fail when calling a module-level __getattr__ function.

Why is this bad?

If a module defines __getattr__, Python calls it when a from import requests a name that is not otherwise defined. The import raises an exception if __getattr__ cannot accept the requested name.

Examples

module.py:

def __getattr__() -> str:
    return "fallback"

main.py:

# TypeError: __getattr__() takes 0 positional arguments but 1 was given
from module import missing  # error