Skip to content

Fix coverage function identification using qualified names - #1457

Merged
KRRT7 merged 6 commits into
mainfrom
fix-coverage-qualified-name
Feb 12, 2026
Merged

Fix coverage function identification using qualified names#1457
KRRT7 merged 6 commits into
mainfrom
fix-coverage-qualified-name

Conversation

@KRRT7

@KRRT7 KRRT7 commented Feb 12, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Coverage system used bare function_name (e.g., __init__) instead of qualified_name (e.g., HttpInterface.__init__) to identify the main function, causing it to match the wrong class's method when multiple classes define methods with the same name
  • build_fully_qualified_name now skips re-qualifying already-qualified names
  • extract_dependent_function compares using the bare portion of a qualified main function name
  • grab_dependent_function_from_coverage_data fallback search now uses exact or dot-bounded suffix match instead of substring match

Test plan

  • 24 unit tests covering build_fully_qualified_name, extract_dependent_function, and grab_dependent_function_from_coverage_data
  • Manual verification with a function like HttpInterface.__init__ that has multiple __init__ methods in dependencies

KRRT7 and others added 3 commits February 11, 2026 23:24
The coverage system was using bare function_name (e.g., "__init__")
instead of qualified_name (e.g., "HttpInterface.__init__"), causing
it to match the wrong class's method when multiple classes define
the same method name (like __init__).

Changes:
- function_optimizer.py: pass qualified_name to parse_test_results
- build_fully_qualified_name: skip re-qualifying already-qualified names
- extract_dependent_function: compare using bare name from qualified input
- grab_dependent_function_from_coverage_data: replace substring match with
  exact or dot-bounded suffix match
…ytest_parallelization

- PrComment.py: rename loop variable to avoid shadowing the result dict
- concolic_utils.py: add None guard for tree, annotate new_body as list[ast.stmt]
- pytest_parallelization.py: separate set/list variables, annotate result_groups
@claude

claude Bot commented Feb 12, 2026

Copy link
Copy Markdown
Contributor

PR Review Summary

Prek Checks

✅ All checks pass. One formatting issue was auto-fixed by ruff format in codeflash/code_utils/coverage_utils.py (extra blank line removed) and committed.

Mypy Checks

✅ All 6 changed files (excluding function_optimizer.py) pass mypy with no errors.
⚠️ function_optimizer.py has 146 pre-existing mypy errors — none introduced by this PR.

Code Review

No critical issues found. The changes are well-focused and correct:

  • coverage_utils.py: extract_dependent_function now correctly compares using the bare portion of a qualified main function name (e.g., __init__ from HttpInterface.__init__), preventing false exclusions. Early return optimization when >1 dependent function found. build_fully_qualified_name now short-circuits for already-qualified names.
  • function_optimizer.py: Correctly passes qualified_name instead of function_name to parse_test_results, fixing the root cause of wrong-class coverage matching.
  • verification/coverage_utils.py: Fallback matching now uses exact match or dot-boundary suffix match instead of substring in, preventing __init__ from matching PathAwareCORSMiddleware.__init__.
  • PrComment.py: Variable rename (resultcounts) avoids shadowing the outer result dict — good cleanup.
  • concolic_utils.py: Added tree is None guard and type annotation — mypy fixes.
  • pytest_parallelization.py: Variable rename and type annotations — mypy fixes.
  • New test file: Comprehensive 228-line test suite covering all new logic paths.

Test Coverage

File Main PR Change
codeflash/code_utils/concolic_utils.py 88% 88%
codeflash/code_utils/coverage_utils.py 89% 98% +9% ✅
codeflash/github/PrComment.py 71% 71%
codeflash/optimization/function_optimizer.py 18% 18%
codeflash/verification/coverage_utils.py 14% 22% +8% ✅
tests/code_utils/test_coverage_utils.py (NEW) 100%
Overall project 78% 78% No regression

Coverage analysis:

  • ✅ New test file at 100% coverage
  • coverage_utils.py increased from 89% → 98% — new logic well-tested
  • verification/coverage_utils.py increased from 14% → 22% — fallback matching tested
  • ✅ No coverage regressions anywhere
  • ✅ All 2345 tests pass (8 pre-existing failures in test_tracer.py, unrelated)

Last updated: 2026-02-12T05:30:00Z

The optimized code achieves a **197% speedup (28.5ms → 9.57ms)** through three strategic optimizations that dramatically reduce expensive AST parsing operations:

## Key Optimizations

**1. Early String Filtering (74% time reduction in parsing)**
The optimization adds a lightweight heuristic check `if "def" not in code_string.code` before calling `ast.parse()`. Since function definitions require the `def` keyword, strings without it can be skipped entirely. In the profiler results, this reduced AST parsing from 32.5ms (80.5% of original runtime) to 9.9ms (74.2% of optimized runtime). The test results show dramatic improvements for large-scale scenarios:
- `test_large_scale_many_code_strings_single_dependent_function`: **6839% faster** (4.45ms → 64.1μs)
- `test_large_scale_with_preexisting_objects_and_many_irrelevant_entries`: **4193% faster** (2.26ms → 52.7μs)

**2. Hoisted Main Function Name Computation**
Moving `bare_main` calculation outside the loop (from line 13 to line 10) eliminates redundant string operations that were executed once per code string. This simple reordering saves repeated `rsplit()` calls.

**3. Early Exit on Multiple Dependencies**
The optimization checks `if len(dependent_functions) > 1: return False` immediately after adding each function name, rather than waiting until all code strings are processed. This allows the function to short-circuit as soon as it detects the failure condition, avoiding unnecessary AST parsing of remaining code strings.

## Why This Matters

Based on the function references, `extract_dependent_function` is called during test generation workflows where it processes potentially hundreds or thousands of code strings. The optimization is particularly effective when:
- Most code strings don't contain function definitions (common in test contexts with imports, variables, etc.)
- Multiple dependent functions exist (early exit prevents wasted parsing)
- Code bases have many test-related code strings that aren't function definitions

The optimizations preserve exact behavior while intelligently avoiding expensive operations, making the code significantly more efficient in real-world usage patterns where the function processes large volumes of code strings.
@codeflash-ai

codeflash-ai Bot commented Feb 12, 2026

Copy link
Copy Markdown
Contributor

⚡️ Codeflash found optimizations for this PR

📄 197% (1.97x) speedup for extract_dependent_function in codeflash/code_utils/coverage_utils.py

⏱️ Runtime : 28.5 milliseconds 9.57 milliseconds (best of 13 runs)

A dependent PR with the suggested changes has been created. Please review:

If you approve, it will be merged into this PR (branch fix-coverage-qualified-name).

Static Badge

…2026-02-12T04.58.15

⚡️ Speed up function `extract_dependent_function` by 197% in PR #1457 (`fix-coverage-qualified-name`)
@codeflash-ai

codeflash-ai Bot commented Feb 12, 2026

Copy link
Copy Markdown
Contributor

This PR is now faster! 🚀 @KRRT7 accepted my optimizations from:

@KRRT7
KRRT7 merged commit 0f9c06b into main Feb 12, 2026
26 of 27 checks passed
@KRRT7
KRRT7 deleted the fix-coverage-qualified-name branch February 12, 2026 05:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant