Skip to content

feat: include external class __init__ signatures with transitive type deps in testgen context#1481

Merged
KRRT7 merged 11 commits intomainfrom
include-external-class-inits-in-testgen
Feb 13, 2026
Merged

feat: include external class __init__ signatures with transitive type deps in testgen context#1481
KRRT7 merged 11 commits intomainfrom
include-external-class-inits-in-testgen

Conversation

@KRRT7
Copy link
Collaborator

@KRRT7 KRRT7 commented Feb 13, 2026

Summary

  • Extract __init__ signatures from directly imported external (site-packages) classes so the LLM can construct them correctly in generated tests
  • Resolve transitive type dependencies: classes referenced in __init__ type annotations are also extracted (BFS with depth limit of 2)
  • Cycle-safe and dedup-safe via processed_classes set and emitted_names tracking

Test plan

  • All 100 existing + new tests pass (uv run pytest tests/test_code_context_extractor.py)
  • mypy clean on changed file
  • prek (ruff check + format) passes

KRRT7 and others added 3 commits February 13, 2026 08:42
…sses in testgen context

When generating regression tests, the LLM needs to know how to construct
external types used as function parameters. This extends the testgen context
to include __init__ signatures from external (site-packages) classes that
are directly imported, complementing the existing base class init extraction.
Tests cover: extracting __init__ from site-packages classes (click.Option),
skipping project classes, non-classes, already-defined classes, builtins,
classes with trivial object.__init__, and empty import scenarios.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@claude
Copy link
Contributor

claude bot commented Feb 13, 2026

PR Review Summary

Prek Checks

All checks pass. Ruff check and ruff format both pass cleanly.

Mypy

No new mypy errors introduced by this PR. Pre-existing errors in instrument.py (5 errors: re.Match missing type params, import-untyped) and parse_test_output.py (19 errors) remain unchanged from main.

Code Review

No critical bugs, security vulnerabilities, or breaking API changes found.

New code in code_context_extractor.py (4 new functions + integration into build_testgen_context):

  • extract_classes_from_type_hint — correctly unwraps generic type annotations recursively
  • resolve_transitive_type_deps — finds external classes referenced in __init__ type hints
  • extract_init_stub_for_class — builds a stub with only the __init__ method
  • get_external_class_inits — BFS traversal with cycle detection (processed_classes set) and depth limit (MAX_TRANSITIVE_DEPTH = 2)

All edge cases are handled: SyntaxError, ImportError, OSError, missing __init__, object.__init__, circular refs, and duplicate class names.

Minor nits (non-blocking):

  • resolve_transitive_type_deps reuses the name init_method for both the outer class and inner loop dep classes (variable shadowing — works correctly but slightly confusing)
  • worklist.pop(0) is O(n); collections.deque.popleft() would be O(1), though the list is always small

Other changes: Import cleanup in parse_test_output.py, Path.as_posix() improvement in instrument.py, and import path fix in test_vitest_junit.py — all clean.

Test Coverage

File Main PR Change
codeflash/context/code_context_extractor.py 93% (695 stmts) 92% (837 stmts) -1%
codeflash/languages/javascript/instrument.py 71% (638 stmts) 71% (637 stmts) 0%
codeflash/verification/parse_test_output.py 59% (515 stmts) 58% (513 stmts) -1%

The main file (code_context_extractor.py) added 142 new statements with only a 1% coverage drop, meaning the 18 new tests provide strong coverage of the new functionality. No coverage regressions of concern.

Test results: 2384 passed, 57 skipped, 8 failed (all 8 failures are pre-existing in test_tracer.py, unrelated to this PR).


Last updated: 2026-02-13

Add BFS-based transitive resolution so that classes referenced in __init__
type annotations of imported external classes are also extracted. This gives
the LLM the constructor signatures it needs to instantiate parameter types.
@KRRT7 KRRT7 changed the title feat: include external class __init__ signatures in testgen context feat: include external class __init__ signatures with transitive type deps in testgen context Feb 13, 2026
github-actions bot and others added 7 commits February 13, 2026 14:39
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The formatter correctly removed the unused re-exports from
parse_test_output.py. Update the test to import directly from
codeflash.languages.javascript.parse.
os.path.relpath returns backslashes on Windows. The backslash-to-slash
conversion happened after the ./ / ../ prefix check, so the check
failed and prepended ./ producing ./../src/... paths. Use
Path.as_posix() instead of manual string replacement.
@@ -32,10 +34,6 @@
)
from codeflash.verification.coverage_utils import CoverageUtils, JestCoverageUtils

Copy link
Contributor

@claude claude bot Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Fixed in latest commit

@KRRT7 KRRT7 merged commit 42a1150 into main Feb 13, 2026
27 of 28 checks passed
@KRRT7 KRRT7 deleted the include-external-class-inits-in-testgen branch February 13, 2026 22:51
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