diff --git a/.github/workflows/codeflash.yml b/.github/workflows/codeflash.yml new file mode 100644 index 0000000000..36ad5dbe42 --- /dev/null +++ b/.github/workflows/codeflash.yml @@ -0,0 +1,32 @@ +name: Codeflash +on: + pull_request: + paths: + # So that this workflow only runs when code within the target module is modified + - '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 + - name: 📦 Install Dependencies + run: uv sync --all-extras --all-groups + - name: ⚡️Codeflash Optimization + run: uv run codeflash \ No newline at end of file diff --git a/pydantic_ai_slim/pydantic_ai/bubble_sort.py b/pydantic_ai_slim/pydantic_ai/bubble_sort.py new file mode 100644 index 0000000000..3f268e7e98 --- /dev/null +++ b/pydantic_ai_slim/pydantic_ai/bubble_sort.py @@ -0,0 +1,11 @@ +def sorter(arr): + #trigger workflow now + print("codeflash stdout: Sorting list") + for i in range(len(arr)): + for j in range(len(arr) - 1): + if arr[j] > arr[j + 1]: + temp = arr[j] + arr[j] = arr[j + 1] + arr[j + 1] = temp + print(f"result: {arr}") + return arr diff --git a/pyproject.toml b/pyproject.toml index 9ba1763adb..b78161c016 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -283,3 +283,10 @@ 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"] \ No newline at end of file diff --git a/tests/test_bubble_sort.py b/tests/test_bubble_sort.py new file mode 100644 index 0000000000..eccad6e099 --- /dev/null +++ b/tests/test_bubble_sort.py @@ -0,0 +1,15 @@ +from code_to_optimize.bubble_sort import sorter + + +def test_sort(): + input = [5, 4, 3, 2, 1, 0] + output = sorter(input) + assert output == [0, 1, 2, 3, 4, 5] + + input = [5.0, 4.0, 3.0, 2.0, 1.0, 0.0] + output = sorter(input) + assert output == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] + + input = list(reversed(range(5000))) + output = sorter(input) + assert output == list(range(5000))