Replace click.style with an internal ANSI style helper - #2981
Conversation
Merging this PR will improve performance by 12.48%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ⚡ | test_bench_fragmented_body[httptools] |
2.4 ms | 2.1 ms | +12.48% |
Tip
Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.
Comparing drop-click-outside-cli (bd6e77c) with main (eea1bcc)
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a6770f96d6
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
|
||
| import click | ||
|
|
||
| from uvicorn._ansi import style |
There was a problem hiding this comment.
Keep click out of the package root import
When the package is imported via the public import uvicorn path, this replacement still does not avoid loading click: uvicorn/__init__.py imports uvicorn.main, and uvicorn.main imports click at module import time before these new ANSI helpers are involved. In an environment where click is blocked, import uvicorn still raises ModuleNotFoundError from uvicorn/main.py, so this only helps direct submodule imports such as uvicorn.config and leaves the package-level import behavior unchanged.
Useful? React with 👍 / 👎.
|
📖 Docs preview: https://70b38547-uvicorn.marcelotryle.workers.dev |
Avoids importing uvicorn.main (and thus click) on 'import uvicorn'; run resolves on first access via a module __getattr__.
There was a problem hiding this comment.
2 issues found across 6 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="uvicorn/__init__.py">
<violation number="1">
P2: Eagerly importing `main`/`run` from `uvicorn.main` in package init reintroduces CLI-side imports (including `click`) on plain `import uvicorn` and can break import when CLI deps are unavailable.</violation>
</file>
<file name="uvicorn/main.py">
<violation number="1">
P2: `uvicorn.main` now imports and initializes Click on module import, so non-CLI usage (`import uvicorn`, `uvicorn.run`) still pays the CLI dependency/initialization cost.</violation>
</file>
Tip: Review your code locally with the cubic CLI to iterate faster.
Re-trigger cubic
Outside the CLI, click is only used for
click.styleANSI coloring. This adds a small internaluvicorn/_ansi.pyhelper emitting the exact same escape sequences and swaps the call sites inconfig.py,server.py,logging.py, and the supervisors, somain.pyis now the only module using click.Output is byte-for-byte identical to
click.stylefor every color/bold combination used, verified at the function level and by rendering real log records throughDefaultFormatterandAccessFormatter. The CLI keeps using click, so the dependency is unchanged.AI Disclaimer
This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.