Avoid formatting that causes large diffs (CF-637) #136

Closed
codeflash-ai wants to merge 30 commits from skip-formatting-for-large-diffs into main
Owner

User description

Details

Applied the user-provided formatter commands to the file with the optimized function, calculated the unified diff, and skipped formatting if the number of changed lines exceeds 100 (configurable).


PR Type

Enhancement, Tests


Description

  • Add diff-count check to skip large formatting

  • Integrate should_format_file into formatter logic

  • Introduce sample files for formatting tests

  • Add tests for skip/format behavior based on diff size


Changes walkthrough 📝

Relevant files
Enhancement
formatter.py
Add diff-based formatting skip                                                     

codeflash/code_utils/formatter.py

  • Introduce should_format_file detecting black diff count
  • Use skip logic in format_code when diff too large
  • Add debug and warning logs for skip and errors
  • +34/-2   
    Tests
    test_formatter.py
    Add tests for formatting skip logic                                           

    tests/test_formatter.py

  • Add _run_formatting_test helper for formatting validation
  • Add tests for few vs many diff scenarios
  • Update imports and test setup for new functionality
  • +68/-0   
    Miscellaneous
    few_formatting_errors.py
    Add few-formatting-errors sample file                                       

    code_to_optimize/few_formatting_errors.py

  • Add sample file with minor formatting issues
  • Define BadlyFormattedClass and process_data function
  • +47/-0   
    many_formatting_errors.py
    Add many-formatting-errors sample file                                     

    code_to_optimize/many_formatting_errors.py

  • Add extensive sample file with major formatting errors
  • Include classes, utility functions, and nested logic
  • +147/-0 

    Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • ### **User description** ### _Details_ Applied the user-provided formatter commands to the file with the optimized function, calculated the unified diff, and skipped formatting if the number of changed lines exceeds 100 (configurable). ___ ### **PR Type** Enhancement, Tests ___ ### **Description** - Add diff-count check to skip large formatting - Integrate should_format_file into formatter logic - Introduce sample files for formatting tests - Add tests for skip/format behavior based on diff size ___ ### **Changes walkthrough** 📝 <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Enhancement</strong></td><td><table> <tr> <td> <details> <summary><strong>formatter.py</strong><dd><code>Add diff-based formatting skip</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary> <hr> codeflash/code_utils/formatter.py <li>Introduce should_format_file detecting black diff count<br> <li> Use skip logic in format_code when diff too large<br> <li> Add debug and warning logs for skip and errors </details> </td> <td><a href="https://github.com/codeflash-ai/codeflash/pull/274/files#diff-ead54b6ab4522d27ae1bc0af93885ab05a8f49ea3c96308972c17deaa97515d2">+34/-2</a>&nbsp; &nbsp; </td> </tr> </table></td></tr><tr><td><strong>Tests</strong></td><td><table> <tr> <td> <details> <summary><strong>test_formatter.py</strong><dd><code>Add tests for formatting skip logic</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary> <hr> tests/test_formatter.py <li>Add _run_formatting_test helper for formatting validation<br> <li> Add tests for few vs many diff scenarios<br> <li> Update imports and test setup for new functionality </details> </td> <td><a href="https://github.com/codeflash-ai/codeflash/pull/274/files#diff-e0cd3f215f68d0608393c012e268cb2f896c1e88315c97fa77e114d400f5283b">+68/-0</a>&nbsp; &nbsp; </td> </tr> </table></td></tr><tr><td><strong>Miscellaneous</strong></td><td><table> <tr> <td> <details> <summary><strong>few_formatting_errors.py</strong><dd><code>Add few-formatting-errors sample file</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary> <hr> code_to_optimize/few_formatting_errors.py <li>Add sample file with minor formatting issues<br> <li> Define BadlyFormattedClass and process_data function </details> </td> <td><a href="https://github.com/codeflash-ai/codeflash/pull/274/files#diff-483c2db92597a507ef06dd6489322bf8dffebf9464a891cf190830a01ce783a3">+47/-0</a>&nbsp; &nbsp; </td> </tr> <tr> <td> <details> <summary><strong>many_formatting_errors.py</strong><dd><code>Add many-formatting-errors sample file</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary> <hr> code_to_optimize/many_formatting_errors.py <li>Add extensive sample file with major formatting errors<br> <li> Include classes, utility functions, and nested logic </details> </td> <td><a href="https://github.com/codeflash-ai/codeflash/pull/274/files#diff-5da845185192119f4edc7205b9a63010e7112c49d3ea5eb081c9fa6d8495ea95">+147/-0</a>&nbsp; </td> </tr> </table></td></tr></tr></tbody></table> ___ > <details> <summary> Need help?</summary><li>Type <code>/help how to ...</code> in the comments thread for any questions about PR-Agent usage.</li><li>Check out the <a href="https://qodo-merge-docs.qodo.ai/usage-guide/">documentation</a> for more information.</li></details>
    codeflash-ai added 30 commits 2026-05-05 00:27:33 +00:00
    add a todo comment
    Update formatter.py
    Some checks are pending
    CodeFlash / Optimize new Python code (pull_request) Waiting to run
    PR Labeler / label-workflow-changes (pull_request) Waiting to run
    Mypy Type Checking for CLI / type-check-cli (pull_request) Waiting to run
    / Run pr agent on every pull request, respond to user comments (pull_request) Waiting to run
    Lint / Run pre-commit hooks (pull_request) Waiting to run
    unit-tests / unit-tests (3.10) (pull_request) Waiting to run
    unit-tests / unit-tests (3.11) (pull_request) Waiting to run
    unit-tests / unit-tests (3.12) (pull_request) Waiting to run
    unit-tests / unit-tests (3.9) (pull_request) Waiting to run
    94e64d3218
    Fix ruff lint
    codeflash-ai closed this pull request 2026-05-05 00:27:35 +00:00
    Some checks are pending
    CodeFlash / Optimize new Python code (pull_request) Waiting to run
    PR Labeler / label-workflow-changes (pull_request) Waiting to run
    Mypy Type Checking for CLI / type-check-cli (pull_request) Waiting to run
    / Run pr agent on every pull request, respond to user comments (pull_request) Waiting to run
    Lint / Run pre-commit hooks (pull_request) Waiting to run
    unit-tests / unit-tests (3.10) (pull_request) Waiting to run
    unit-tests / unit-tests (3.11) (pull_request) Waiting to run
    unit-tests / unit-tests (3.12) (pull_request) Waiting to run
    unit-tests / unit-tests (3.9) (pull_request) Waiting to run

    Pull request closed

    Sign in to join this conversation.
    No reviewers
    No milestone
    No project
    No assignees
    1 participant
    Notifications
    Due date
    The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

    No due date set.

    Dependencies

    No dependencies set.

    Reference: codeflash-ai/codeflash#136
    No description provided.