Enable lazy loading for CLI commands via cyclopts v4#20878
Draft
Enable lazy loading for CLI commands via cyclopts v4#20878
Conversation
Upgrade cyclopts from v3 to v4 and convert all 29 command
registrations in `_app.py` from eager imports to lazy string-based
registrations (`app.command("module:attr")`). Command modules are now
only imported when actually invoked, eliminating ~1s of unnecessary
import overhead for targeted commands like `prefect config view`.
- Bump cyclopts dependency from >=3.0 to >=4.0
- Replace 170+ lines of eager `from ... import` with 29 one-liner
lazy registrations including explicit `name=` and `alias=`
- Add missing `import prefect.cli.dev` in test_dev.py (lazy loading
means modules aren't available via attribute access on parent
packages until explicitly imported)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The `reset_sys_modules` autouse fixture deletes any modules added to `sys.modules` during a test. With lazy CLI loading, command modules like `prefect.cli.events` are imported on-demand during test invocations and then deleted after the test. However, cyclopts caches the resolved App internally (`CommandSpec._resolved`), so subsequent tests get a stale App whose function references point to the deleted module. When monkeypatch patches the freshly re-imported module, the stale cached App still uses the old (unpatched) functions. Fix: skip `prefect.cli.*` modules during cleanup so cyclopts' cached references remain valid. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
app.command("module:attr"))Performance
Before (all 25+ command modules imported eagerly at
import prefect.cli._app):After (zero command modules imported until needed):
Note:
prefect --helpstill imports all modules (cyclopts resolves all lazy commands to render the help page). The win is forprefect <specific-command>which now only imports the module it needs.Test plan
flows,profiles,blocks,gcl, etc.)--help,version,config view,profile ls,flow-run ls,server services ls,deploy --help🤖 Generated with Claude Code