Skip to content

Conversation

@athewsey
Copy link
Contributor

Issue #, if available: N/A

Description of changes:

Similar to how #12 previously enabled automatic formatting-on-commit (via Prettier) for TypeScript sections of the codebase, this PR:

  1. Enables format-on-commit for Python files (via ruff from the chainlit image's dev dependencies)
    • The relevant changes here are only bin/format-python.ts, lint-staged.config.mjs, and package.json - as per the first commit
  2. Runs the full npm run format to align any files that hadn't been formatted yet in Add linters and use uv for Python package #12.
    • Files edited in the second commit have no functional changes

Implementation notes:

husky git hooks and lint-staged together provide the functionality for automatically linting/formatting files before commit as implemented in #12. They live in NodeJS world, and are activated when the developer npm installs the project.

ruff on the other hand, which powers our Python linting/formatting, is specified as a dependency of the chainlit_image/foundational-llm-chat_app/pyproject.toml project. This presents complexity because of the many ways users might manage their Python virtual environments (Poetry, uv, venv, etc), and the fact that a developer technically doesn't need to have the Python venv set up at all to deploy the solution today (since the container build will happen in Docker/finch/etc).

In this solution:

  • bin/format-python.ts attempts to provide an OS-agnostic invocation of ruff format ... that should work either on macOS or Windows, and with either Python or uv or any other venv manager on the condition that the environment is created at chainlit_image/foundational-llm-chat_app/.venv.
    • If ruff invocation fails, this script will currently throw an error and therefore (if it was called by lint-staged) block commit.
  • lint-staged should only invoke the Python linter if the developer modifies Python files
    • ...so if somebody only modifies CDK and never bothers to set up their local Py dev environment, this shouldn't block them - but any changes to Py files will be blocked unless they've set up their virtual environment.

Testing done:

  • New npm run format works and generated the formatting changes proposed
  • To check the auto lint-on-commit works, I:
    • Made specific, small, noncompliant whitespace edits to a broad selection of the files changed by npm run format
    • Started the commit and waited for lint-staged to complete
    • Checked that all these changes had been reverted

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Set up ruff format for .py files on commit, to bring in line with
JavaScript/TypeScript which was already working through lint-staged.
Apply linters/formatters only - no functional changes
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