Skip to content
This repository was archived by the owner on Jun 17, 2026. It is now read-only.

Format generated Python reliably; lint with UP rules - #86

Merged
markbackman merged 1 commit into
mainfrom
mb/format-generated-python
May 28, 2026
Merged

markbackman merged 1 commit into
mainfrom
mb/format-generated-python

Conversation

@markbackman

Copy link
Copy Markdown
Contributor

Problem

pipecat init is supposed to Ruff-format generated Python (ProjectGenerator._format_python_files), but the output often arrives raw — mis-indented blocks, stray blank lines, unsorted imports.

Root cause: the formatting step shelled out to a bare ruff on PATH. ruff is a hard runtime dependency, so it's installed alongside the CLI — but when the CLI is installed as an isolated tool (uv tool install / pipx), only the declared pipecat/pc scripts are exposed on PATH. Ruff's console script lives in the tool's private venv and isn't reachable, so subprocess.run(["ruff", ...]) raised FileNotFoundError, which was caught and passed — formatting silently skipped. (It worked in a dev checkout because .venv/bin is on PATH, which is why it wasn't caught.)

Changes

  • Resolve the bundled Ruff via ruff.__main__.find_ruff_bin(), falling back to [sys.executable, "-m", "ruff"] — no longer depends on ruff being on PATH.
  • No more silent failures: capture Ruff's output/return code and print a warning if it can't run or exits non-zero, instead of shipping unformatted code invisibly.
  • Enable UP (pyupgrade) lint rules in generated projects' pyproject.toml alongside import sorting (I).

Tests

Added regression guards in tests/test_project_generation.py:

  • The existing 28-config generation sweep now also asserts each generated server/ is Ruff-clean (ruff format --check).
  • test_generated_python_formatted_without_ruff_on_path — points PATH at an empty dir (verifies ruff is genuinely unreachable) and confirms output is still formatted. Guards the bundled-binary resolution.
  • test_format_warns_when_ruff_cannot_run — confirms a warning is emitted when Ruff can't run.

Verified these tests go red against the old bare-ruff behavior and green with the fix.

Verification

Full real-world reproduction before/after: built the wheel, uv tool install'd it into throwaway dirs, ran pc init in a shell with ruff absent from PATH → exit 0, no warning, fully formatted bot.py, ruff format --check reports clean.

Suite: 418 passed (was 416).

🤖 Generated with Claude Code

The post-generation Ruff step shelled out to a bare `ruff` on PATH. When
the CLI is installed as an isolated tool (uv tool install / pipx), only the
`pipecat`/`pc` scripts are exposed on PATH — Ruff's console script is not —
so formatting was silently skipped (FileNotFoundError → pass), shipping raw,
mis-indented template output.

- Resolve the Ruff binary bundled with the CLI's dependencies via
  `ruff.__main__.find_ruff_bin()` (falling back to `python -m ruff`) instead
  of relying on PATH.
- Surface a warning when Ruff cannot run or exits non-zero, rather than
  silently shipping unformatted code.
- Enable Ruff's `UP` (pyupgrade) rules in generated projects alongside `I`.
- Add regression tests: assert every generated config is Ruff-clean, that
  formatting works with `ruff` absent from PATH, and that failures warn.
@markbackman
markbackman merged commit 16f24be into main May 28, 2026
1 check passed
@markbackman
markbackman deleted the mb/format-generated-python branch May 28, 2026 18:35
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant