Skip to content

Commit 753829b

Browse files
committed
add codeflash GHA
1 parent 091c499 commit 753829b

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

.github/workflows/codeflash.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Codeflash Optimization
2+
on:
3+
pull_request:
4+
paths:
5+
- 'pydantic_ai_slim/pydantic_ai/**'
6+
workflow_dispatch:
7+
concurrency:
8+
# Any new push to the PR will cancel the previous run, so that only the latest code is optimized
9+
group: ${{ github.workflow }}-${{ github.ref }}
10+
cancel-in-progress: true
11+
jobs:
12+
optimize:
13+
name: Optimize new Python code
14+
# Don't run codeflash on codeflash-ai[bot] commits, prevent duplicate optimizations
15+
if: ${{ github.actor != 'codeflash-ai[bot]' }}
16+
runs-on: ubuntu-latest
17+
env:
18+
CODEFLASH_API_KEY: ${{ secrets.CODEFLASH_API_KEY }}
19+
steps:
20+
- name: 🛎️ Checkout
21+
uses: actions/checkout@v4
22+
with:
23+
fetch-depth: 0
24+
- name: 🐍 Setup UV
25+
uses: astral-sh/setup-uv@v6
26+
with:
27+
enable-cache: true
28+
- uses: denoland/setup-deno@v2
29+
with:
30+
deno-version: v2.x
31+
- name: 📦 Install Dependencies
32+
run: uv sync --frozen --all-extras --all-packages --group lint --group docs
33+
- name: Functions to Optimize
34+
id: discover_step
35+
run: |
36+
uv run python -c "
37+
from codeflash.cli_cmds.cli import parse_args, process_pyproject_config
38+
from codeflash.verification.verification_utils import TestConfig
39+
from codeflash.discovery.functions_to_optimize import get_functions_to_optimize
40+
import os
41+
args = process_pyproject_config(parse_args())
42+
t = TestConfig(tests_root=args.tests_root, tests_project_rootdir=args.test_project_root, project_root_path=args.project_root, test_framework=args.test_framework, pytest_cmd=args.pytest_cmd,)
43+
f = get_functions_to_optimize(test_cfg=t, ignore_paths=args.ignore_paths, project_root=args.project_root, module_root=args.module_root,optimize_all=None, replay_test=None, file=None, only_get_this_function=None)
44+
functions = ' '.join([fto.function_name for ftol in f[0].values() for fto in ftol])
45+
open(os.environ.get('GITHUB_OUTPUT'), 'a').write(f'functions_to_optimize={functions}')
46+
"
47+
- name: ⚡️Codeflash Optimization
48+
run: uv run codeflash optimize --only-functions ${{ steps.discover_step.outputs.functions_to_optimize }} -m pytest --inline-snapshot=disable tests/test_examples.py

pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,11 @@ check-hidden = true
287287
# Ignore "formatting" like **L**anguage
288288
ignore-regex = '\*\*[A-Z]\*\*[a-z]+\b'
289289
ignore-words-list = 'asend,aci'
290+
291+
[tool.codeflash]
292+
module-root = "pydantic_ai_slim/pydantic_ai"
293+
tests-root = "tests"
294+
test-framework = "pytest"
295+
ignore-paths = []
296+
formatter-cmds = ["ruff check --exit-zero --fix $file", "ruff format $file"]
297+
benchmarks-root = "tests"

tests/test_examples.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def test_docs_examples( # noqa: C901
117117
allow_model_requests: None,
118118
env: TestEnv,
119119
tmp_path_cwd: Path,
120+
benchmark: Any,
120121
):
121122
mocker.patch('pydantic_ai.agent.models.infer_model', side_effect=mock_infer_model)
122123
mocker.patch('pydantic_ai._utils.group_by_temporal', side_effect=mock_group_by_temporal)
@@ -216,9 +217,19 @@ def print(self, *args: Any, **kwargs: Any) -> None:
216217
test_globals: dict[str, str] = {'__name__': dunder_name}
217218

218219
if eval_example.update_examples: # pragma: lax no cover
219-
eval_example.run_print_update(example, call=call_name, module_globals=test_globals)
220+
benchmark(
221+
eval_example.run_print_update,
222+
example,
223+
call=call_name,
224+
module_globals=test_globals,
225+
)
220226
else:
221-
eval_example.run_print_check(example, call=call_name, module_globals=test_globals)
227+
benchmark(
228+
eval_example.run_print_check,
229+
example,
230+
call=call_name,
231+
module_globals=test_globals,
232+
)
222233

223234

224235
def print_callback(s: str) -> str:

0 commit comments

Comments
 (0)