Skip to content

← Back to Distributed_Design_Optimizer

generate_inits

Source: Distributed_Design_Optimizer/generate_inits.py

Script to auto-generate init.py files for the Distributed_Design_Optimizer package.

Uses AST parsing to discover public classes/functions from each package's direct .py submodules ONLY (not from subpackages), then generates lazy imports via getattr with a custom module class to handle submodule name collisions.

Problem: When ClassName.py defines class ClassName, from package import ClassName resolves the submodule file rather than calling getattr, so beartype sees a module object instead of the class. The fix: a custom module subclass overrides getattribute to detect when a submodule would be returned for a name in _LAZY_IMPORTS, and lazily resolves the class instead.

This means you can do: from Distributed_Design_Optimizer.coordination.multilevelmethod import ALC, PC, LC but NOT: from Distributed_Design_Optimizer.coordination import ALC

Each level only re-exports what is defined in its own .py files.

Usage: python -m Distributed_Design_Optimizer.generate_inits # generate all init.py files python -m Distributed_Design_Optimizer.generate_inits --dry # preview without writing

Notes:

  • Preserves everything outside the tags (e.g. beartype_this_package())
  • Creates missing init.py files with proper headers
  • Safe to re-run at any time after adding/removing modules
  • Uses lazy imports with module class override to avoid both circular imports
    and submodule name collisions with beartype

Functions

find_all_packages(root)

Find all directories under root that are (or should be) Python packages.

Args:

root: Root directory path to search for packages.

ensure_init_with_tags(package_dir)

Ensure init.py exists and contains AUTOGEN_INIT tags.

Args:

package_dir: Path to the package directory.

get_package_dotpath(package_dir)

Convert a filesystem path to a Python dotted module path.

Args:

package_dir: Filesystem path to the package directory.

get_module_names(package_dir)

Get names of direct .py submodules (without .py extension).

Args:

package_dir: Path to the package directory.

get_public_names_from_module(module_path)

Extract public class/function/variable names from a .py file using AST.

Returns a list of public names defined at module level.

Args:

module_path: Filesystem path to the .py file.

generate_lazy_init_content(package_dir)

Generate lazy import init.py content for a package.

Uses getattr for lazy resolution (avoids circular imports) combined with a custom module class override (getattribute) that intercepts submodule objects being returned for names in _LAZY_IMPORTS, resolving the class instead. This handles the Python quirk where from package import X returns the submodule when X.py exists, bypassing getattr. Includes TYPE_CHECKING imports for Pylance/mypy static analysis.

Args:

package_dir: Path to the package directory.

update_init_file(init_path, autogen_content)

Replace the AUTOGEN section in an init.py file.

Args:

init_path: Path to the init.py file.
autogen_content: The new autogenerated content to insert.

main(dry)

Generate lazy-import init.py files for all packages.

Args:

dry: If True, perform a dry run without writing files.