Skip to content

fix(cli): import yaml lazily so the CLI works without the yaml extra#4929

Merged
sobolevn merged 1 commit into
litestar-org:mainfrom
uttam12331:fix/cli-lazy-yaml-import-4449
Jul 15, 2026
Merged

fix(cli): import yaml lazily so the CLI works without the yaml extra#4929
sobolevn merged 1 commit into
litestar-org:mainfrom
uttam12331:fix/cli-lazy-yaml-import-4449

Conversation

@uttam12331

@uttam12331 uttam12331 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes the CLI dying at import time when litestar is installed without pyyaml:

$ litestar -h
...
  File ".../litestar/cli/commands/schema.py", line 13, in <module>
    from yaml import dump as dump_yaml
ModuleNotFoundError: No module named 'yaml'

litestar/cli/commands/schema.py imported yaml at module level, and it is imported eagerly via litestar.cli.main — so every CLI invocation failed. pyyaml only arrives transitively with the standard extras (via uvicorn), while the docs promise the CLI and its hard dependencies are included by default.

The yaml import now happens inside the YAML-export branch of _generate_openapi_schema, raising MissingDependencyException("pyyaml", extra="yaml") when missing — the same pattern used for other optional dependencies. Exporting to .json (and every other CLI command) no longer needs pyyaml; exporting to .yaml/.yml gives an actionable install hint instead of a bare traceback.

Verified in a venv without pyyaml: litestar --help fails on main with the reported traceback and works with this change. Added test_cli_importable_without_pyyaml (simulates pyyaml's absence via sys.modules), which fails on main; the full test_schema_commands.py suite passes (14 passed).

Disclosure per the contribution guidelines: this fix was developed with AI assistance (Claude Code), with the behavior verified end-to-end locally as described above.

Closes

Closes #4449


📚 Documentation preview 📚: https://litestar-org.github.io/litestar-docs-preview/4929

The module-level 'from yaml import dump' in litestar/cli/commands/schema.py
is imported eagerly via litestar.cli.main, so every CLI invocation failed
with ModuleNotFoundError when litestar was installed without the yaml extra
(pyyaml only comes in transitively with the standard extras via uvicorn).

Import yaml inside the YAML export branch and raise
MissingDependencyException('pyyaml', extra='yaml') if it is missing, matching
how other optional dependencies are handled.

Fixes litestar-org#4449
@uttam12331
uttam12331 requested review from a team as code owners July 15, 2026 11:30
@codecov

codecov Bot commented Jul 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 67.30%. Comparing base (bd18fd4) to head (f48acb3).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4929   +/-   ##
=======================================
  Coverage   67.29%   67.30%           
=======================================
  Files         293      293           
  Lines       15228    15230    +2     
  Branches     1728     1728           
=======================================
+ Hits        10248    10250    +2     
  Misses       4833     4833           
  Partials      147      147           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you!

@sobolevn
sobolevn merged commit 6d07038 into litestar-org:main Jul 15, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: No module named 'yaml'

2 participants