-
Couldn't load subscription status.
- Fork 22
[FIX] Prevent cst duplicate imports #665
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
Conversation
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
| new_module = add_needed_imports_from_module(src_module, dst_module, src_path, dst_path, project_root) | ||
|
|
||
| matches = re.findall(r"^\s*from\s+recce\.adapter\.base\s+import\s+BaseAdapter\s*$", new_module, re.MULTILINE) | ||
| assert len(matches) == 1, f"Expected 1 match for BaseAdapter import, but found {len(matches)}" |
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.
i would prefer a direct string comparison over the entirety of the newly replaced string. this way we have more of a guarantee than searching for particular patterns.
This fixes the issue here: misrasaurabh1/recce@16f0638
Basically,
from recce.adapter.base import BaseAdaptergot added even though it was already in the file.my guess is that
AddImportsVisitor.add_needed_importstops looking for imports once it hits something that’s not an import. In this case, it hit thetry/exceptblock within lines 39:46, so it never saw the existingBaseAdapterimport at line 50 and just added it again.The fix was to manually collect all top-level imports in a normalized dotted format, so duplicates don’t slip through anymore.
Description
Replace conditional import collector with dotted importer
Prevent adding duplicate imports in extraction logic
Update import normalization and alias handling
Add test for duplicate import removal
Diagram Walkthrough
File Walkthrough
code_extractor.py
Refactor import collector and dedupe logiccodeflash/code_utils/code_extractor.py
test_add_needed_imports_from_module.py
Add test for duplicate import removaltests/test_add_needed_imports_from_module.py
refor regex assertionstest_duplicated_importsfor dedupe check