Skip to content

PY-MODU0003 · initializer_declaration

Reject ordinary function and class declarations in package initializers.

This is a deterministic rule for python. Read its implementation.

Count every top-level class and every ordinary top-level function declared in __init__.py. Package initializers state a package surface and leave implementation declarations in focused sibling modules. Python module customization hooks remain valid declarations because the language invokes them on the module itself.

Each finding covers one initializer and records its exact disallowed declaration count. The value is 0 when the initializer contains only surface declarations and supported hooks.

The module hooks __getattr__ and __dir__ are accepted as specified by Python. Imported functions and classes are not declarations in the initializer and remain accepted.

Bad

`class Client` or `def connect()` declared directly in `__init__.py` returns `1`.
Good

def __getattr__(name) in __init__.py returns 0, as does an imported Client.

  • Cites “PEP 562, Module getattr and dir”. Open reference
  • Cites “The Python Language Reference”, customizing module attribute access. Open reference