| 1 |
.. _Lazy Loading: |
| 2 |
|
| 3 |
============ |
| 4 |
Lazy Loading |
| 5 |
============ |
| 6 |
|
| 7 |
Lazy loading allows you to register commands **using import path strings instead of direct function references**. |
| 8 |
This defers importing command modules until they are actually executed, which could significantly improve CLI startup time for large applications that have expensive per-command imports. |
| 9 |
|
| 10 |
----------- |
| 11 |
Basic Usage |
| 12 |
----------- |
| 13 |
|
| 14 |
Instead of importing and registering a function directly: |
| 15 |
|
| 16 |
.. code-block:: python |
| 17 |
|
| 18 |
from cyclopts import App |
| 19 |
from myapp.commands.users import create, delete, list_users # Imported immediately |
| 20 |
|
| 21 |
app = App() |
| 22 |
user_app = App(name="user") |
| 23 |
|
| 24 |
user_app.command(create) |
| 25 |
user_app.command(delete) |
| 26 |
user_app.command(list_users, name="list") |
| 27 |
|
| 28 |
app.command(user_app) |
| 29 |
|
| 30 |
Use an import path string: |
| 31 |
|
| 32 |
.. code-block:: python |
| 33 |
|
| 34 |
from cyclopts import App |
| 35 |
|
| 36 |
app = App() |
| 37 |
user_app = App(name="user") |
| 38 |
|
| 39 |
# No imports! Modules loaded only when commands execute |
| 40 |
user_app.command("myapp.commands.users:create") |
| 41 |
user_app.command("myapp.commands.users:delete") |
| 42 |
user_app.command("myapp.commands.users:list_users", name="list") |
| 43 |
|
| 44 |
app.command(user_app) |
| 45 |
|
| 46 |
The import path format is ``"module.path:function_name"``, similar to setuptools entry points. |
| 47 |
|
| 48 |
Lazy commands are resolved/imported in these situations: |
| 49 |
|
| 50 |
- **Command Execution** - When the user runs that specific command |
| 51 |
- **Subcommand Help** - When displaying ``--help`` for the specific lazy command (e.g., ``myapp lazy-cmd --help``) |
| 52 |
- **Direct Access** - When accessing via ``app["command_name"]`` |
| 53 |
|
| 54 |
Parent ``--help`` (e.g., ``myapp --help``) displays lazy commands using metadata provided at registration time |
| 55 |
(``help=``, ``group=``, ``show=``, ``sort_key=``) **without** resolving them. |
| 56 |
|
| 57 |
In order to benefit from lazy loading, you have to make sure that the files are not imported by other means when your CLI starts up. |
| 58 |
|
| 59 |
------------------ |
| 60 |
Import Path Format |
| 61 |
------------------ |
| 62 |
|
| 63 |
The import path string has two parts separated by a colon (``:``): |
| 64 |
|
| 65 |
**Module Path** (before the ``:``) |
| 66 |
The Python **module** to import, using dot notation (e.g., ``myapp.commands.users``). |
| 67 |
|
| 68 |
**Attribute Name** (after the ``:``) |
| 69 |
The function or App to get from the module using :func:`getattr`. |
| 70 |
|
| 71 |
Examples: |
| 72 |
|
| 73 |
.. code-block:: python |
| 74 |
|
| 75 |
# Simple function in a module |
| 76 |
app.command("myapp.commands:create_user") |
| 77 |
|
| 78 |
# Nested module path |
| 79 |
app.command("myapp.admin.database.operations:migrate") |
| 80 |
|
| 81 |
# Import an App instance, exposed to the CLI as "admin" |
| 82 |
app.command("myapp.admin:admin_app", name="admin") |
| 83 |
|
| 84 |
.. note:: |
| 85 |
The attribute name (after ``:``) is the **actual Python name**, not the CLI command name. |
| 86 |
Use the ``name`` parameter to specify the CLI command name. |
| 87 |
|
| 88 |
---------------------- |
| 89 |
Name vs Function Name |
| 90 |
---------------------- |
| 91 |
|
| 92 |
The ``name`` parameter specifies **how the command appears in the CLI**, while the import path |
| 93 |
specifies **what code to execute**. They can be completely different: |
| 94 |
|
| 95 |
.. code-block:: python |
| 96 |
|
| 97 |
from cyclopts import App |
| 98 |
|
| 99 |
user_app = App(name="user") |
| 100 |
|
| 101 |
# Function name: "list_users" |
| 102 |
# CLI command name: "list" |
| 103 |
user_app.command("myapp.commands.users:list_users", name="list") |
| 104 |
|
| 105 |
# Function name: "delete" |
| 106 |
# CLI command name: "remove" |
| 107 |
user_app.command("myapp.commands.users:delete", name="remove") |
| 108 |
|
| 109 |
.. code-block:: console |
| 110 |
|
| 111 |
$ myapp user list --limit 10 |
| 112 |
# Imports and runs myapp.commands.users:list_users |
| 113 |
|
| 114 |
$ myapp user remove --username alice |
| 115 |
# Imports and runs myapp.commands.users:delete |
| 116 |
|
| 117 |
If ``name`` is not specified, Cyclopts derives it from the function name with |
| 118 |
:attr:`App.name_transform <cyclopts.App.name_transform>` applied (typically converting underscores to hyphens). |
| 119 |
|
| 120 |
-------------- |
| 121 |
Error Handling |
| 122 |
-------------- |
| 123 |
|
| 124 |
If an import path/configuration is invalid, the error occurs **when the command is executed**, not when it's registered: |
| 125 |
|
| 126 |
.. code-block:: python |
| 127 |
|
| 128 |
from cyclopts import App |
| 129 |
|
| 130 |
app = App() |
| 131 |
|
| 132 |
# This won't error immediately - registration succeeds |
| 133 |
app.command("nonexistent.module:func") |
| 134 |
|
| 135 |
app() |
| 136 |
|
| 137 |
.. code-block:: console |
| 138 |
|
| 139 |
$ myapp func |
| 140 |
# Now the error occurs: |
| 141 |
ImportError: Cannot import module 'nonexistent.module' |
| 142 |
|
| 143 |
To catch import errors early, you can access the command during testing: |
| 144 |
|
| 145 |
.. code-block:: python |
| 146 |
|
| 147 |
import pytest |
| 148 |
from cyclopts import App |
| 149 |
|
| 150 |
def test_lazy_commands_are_importable(): |
| 151 |
app = App() |
| 152 |
app.command("myapp.commands:create") |
| 153 |
|
| 154 |
# This will trigger the import and fail if path is wrong |
| 155 |
resolved = app["create"] |
| 156 |
assert resolved is not None |
| 157 |
|
| 158 |
----------------------- |
| 159 |
Groups and Lazy Loading |
| 160 |
----------------------- |
| 161 |
|
| 162 |
.. tip:: **TL;DR:** Pass ``group=`` at registration time, and define :class:`~cyclopts.Group` objects in your main CLI module, NOT in lazy-loaded modules. |
| 163 |
|
| 164 |
Lazy commands support ``group=`` at registration time, so they appear in the correct group in ``--help`` output without being resolved. |
| 165 |
However, :class:`~cyclopts.Group` objects defined **only inside** unresolved lazy modules won't be available until those modules are imported. |
| 166 |
To avoid this, define :class:`~cyclopts.Group` objects in non-lazy modules. |
| 167 |
|
| 168 |
.. code-block:: python |
| 169 |
|
| 170 |
# myapp/cli.py (always imported) |
| 171 |
from cyclopts import App, Group |
| 172 |
|
| 173 |
# Define Group objects here |
| 174 |
admin_group = Group("Admin Commands", validator=require_admin_role) |
| 175 |
db_group = Group("Database", default_parameter=Parameter(envvar_prefix="DB_")) |
| 176 |
|
| 177 |
app = App() |
| 178 |
|
| 179 |
# Lazy commands can reference the Group objects |
| 180 |
app.command("myapp.admin:create_user", group=admin_group) |
| 181 |
app.command("myapp.admin:delete_user", group=admin_group) |
| 182 |
app.command("myapp.db:migrate", group=db_group) |
| 183 |
|
| 184 |
**What to avoid:** Defining ``Group`` objects inside lazy-loaded modules: |
| 185 |
|
| 186 |
.. code-block:: python |
| 187 |
|
| 188 |
# myapp/admin.py (lazy-loaded) |
| 189 |
from cyclopts import App, Group |
| 190 |
|
| 191 |
# BAD: This Group won't be available to other commands until this module is imported |
| 192 |
admin_group = Group("Admin Commands", validator=require_admin_role) |
| 193 |
|
| 194 |
def create_user(): |
| 195 |
... |
| 196 |
|
| 197 |
If you reference a group by string (e.g., ``group="Admin Commands"``) and the :class:`~cyclopts.Group` object |
| 198 |
with that name is only defined in an unresolved lazy module, the group won't be available |
| 199 |
until that lazy module is imported. This means that: |
| 200 |
|
| 201 |
- Validators defined on the lazy-loaded :class:`~cyclopts.Group` won't be applied to commands in other modules. |
| 202 |
- :attr:`.Group.default_parameter` and other settings won't be inherited by commands **referencing the group by string**. |
| 203 |
|
| 204 |
Once the lazy module is imported (e.g., by executing one of its commands), the :class:`~cyclopts.Group` object |
| 205 |
becomes available and subsequent operations will use it correctly. |