refactor: inline async decorators to remove codeflash import dependency#1518
Merged
refactor: inline async decorators to remove codeflash import dependency#1518
Conversation
Instead of injecting `from codeflash.code_utils.codeflash_wrap_decorator import ...` into instrumented source files, inject the decorator function definitions directly. This removes the hard dependency on the codeflash package being importable at runtime in the target environment, matching the pattern already used for sync instrumentation.
Replace inline code injection with a helper file approach that writes decorator implementations to a separate codeflash_async_wrapper.py file. This removes the codeflash package import dependency from instrumented source files while keeping line numbers stable (only 1 import + 1 decorator line added, same as before). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…e file Write all three async decorator implementations into one helper file to avoid overwrite issues when switching modes. Clean up the helper file in revert_code_and_helpers and early-exit paths so it doesn't persist in the user's project root after optimization.
…rget The e2e test expects codeflash to detect and fix the intentional use of blocking time.sleep() in an async function. Using asyncio.sleep() removes the optimization opportunity and causes the CI job to fail. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The optimized code removes `import time`, shifting all function lines up by 1. Update expected_lines from [10-20] to [9-19] to match. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
PR Review SummaryPrek ChecksAll checks passed (ruff check, ruff format). No issues found. MypyNo new type errors introduced by this PR. All mypy errors in the changed files are pre-existing. Code ReviewNo critical issues found. The refactoring cleanly replaces the
Test Coverage
Last updated: 2026-02-18 |
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.
Summary
AsyncDecoratorImportAdderCST transformer with standalone helper-file approach: decorator implementations are written to acodeflash_async_wrapper.pyfile in the project root, and source files import from that module instead offrom codeflash.code_utils.codeflash_wrap_decorator import ...codeflashpackage being importable at runtime in the target environment, matching the pattern already used for sync instrumentationget_behavior_async_inline_code,get_performance_async_inline_code,get_concurrency_async_inline_code)project_rootparameter toadd_async_decorator_to_functionso the helper file is written to a directory onsys.path; all call sites infunction_optimizer.pyupdatedasync_e2e/main.pytest fixture: replace blockingtime.sleepwithawait asyncio.sleepTest plan
uv run pytest tests/test_instrument_async_tests.py -x -v— 14 passeduv run pytest tests/test_async_run_and_parse_tests.py -x -v— 8 passeduv run pytest tests/test_async_wrapper_sqlite_validation.py tests/test_async_concurrency_decorator.py -x -v— 15 passed (unchanged)from codeflashimportsruff checkclean on changed files