This document helps new contributors set up their local development environment for the metrics-utility repository. It covers installing required tools, configuring uv, and enabling a pre-commit hook that uses ruff for linting and formatting.
uv: A Python virtual environment manager that simplifies dependency management.ruff: A fast Python linter and formatter. Ensures code adheres to style and best practices.
The pre-commit hook leverages ruff (managed by uv) to automatically check and format your code whenever you run git commit. This helps maintain consistent code quality.
- Python 3.11+ installed on your system.
- Git installed for version control.
pipor other package managers (e.g.,pipx) for installing Python tools.
-
Clone the Repository:
git clone https://github.com/ansible/metrics-utility.git cd metrics-utility -
Install
uv(if not installed):pip install uv
Note: You may need to use
pip3or run under a virtual environment if you have multiple Python versions. -
Synchronize Dependencies:
uv sync
This command creates (or updates) the virtual environment defined by
pyproject.tomlanduv.lock. It installs all project dependencies, includingruff,pytest,django-admin, etc. -
(Optional) Using the Virtual Environment Directly
- By default,
uvstores the environment in.venv. If you want to manually activate it, you can run:source .venv/bin/activate # For Unix/MacOS # OR .venv\Scripts\activate # For Windows
- Typically, you won't need to do this if you rely on
uvxoruv run(detailed below) to execute commands within the environment. You can also useuv tool installto add tools likerufforpytestto your path, eliminating the need for manual activation.
- By default,
-
Verify Installations:
ruff --version pytest --version django-admin --version pre-commit --version
If any command fails, run
uv syncagain or check youruvinstallation.
Depending on your workflow, you can run commands in various ways:
uvx <command>: Runs a command within the.venvenvironment without manual activation:uvx ruff check . uvx pytestuv tool install: Installs binaries in a way that they become available on your local PATH (i.e., nouvprefix needed):uv tool install ruff uv tool install pytest # Now ruff/pytest commands are available in your shell directly
Use whichever approach suits your workflow.
-
Install Pre-commit Hooks:
pre-commit install
This registers the hooks defined in
.pre-commit-config.yamlso that everygit committriggers a lint/format check usingruff. -
Test the Hook:
- Create a test file with a simple linting error:
echo "import os\n\nprint( 'Hello World' )" > test.py
- Stage and commit the file:
git add test.py git commit -m "Test pre-commit hook" - The hook should block the commit, showing an error about the unused import or formatting issues.
- Create a test file with a simple linting error:
Depending on the project's configuration:
- Manual Fix: Remove unused imports or fix spacing.
- Auto-fix with Ruff:
or
ruff format test.py
(Both commands achieve a similar result in the latest versions of Ruff.)ruff check test.py --fix
After fixing, re-stage and commit again:
git add test.py
git commit -m "Fix linting issues"This time, the commit should succeed if all issues are resolved.
-
uv syncErrors- Ensure Python 3.11+ is installed.
- Confirm you have the correct permissions to install packages.
-
rufforpre-commitNot Found- Run
uv syncagain. - Make sure your shell is set to use the environment from
uv(viauv shellor by sourcing.venv/bin/activate). - Alternatively, use
uvx rufforuv tool install ruff.
- Run
-
Hook Doesn't Run
- Check that
.pre-commit-config.yamlreferencesruff. - Re-run
pre-commit install.
- Check that
-
Bypassing Hooks
- If you see developers bypassing hooks with
--no-verify, note that the checks won't run. For consistent code quality, discourage skipping hooks.
- If you see developers bypassing hooks with
That's it! You should now have a functioning development environment for metrics-utility with a pre-commit hook that catches lint and formatting issues via ruff.