8000
Skip to content

Replace click.style with an internal ANSI style helper - #2981

Merged
Kludex merged 6 commits into
mainfrom
drop-click-outside-cli
Jun 10, 2026
Merged

Replace click.style with an internal ANSI style helper#2981
Kludex merged 6 commits into
mainfrom
drop-click-outside-cli

Conversation

@Kludex
@Kludex Kludex commented Jun 9, 2026
Copy link
Copy Markdown
Owner

Outside the CLI, click is only used for click.style ANSI coloring. This adds a small internal uvicorn/_ansi.py helper emitting the exact same escape sequences and swaps the call sites in config.py, server.py, logging.py, and the supervisors, so main.py is now the only module using click.

Output is byte-for-byte identical to click.style for every color/bold combination used, verified at the function level and by rendering real log records through DefaultFormatter and AccessFormatter. 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.

@codspeed-hq
codspeed-hq Bot commented Jun 9, 2026
Copy link
Copy Markdown

Merging this PR will improve performance by 12.48%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 23 untouched benchmarks

Performance Changes

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)

Open in CodSpeed

@cubic-dev-ai cubic-dev-ai Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No issues found across 6 files

Re-trigger cubic

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread uvicorn/config.py

import click

from uvicorn._ansi import style
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

@github-actions
github-actions Bot commented Jun 9, 2026
Copy link
Copy Markdown
Contributor

📖 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__.
@Kludex Kludex changed the title Drop click usage outside the CLI Replace click.style with an internal ANSI style helper Jun 10, 2026
@cubic-dev-ai cubic-dev-ai Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

@Kludex
Kludex merged commit bf3f60c into main Jun 10, 2026
25 checks passed
@Kludex
Kludex deleted the drop-click-outside-cli branch June 10, 2026 05:40
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.

1 participant

0