Skip to content
Closed
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
36 changes: 36 additions & 0 deletions .github/workflows/codeflash.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Codeflash Optimization
on:
pull_request:
paths:
- 'pydantic_ai_slim/pydantic_ai/**'
workflow_dispatch:
concurrency:
# Any new push to the PR will cancel the previous run, so that only the latest code is optimized
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
optimize:
name: Optimize new Python code
# Don't run codeflash on codeflash-ai[bot] commits, prevent duplicate optimizations
if: ${{ github.actor != 'codeflash-ai[bot]' }}
runs-on: ubuntu-latest
env:
CODEFLASH_API_KEY: ${{ secrets.CODEFLASH_API_KEY }}
steps:
- name: 🛎️ Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: 🐍 Setup UV
uses: astral-sh/setup-uv@v6
with:
enable-cache: true
- uses: denoland/setup-deno@v2
with:
deno-version: v2.x
- name: 📦 Install Dependencies
run: |
uv sync --all-extras --all-packages --group lint --group docs
uv pip install -U codeflash
- name: ⚡️Codeflash Optimization
run: uv run codeflash --benchmark
1 change: 1 addition & 0 deletions pydantic_ai_slim/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ dev = [
"boto3-stubs[bedrock-runtime]",
"strict-no-cover>=0.1.1",
"pytest-xdist>=3.6.1",
"codeflash>=0.15.6",
]

[tool.hatch.metadata]
Expand Down
14 changes: 13 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ members = [

[dependency-groups]
# dev dependencies are defined in `pydantic-ai-slim/pyproject.toml` to allow for minimal testing
lint = ["mypy>=1.11.2", "pyright>=1.1.390", "ruff>=0.6.9"]
lint = [
"mypy>=1.11.2",
"pyright>=1.1.390",
"ruff>=0.6.9",
]
docs = [
"pydantic-ai[a2a]",
"black>=24.10.0",
Expand Down Expand Up @@ -287,3 +291,11 @@ check-hidden = true
# Ignore "formatting" like **L**anguage
ignore-regex = '\*\*[A-Z]\*\*[a-z]+\b'
ignore-words-list = 'asend,aci'

[tool.codeflash]
module-root = "pydantic_ai_slim/pydantic_ai"
tests-root = "tests"
test-framework = "pytest"
ignore-paths = []
formatter-cmds = ["ruff check --exit-zero --fix $file", "ruff format $file"]
benchmarks-root = "tests"
15 changes: 13 additions & 2 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def test_docs_examples( # noqa: C901
allow_model_requests: None,
env: TestEnv,
tmp_path_cwd: Path,
benchmark: Any,
):
mocker.patch('pydantic_ai.agent.models.infer_model', side_effect=mock_infer_model)
mocker.patch('pydantic_ai._utils.group_by_temporal', side_effect=mock_group_by_temporal)
Expand Down Expand Up @@ -222,9 +223,19 @@ def print(self, *args: Any, **kwargs: Any) -> None:
test_globals: dict[str, str] = {'__name__': dunder_name}

if eval_example.update_examples: # pragma: lax no cover
eval_example.run_print_update(example, call=call_name, module_globals=test_globals)
benchmark(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this fixture coming directly from codeflash?

Copy link

Choose a reason for hiding this comment

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

Yes.

This fixture is provided by codeflash & adapts based on the testing environment: it uses CodeFlash's internal benchmarking when Codeflash mechanisms are active, falls back to pytest-benchmark when available in standard pytest, or acts as a no-op otherwise to preserve normal test behavior.

eval_example.run_print_update,
example,
call=call_name,
module_globals=test_globals,
)
else:
eval_example.run_print_check(example, call=call_name, module_globals=test_globals)
benchmark(
eval_example.run_print_check,
example,
call=call_name,
module_globals=test_globals,
)


def print_callback(s: str) -> str:
Expand Down
Loading