Lazy help page
Right now, when doing --help on a lazy registered command or on an app containing lazy registered commands, the respective modules will be imported.
This leads to a decreased user experience when exploring a CLI and it's commands when heavy dependencies, e.g. PyTorch, are used.
Pythons ast module could help with that, by only parsing a module's content when calling "--help" or "--version" and their variants.
The ast representation includes docstrings and type annotations, so it should include everything needed to create the usage string.
Here is a quick example on how to extract the docstring and arguments from the AST:
# heavymodule.py
import torch
def myfunc(x: torch.Tensor) -> torch.Tensor:
"""Return the doubled tensor.
Args:
x (torch.Tensor): Input tensor.
Returns:
torch.Tensor: Output tensor with values doubled.
"""
return x * 2
import ast
import importlib
import pathlib
import_path = "heavymodule:myfunc"
module_path, _, attr_path = import_path.rpartition(":")
# get the path of heavymodule.py and read its content
module_file = pathlib.Path(importlib.util.find_spec(module_path).origin)
module_code = module_file.read_text()
module_tree = ast.parse(module_code)
for node in module_tree.body:
if isinstance(node, ast.FunctionDef) and node.name == attr_path:
func_node = node
break
print(f"=== Functions AST:\n{ast.dump(func_node)}")
print(f"=== Functions docstring:\n{func_node.body[0].value.value}")
print(f"=== Functions attributes as AST:\n{ast.dump(func_node.args)}")
Related: #675; just take this into consideration depending on how much effort/work this will take.