Skip to content

fix: detect attribute-referenced methods as used in unused helper detection#1650

Open
aseembits93 wants to merge 2 commits intomainfrom
fix/unused-helper-attribute-refs
Open

fix: detect attribute-referenced methods as used in unused helper detection#1650
aseembits93 wants to merge 2 commits intomainfrom
fix/unused-helper-attribute-refs

Conversation

@aseembits93
Copy link
Contributor

Summary

  • detect_unused_helper_functions only walked ast.Call nodes to find which helpers were "used" by the optimized entrypoint
  • Methods referenced via attribute assignment (e.g., self._parse1 = self._parse_literal — a common callback/state-machine pattern) were missed, causing them to be falsely classified as unused and reverted to their original unoptimized code
  • Added an elif branch that also walks ast.Attribute nodes with ast.Name values, using the same qualified-name and import-mapping logic as the call-based detection

Test plan

  • 3 new tests in tests/test_mock_candidate_replacement.py (mock candidate with PSBaseParser class methods used as callbacks)
  • 17 existing tests in tests/test_unused_helper_revert.py all pass unchanged

🤖 Generated with Claude Code

@claude
Copy link
Contributor

claude bot commented Feb 24, 2026

PR Review Summary

Prek Checks

✅ All checks pass — ruff check and ruff format both passed with no issues.

Mypy

  • unused_definition_remover.py: All 12 mypy errors are pre-existing (at lines not touched by this PR: 136, 145, 164, 173, 225, 496, 499, 567, 640, 643, 673, 764). No new type errors introduced.
  • tests/test_mock_candidate_replacement.py (new file): 10 mypy errors — mostly missing type annotations on test functions (no-untyped-def) and call-arg/arg-type errors. These follow the same patterns as the existing test_unused_helper_revert.py on main and are not regressions.

Code Review

Core fix (unused_definition_remover.py)

  • The new elif isinstance(node, ast.Attribute) and isinstance(node.value, ast.Name) branch correctly mirrors the existing ast.Call detection logic for attribute references
  • Properly handles both self.method references (adding qualified class name) and module.attr references (with import mapping)
  • Duplicate comments removed — good cleanup
  • No logic bugs or security issues found

Test file (test_mock_candidate_replacement.py)

  • 3 well-structured tests covering: full replacement pipeline, unused helper detection with attribute refs, and valid Python output verification
  • Tests follow the same patterns as existing test_unused_helper_revert.py
  • Note: Tests require CODEFLASH_API_KEY (via FunctionOptimizer) — same as existing test file on main. Tests will only pass in CI where the key is configured.

No critical issues found in changed code.

Test Coverage

File Stmts Miss Coverage
codeflash/.../unused_definition_remover.py 497 28 94%
tests/test_mock_candidate_replacement.py 55 0 100%
  • ✅ All new lines (811-827) in unused_definition_remover.py are covered by tests
  • ✅ New test file has 100% coverage
  • ✅ No coverage regression — 28 missing lines are all pre-existing gaps
  • 8 unrelated test failures in test_tracer.py (pre-existing, not related to this PR)

Last updated: 2026-02-24T

…ection

detect_unused_helper_functions only walked ast.Call nodes, missing methods
referenced via attribute assignment (e.g., self._parse1 = self._parse_literal).
This caused optimized helper methods used as callbacks to be incorrectly
reverted to their original code.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@aseembits93 aseembits93 force-pushed the fix/unused-helper-attribute-refs branch from 989af0c to 14bdc3c Compare February 24, 2026 18:40
Replace substring assertions with exact equality check against the full
expected output (EXPECTED_OUTPUT constant). Extract shared setup into a
run_replacement helper.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@aseembits93 aseembits93 force-pushed the fix/unused-helper-attribute-refs branch from 4324455 to 5c829cd Compare February 24, 2026 18:47
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