-
Notifications
You must be signed in to change notification settings - Fork 52
CG-10899: file.add_import
#673
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3240904
.
ccf7a26
Merge branch 'develop' into tawsif/codebase-from-string-from-files
d377327
done
772ac1f
Merge branch 'develop' into tawsif/fix-imports
c0544a1
done
tawsifkamal c7dfc2a
done
tawsifkamal 7e26d80
stuff
tawsifkamal fb5549a
Merge branch 'develop' into tawsif/add-import
tawsifkamal 56f91d6
Merge branch 'develop' into tawsif/add-import
tawsifkamal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -944,62 +944,56 @@ def update_filepath(self, new_filepath: str) -> None: | |
imp.set_import_module(new_module_name) | ||
|
||
@writer | ||
def add_symbol_import( | ||
self, | ||
symbol: Symbol, | ||
alias: str | None = None, | ||
import_type: ImportType = ImportType.UNKNOWN, | ||
is_type_import: bool = False, | ||
) -> Import | None: | ||
"""Adds an import to a file for a given symbol. | ||
|
||
This method adds an import statement to the file for a specified symbol. If an import for the | ||
symbol already exists, it returns the existing import instead of creating a new one. | ||
def add_import(self, imp: Symbol | str, *, alias: str | None = None, import_type: ImportType = ImportType.UNKNOWN, is_type_import: bool = False) -> Import | None: | ||
"""Adds an import to the file. | ||
|
||
Args: | ||
symbol (Symbol): The symbol to import. | ||
alias (str | None): Optional alias for the imported symbol. Defaults to None. | ||
import_type (ImportType): The type of import to use. Defaults to ImportType.UNKNOWN. | ||
is_type_import (bool): Whether this is a type-only import. Defaults to False. | ||
|
||
Returns: | ||
Import | None: The existing import for the symbol or None if it was added. | ||
""" | ||
imports = self.imports | ||
match = next((x for x in imports if x.imported_symbol == symbol), None) | ||
if match: | ||
return match | ||
|
||
import_string = symbol.get_import_string(alias, import_type=import_type, is_type_import=is_type_import) | ||
self.add_import_from_import_string(import_string) | ||
|
||
@writer(commit=False) | ||
def add_import_from_import_string(self, import_string: str) -> None: | ||
"""Adds import to the file from a string representation of an import statement. | ||
|
||
This method adds a new import statement to the file based on its string representation. | ||
This method adds an import statement to the file. It can handle both string imports and symbol imports. | ||
If the import already exists in the file, or is pending to be added, it won't be added again. | ||
If there are existing imports, the new import will be added before the first import, | ||
otherwise it will be added at the beginning of the file. | ||
|
||
Args: | ||
import_string (str): The string representation of the import statement to add. | ||
imp (Symbol | str): Either a Symbol to import or a string representation of an import statement. | ||
alias (str | None): Optional alias for the imported symbol. Only used when imp is a Symbol. Defaults to None. | ||
import_type (ImportType): The type of import to use. Only used when imp is a Symbol. Defaults to ImportType.UNKNOWN. | ||
is_type_import (bool): Whether this is a type-only import. Only used when imp is a Symbol. Defaults to False. | ||
|
||
Returns: | ||
None | ||
Import | None: The existing import for the symbol if found, otherwise None. | ||
""" | ||
if any(import_string.strip() in imp.source for imp in self.imports): | ||
return | ||
# Handle Symbol imports | ||
if isinstance(imp, str): | ||
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. Can we change the behavior to
|
||
# Handle string imports | ||
import_string = imp | ||
# Check for duplicate imports | ||
if any(import_string.strip() in imp.source for imp in self.imports): | ||
return None | ||
else: | ||
# Check for existing imports of this symbol | ||
imports = self.imports | ||
match = next((x for x in imports if x.imported_symbol == imp), None) | ||
if match: | ||
return match | ||
|
||
# Convert symbol to import string | ||
import_string = imp.get_import_string(alias, import_type=import_type, is_type_import=is_type_import) | ||
|
||
if import_string.strip() in self._pending_imports: | ||
# Don't add the import string if it will already be added by another symbol | ||
return | ||
return None | ||
|
||
# Add to pending imports and setup undo | ||
self._pending_imports.add(import_string.strip()) | ||
self.transaction_manager.pending_undos.add(lambda: self._pending_imports.clear()) | ||
|
||
# Insert the import at the appropriate location | ||
if self.imports: | ||
self.imports[0].insert_before(import_string, priority=1) | ||
else: | ||
self.insert_before(import_string, priority=1) | ||
|
||
return None | ||
|
||
@writer | ||
def add_symbol_from_source(self, source: str) -> None: | ||
"""Adds a symbol to a file from a string representation. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we add typing overloads to make sure the user doesn't pass the other parameters if imp is of type str