mkinit.static_mkinit module

Static version of mkinit.dynamic_autogen

mkinit.static_mkinit.autogen_init(modpath_or_name, submodules=None, respect_all=True, options=None, dry=False, diff=False, recursive=False)[source]

Autogenerates imports for a package __init__.py file.

Parameters:
  • modpath_or_name (PathLike | str) – path to or name of a package module. The path should reference the dirname not the __init__.py file. If specified by name, must be findable from the PYTHONPATH.

  • submodules (List[str] | None) – if specified, then only these specific submodules are used in package generation. Otherwise, all non underscore prefixed modules are used. This may be deprecated in the future in favor of using the user declaration variables.

  • respect_all (bool) – if False the __all__ attribute is ignored while parsing. Defaults to True.

  • options (dict | None) – formatting options; customizes how output is formatted. See formatting._ensure_options for defaults.

  • dry (bool) – if True, the autogenerated string is not written. Defaults to False.

  • recursive (bool) – if True, we will autogenerate init files for all subpackages. Defaults to Fasle.

Note

This will partially override the __init__ file. By default everything up to the last comment / __future__ import is preserved, and everything after is overriden. For more fine grained control, you can specify XML-like # <AUTOGEN_INIT> and # </AUTOGEN_INIT> comments around the volitle area. If specified only the area between these tags will be overwritten.

To autogenerate a module on demand, its useful to keep a doctr comment in the __init__ file like this

python -m mkinit <your_module>

Example

>>> init_fpath, new_text = autogen_init('mkinit', submodules=None,
>>>                                     respect_all=True,
>>>                                     dry=True)
>>> assert 'autogen_init' in new_text
mkinit.static_mkinit.static_init(modpath_or_name, submodules=None, respect_all=True, options=None)[source]

Returns the autogenerated initialization string. This can either be executed with exec or directly copied into the __init__.py file.

Note

This only returns the generated part, it does not put it the context of existing code. For that functionality see autogen_init() using the dry=True argument to get a return value instead of modifying inplace.

Parameters:

options (dict | None) – formatting options; customizes how output is formatted. See formatting._ensure_options for defaults.