mirror of
https://github.com/astral-sh/ruff.git
synced 2026-12-04 02:03:58 +08:00
## 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.
546 B
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