Skip to content

Conversation

@KRRT7
Copy link
Contributor

@KRRT7 KRRT7 commented Apr 10, 2025

PR Type

  • Enhancement
  • Tests

Description

  • Add new function format_code_in_memory with Black support.

  • Replace sort_imports calls with format_code_in_memory.

  • Update tests to use new formatting and sorting method.

  • Loosen pytest version constraints and add Black dependency in CI.


Changes walkthrough 📝

Relevant files
Enhancement
3 files
formatter.py
Introduce `format_code_in_memory` with Black integration 
+18/-7   
instrument_existing_tests.py
Replace isort call with `format_code_in_memory` in instrumentation
+2/-4     
function_optimizer.py
Update import sorting to use new formatting function         
+3/-3     
Tests
3 files
test_formatter.py
Replace `sort_imports` with `format_code_in_memory` in tests
+7/-7     
test_instrument_all_and_run.py
Update test assertions to use new formatting function       
+7/-4     
test_instrument_tests.py
Multiple test updates for formatting consistency                 
+198/-89
Configuration changes
1 files
unit-tests.yaml
Add --disable-warnings option to pytest command                   
+1/-1     
Dependencies
1 files
pyproject.toml
Loosen pytest constraint and add Black dependency               
+2/-1     

Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:31 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:31 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:31 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:31 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:31 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:31 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:31 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:31 — with GitHub Actions Error
    @github-actions
    Copy link

    github-actions bot commented Apr 10, 2025

    PR Reviewer Guide 🔍

    (Review updated until commit d3dcc20)

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Exception Handling

    The new function format_code_in_memory catches broad exceptions which may hide underlying issues during formatting or import sorting. It is recommended to consider more specific exception types or provide detailed logging to help diagnose issues.

    def format_code_in_memory(code: str, *, imports_only: bool = False) -> str:
        if imports_only:
            try:
                sorted_code = imports_sort(code)
            except Exception:  # noqa: BLE001
                logger.debug("Failed to sort imports with isort.")
                return code
            return sorted_code
        try:
            formatted_code = black.format_str(code, mode=black.FileMode())
            formatted_code = imports_sort(formatted_code)
        except Exception:  # noqa: BLE001
            logger.debug("Failed to format code with black.")
            return code
    Test Fragility

    Tests comparing formatted output may become brittle if Black's formatting rules change. It is worth checking that these tests remain resilient under future updates to the formatter.

    new_code = format_code_in_memory(original_code, imports_only=True)
    assert new_code == "import os\n"

    @github-actions
    Copy link

    github-actions bot commented Apr 10, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Improve formatting error logging

    Enhance error handling by logging the exception details to provide better context on
    formatting failures.

    codeflash/code_utils/formatter.py [61-66]

     try:
         formatted_code = black.format_str(code, mode=black.FileMode())
         formatted_code = imports_sort(formatted_code)
    -except Exception:  # noqa: BLE001
    -    logger.debug("Failed to format code with black.")
    +except Exception as e:  # noqa: BLE001
    +    logger.debug(f"Failed to format code with black: {e}")
         return code
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion enhances error logging by capturing and reporting the exception details, providing better debugging context. This is a useful, though moderate, improvement to the error handling in the affected code.

    Medium

    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:36 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:36 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:36 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:36 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:36 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:36 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:36 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:36 — with GitHub Actions Error
    @github-actions github-actions bot added the workflow-modified This PR modifies GitHub Actions workflows label Apr 10, 2025
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:56 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:56 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:56 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:56 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:56 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:56 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:56 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:56 — with GitHub Actions Error
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:58 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 10, 2025 06:58 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 11, 2025 04:47 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 11, 2025 04:47 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 11, 2025 04:47 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 11, 2025 04:47 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 11, 2025 04:47 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 11, 2025 04:47 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 11, 2025 04:47 — with GitHub Actions Error
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 11, 2025 04:49 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 11, 2025 04:49 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 11, 2025 04:49 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 11, 2025 04:49 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 11, 2025 04:49 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 11, 2025 04:49 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 11, 2025 04:49 — with GitHub Actions Failure
    @KRRT7 KRRT7 had a problem deploying to external-trusted-contributors April 11, 2025 04:49 — with GitHub Actions Error
    @KRRT7 KRRT7 closed this Apr 11, 2025
    @KRRT7 KRRT7 force-pushed the dependencies-update branch from 1248a4a to 7d7340c Compare April 11, 2025 06:36
    @github-actions
    Copy link

    Persistent review updated to latest commit d3dcc20

    @github-actions
    Copy link

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Improve formatting error logging

    Enhance error handling by logging the exception details to provide better context on
    formatting failures.

    codeflash/code_utils/formatter.py [61-66]

     try:
         formatted_code = black.format_str(code, mode=black.FileMode())
         formatted_code = imports_sort(formatted_code)
    -except Exception:  # noqa: BLE001
    -    logger.debug("Failed to format code with black.")
    +except Exception as e:  # noqa: BLE001
    +    logger.debug(f"Failed to format code with black: {e}")
         return code
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion enhances error logging by capturing and reporting the exception details, providing better debugging context. This is a useful, though moderate, improvement to the error handling in the affected code.

    Medium

    @KRRT7 KRRT7 closed this Apr 12, 2025
    @KRRT7 KRRT7 deleted the dependencies-update branch April 12, 2025 13:08
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    Review effort 4/5 workflow-modified This PR modifies GitHub Actions workflows

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    2 participants