Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable pre-commit #452

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 4 additions & 21 deletions .github/workflows/quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,9 @@ on: [pull_request]
jobs:
check_code_quality:
runs-on: ubuntu-latest
env:
UV_HTTP_TIMEOUT: 600 # max 10min to install deps

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"

# Setup venv
- name: Setup venv + uv
Copy link
Contributor Author

@antoinejeannot antoinejeannot Jan 31, 2025

Choose a reason for hiding this comment

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

pre-commit manages its own isolated venv, hence no more env management.
Also no need to install smolagents for lint & formating, only pre-commit and ruff are needed

run: |
pip install --upgrade uv
uv venv

- name: Install dependencies
run: uv pip install "smolagents[quality] @ ."

# Equivalent of "make quality" but step by step
- run: uv run ruff check examples src tests utils # linter
- run: uv run ruff format --check examples src tests utils # formatter
- run: uv run python utils/check_tests_in_ci.py
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now a pre-commit hook

python-version: "3.10" # lower bound of the supported Python versions
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this action uses some caching under the hood, which speeds up ci actions

- uses: pre-commit/[email protected]
23 changes: 18 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-merge-conflict
- id: check-yaml
- id: debug-statements

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.1
rev: v0.9.4
hooks:
- id: ruff
args:
- --fix
- id: ruff-format
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0

- repo: local
hooks:
- id: check-merge-conflict
- id: check-yaml
- id: check-tests-in-ci
name: check tests listed in ci
entry: .pre-commit-hooks/check_tests_in_ci.py
language: python
always_run: true
pass_filenames: false
types: [python, yaml]
files: ^tests/.*\.py$|^\.github/workflows/tests\.yml$
1 change: 1 addition & 0 deletions utils/check_tests_in_ci.py → .pre-commit-hooks/check_tests_in_ci.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
# coding=utf-8
# Copyright 2025-present, the HuggingFace Inc. team.
#
Expand Down
18 changes: 9 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
.PHONY: quality style test docs utils
.PHONY: quality style test

check_dirs := examples src tests utils
# quality and style targets are redundant
# they are kept not to introduce a breaking change in users' workflows.
# though style target is being deprecated and will be removed in the future.

# Check code quality of the source code
quality:
ruff check $(check_dirs)
ruff format --check $(check_dirs)
python utils/check_tests_in_ci.py
pre-commit run --all-files --show-diff-on-failure --color always

# Format source code automatically
style:
ruff check $(check_dirs) --fix
ruff format $(check_dirs)
pre-commit run --all-files --show-diff-on-failure --color always
echo "\nDeprecated target. Use 'make quality' instead."

# Run smolagents tests
test:
pytest ./tests/
pytest
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,22 @@ At any moment, feel welcome to open an issue, citing your exact error traces and
It's often even better to open a PR with your proposed fixes/changes!

To install dev dependencies, run:
```

```bash
pip install -e ".[dev]"
```

When making changes to the codebase, please check that it follows the repo's code quality requirements by running:
To check code quality of the source code:
```
make quality
```

If the checks fail, you can run the formatter with:
```
make style
```bash
make quality
```

And commit the changes.
If any issues are found, some of them will get fixed automatically, some will need manual intervention.
Once all issues are fixed, you can commit your changes!

To run tests locally, run this command:

```bash
make test
```
Expand Down
27 changes: 17 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,10 @@ openai = [
]
telemetry = [
"arize-phoenix",
"opentelemetry-sdk",
"opentelemetry-sdk",
"opentelemetry-exporter-otlp",
"openinference-instrumentation-smolagents>=0.1.1"
]
quality = [
"ruff>=0.9.0",
]
all = [
"smolagents[accelerate,audio,e2b,gradio,litellm,mcp,openai,transformers,telemetry]",
]
Expand All @@ -74,8 +71,10 @@ test = [
"rank-bm25", # For test_all_docs
]
dev = [
"smolagents[quality,test]",
"smolagents[test]",
"sqlalchemy", # for ./examples
"ruff==0.9.4", # for editor, synced with .pre-commit-config.yaml version
"pre-commit>=4.1.0",
]

[tool.pytest.ini_options]
Expand All @@ -84,11 +83,19 @@ addopts = "-sv --durations=0"

[tool.ruff]
line-length = 119
lint.ignore = [
"F403", # undefined-local-with-import-star
"E501", # line-too-long

[tool.ruff.lint]
select = [
"E", # pycodestyle errors
"F", # pyflakes
"I", # isort
"W", # pycodestyle warnings
]

ignore = [
"E501", # Line too long (handled by formatter)
"F403", # undefined-local-with-import-star
]
lint.select = ["E", "F", "I", "W"]

[tool.ruff.lint.per-file-ignores]
"examples/*" = [
Expand All @@ -101,4 +108,4 @@ lines-after-imports = 2

[project.scripts]
smolagent = "smolagents.cli:main"
webagent = "smolagents.vision_web_browser:main"
webagent = "smolagents.vision_web_browser:main"
Loading