Skip to content

python/getattr-shim

Pack Severity Fix Versions
Python warning none, edit by hand All versions

A module-level __getattr__ hook or a class __getattr__ that delegates straight to an inner attribute hides real definitions behind runtime dispatch, breaking static search and type checking. Import and call the real target directly.

def __getattr__(name: str) -> object:
if name == "LegacyClient":
return OrderClient
raise AttributeError(name)
from billing.orders import OrderClient

The rule reports two dynamic forwarding shims:

  • A module-level def __getattr__(name) hook (PEP 562 lazy alias or compatibility redirect).
  • A class-level def __getattr__(self, name) whose body is a single return getattr(self.<inner>, name) proxy wrapper.

The rule leaves alone a class __getattr__ that validates, computes, or dispatches custom behavior beyond forwarding straight to an inner attribute.

xpd has no automatic fix, so make the change by hand.

Update callers to import the real symbol directly and delete the module-level __getattr__ hook. For delegating proxy classes, pass the wrapped object directly or expose explicit typed methods for the narrow surface you need.

Module-level __getattr__ keeps lazy aliases or re-export shims alive outside static imports. Import each symbol directly from its defining module and delete __getattr__.

See all rules in the Python pack.