Refactor: create_wrapper_function in instrument_existing_tests to reduce complexity and isolate wrapper-building concerns#2069
Open
shreejaykurhade wants to merge 3 commits intocodeflash-ai:mainfrom
Open
Conversation
…ce complexity and isolate wrapper-building concerns
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
The
create_wrapper_functionimplementation incodeflash/code_utils/instrument_existing_tests.pyhad become a large monolithic AST builder responsible for several distinct concerns at the same time:Because all of this logic lived in one function, the code was difficult to review, difficult to modify safely, and unnecessarily risky for future instrumentation work.
Previous Issue #526
The main issue was not functional breakage but maintainability risk.
The function had grown large enough that:
This is the kind of function that tends to slow down future development even when it still works correctly today.
Improvement Made
The wrapper generation logic was refactored into smaller private helper builders, each with a single responsibility.
The refactor separates:
After the refactor,
create_wrapper_functionacts as a thin orchestrator that assembles the wrapper in the correct order.Why This Is Better
This change improves the codebase in several ways:
Scope
This was intentionally kept as a pure refactor.
There are no intended changes to:
Validation
The refactor was validated with focused instrumentation coverage:
uv run pytest -q tests/test_inject_profiling_used_frameworks.pyuv run ruff check codeflash/code_utils/instrument_existing_tests.pyThese checks confirmed that the generated wrapper output remains correct for the covered framework and mode combinations.
Notes On Full Test Run
Some broader
uv run pytestcollection errors may still appear in this workspace, but they are unrelated to this refactor. They come from missing optional test dependencies incode_to_optimizetest modules and are outside the scope of this change.ERROR code_to_optimize/code_directories/retriever/import_test.py
ERROR code_to_optimize/tests/pytest/test_book_catalog.py
ERROR code_to_optimize/tests/pytest/test_book_catalog_2.py
ERROR code_to_optimize/tests/pytest/test_book_catalog_3.py
ERROR code_to_optimize/tests/pytest/test_jax_jit_code.py
ERROR code_to_optimize/tests/pytest/test_numba_jit_code.py
ERROR code_to_optimize/tests/pytest/test_torch_jit_code.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 9 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=================================== 1 skipped, 2 warnings, 9 errors in 3.11s
Risk Assessment
Risk is low.
The change is structural, localized, and supported by output-oriented tests around instrumentation generation. Review attention should focus on statement ordering and equivalence of emitted wrapper code.
Files Changed
codeflash/code_utils/instrument_existing_tests.py