PY-MODU0003 · initializer_declaration
Reject ordinary function and class declarations in package initializers.
This is a deterministic rule for python. Read its implementation.
Definition
Section titled “Definition”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.
Evidence
Section titled “Evidence”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.
Exceptions
Section titled “Exceptions”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.
Examples
Section titled “Examples”Bad
`class Client` or `def connect()` declared directly in `__init__.py` returns `1`.
Gooddef __getattr__(name) in __init__.py returns 0, as does an imported Client.
References
Section titled “References”- Cites “PEP 562, Module getattr and dir”. Open reference
- Cites “The Python Language Reference”, customizing module attribute access. Open reference