-
Notifications
You must be signed in to change notification settings - Fork 4
perf: share parsed AST between verification checks #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,61 @@ | ||
| import ast | ||
| from dataclasses import dataclass | ||
| from typing import List, Optional | ||
|
|
||
| @dataclass(frozen=True) | ||
| class VerificationContext: | ||
| original_code: str | ||
| transformed_code: str | ||
| original_ast: Optional[ast.AST] | ||
| transformed_ast: Optional[ast.AST] | ||
|
|
||
| class BaseCheck: | ||
| def verify(self, original: str, transformed: str, context: Optional[VerificationContext] = None) -> bool: | ||
| """ | ||
| Verify the transformation. | ||
| Backward compatible Signature allows checks to drop the context argument if unsupported by custom checks. | ||
| """ | ||
| raise NotImplementedError | ||
|
Comment on lines
+12
to
+18
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Check if TestSuiteGate exists elsewhere in the codebase
rg -n "class TestSuiteGate" --type pyRepository: Refactron-ai/Refactron_lib Length of output: 147 🏁 Script executed: # Check what's in engine.py
cat -n refactron/verification/engine.py | head -50Repository: Refactron-ai/Refactron_lib Length of output: 2023 🏁 Script executed: # Check the TestSuiteGate implementation
cat -n refactron/verification/checks/test_gate.py | head -30Repository: Refactron-ai/Refactron_lib Length of output: 1207 🏁 Script executed: # Search for all three implementations
rg -n "class (SyntaxVerifier|ImportIntegrityVerifier|TestSuiteGate)" --type pyRepository: Refactron-ai/Refactron_lib Length of output: 469 All three
🤖 Prompt for AI Agents |
||
|
|
||
| class VerificationEngine: | ||
| def __init__(self, checks: List[BaseCheck]): | ||
| self.checks = checks | ||
|
|
||
| def verify(self, original: str, transformed: str) -> bool: | ||
| """ | ||
| Run all verification checks on the original and transformed code, | ||
| parsing the AST only once to improve performance. | ||
| """ | ||
| try: | ||
| orig_ast = ast.parse(original) | ||
| except Exception: | ||
| orig_ast = None | ||
|
|
||
| try: | ||
| trans_ast = ast.parse(transformed) | ||
| except Exception: | ||
| trans_ast = None | ||
|
|
||
| context = VerificationContext( | ||
| original_code=original, | ||
| transformed_code=transformed, | ||
| original_ast=orig_ast, | ||
| transformed_ast=trans_ast | ||
| ) | ||
|
|
||
| all_passed = True | ||
| for check in self.checks: | ||
| try: | ||
| # Attempt to pass context for optimized routines | ||
| passed = check.verify(original, transformed, context=context) | ||
| except TypeError: | ||
| # Fallback for older checks that don't accept context | ||
| passed = check.verify(original, transformed) | ||
|
|
||
| if not passed: | ||
| all_passed = False | ||
|
|
||
| return all_passed | ||
| """VerificationEngine — pipeline orchestrator for verification checks.""" | ||
|
|
||
| import math | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Silent exception swallowing hides parsing failures.
The
try-except-passblocks silently discard parsing errors. If both ASTs fail to parse (and no context was provided), the method skips to line 51 and returnsTrue, incorrectly indicating valid imports for unparseable code.Consider returning
Falsewhen both fallback parses fail, or at least logging the exception for debuggability.🛡️ Proposed fix to handle fallback parse failures
# Fallback for backward compatibility if orig_ast is None: try: orig_ast = ast.parse(original) - except Exception: - pass + except SyntaxError: + return False # Can't verify imports without valid AST if trans_ast is None: try: trans_ast = ast.parse(transformed) - except Exception: - pass + except SyntaxError: + return False # Can't verify imports without valid AST📝 Committable suggestion
🧰 Tools
🪛 Ruff (0.15.7)
[error] 34-35:
try-except-passdetected, consider logging the exception(S110)
[warning] 34-34: Do not catch blind exception:
Exception(BLE001)
[error] 40-41:
try-except-passdetected, consider logging the exception(S110)
[warning] 40-40: Do not catch blind exception:
Exception(BLE001)
🤖 Prompt for AI Agents