Skip to content

Conversation

@haggit-eliyahu
Copy link
Contributor

@haggit-eliyahu haggit-eliyahu commented Jan 12, 2026


Description

handle statements of the format "import * as *".
some cases has to change it to "from * import * as *" due to using relative path.

Checklist:

Please ensure you have completed the following items before submitting your PR.
This helps us review your contribution faster and more efficiently.

General Checks:

  • I have read and followed the project's contributing.md guide.
  • My code follows the project's coding style guidelines.
  • I have performed a self-review of my own code.
  • My changes do not introduce any new warnings.
  • My changes pass all existing tests.
  • I have added new tests where appropriate to cover my changes. (If applicable)
  • I have updated the documentation where necessary (e.g., README, API docs). (If applicable)

Open-Source Specific Checks:

  • My changes do not introduce any Personally Identifiable Information (PII) or sensitive customer data.
  • My changes do not expose any internal-only code examples, configurations, or URLs.
  • All code examples, comments, and messages are generic and suitable for a public repository.
  • I understand that any internal context or sensitive details related to this work are handled separately in internal systems (Buganizer for Google team members).

For Google Team Members and Reviewers Only:

  • I have included the Buganizer ID in the PR title or description (e.g., "Internal Buganizer ID: 123456789" or "Related Buganizer: go/buganizer/123456789").
  • I have ensured that all internal discussions and PII related to this work remain in Buganizer.
  • I have tagged the PR with one or more labels that reflect the pull request purpose.

Screenshots (If Applicable)

If your changes involve UI or visual elements, please include screenshots or GIFs here.
Ensure any sensitive data is redacted or generalized.


Further Comments / Questions

Any additional comments, questions, or areas where you'd like specific feedback.

@haggit-eliyahu haggit-eliyahu requested a review from a team as a code owner January 12, 2026 09:32
@haggit-eliyahu haggit-eliyahu added Bug Something isn't working mp mp related change labels Jan 12, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @haggit-eliyahu, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an issue with how Python 'import ... as ...' statements are processed by the project's code manipulation tools. Previously, these statements were not correctly transformed, especially when converting standard 'import' syntax to 'from ... import ...' for internal and SDK modules. The changes ensure that the 'as' alias is preserved during these transformations, enhancing the robustness of the code refactoring utilities and preventing potential import errors.

Highlights

  • Import 'as' statement resolution: Correctly handles 'import as ' and 'import . as ' statements within the code manipulation logic, ensuring the 'as' alias is preserved during transformations.
  • SdkImportTransformer enhancement: The SdkImportTransformer has been updated to correctly prefix SDK module imports while preserving any 'as' aliases, preventing incorrect module resolution.
  • Core package import transformation: Improved CorePackageImportTransformer to convert 'import manager.submodule as alias' into 'from ..core.manager import submodule as alias' and similar patterns for internal core modules, ensuring proper relative path handling.
  • Expanded test coverage: New test cases have been added to validate the correct handling of various 'import ... as ...' scenarios across different import transformers, increasing confidence in the robustness of the changes.
  • Version bump: The project version has been updated from '1.24.1' to '1.24.2' in 'pyproject.toml' and 'uv.lock', reflecting the bug fix.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly addresses an issue where import ... as ... statements were not handled properly by the import transformers, causing aliases to be lost. The changes successfully preserve aliases and handle more complex import patterns. The accompanying tests are thorough and cover these new scenarios well.

However, I've identified a critical pre-existing issue in how import statements with multiple names (e.g., import os, sys) are processed. The current logic can lead to silently dropping imports, which could break code. I have provided two review comments with suggestions to address this critical bug, ensuring the transformers are more robust.

@haggit-eliyahu haggit-eliyahu changed the title Fix: resolve "import as" statements Fix: resolve "import as" statements in deconstruct flow Jan 12, 2026
@haggit-eliyahu haggit-eliyahu force-pushed the small-fix-in-imports-deconstruct branch from 688df18 to 4df4e4c Compare January 12, 2026 09:42
@haggit-eliyahu haggit-eliyahu force-pushed the small-fix-in-imports-deconstruct branch from 65efbb2 to 4349b37 Compare January 13, 2026 11:09
@TalShafir1
Copy link
Member

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request extends the import transformation logic to handle import ... as ... and multi-module import statements. While the added test cases cover many new scenarios, the implementation in both CorePackageImportTransformer and CorePackageInternalImportTransformer contains a critical flaw. The logic incorrectly assumes all modules on a single import line are of the same type and structure, based only on the first module. This can lead to generating invalid import statements when mixed imports are present. I've left detailed comments on the specific transformers with examples of the failing cases. These issues should be addressed, and the test suite should be expanded to include these negative test cases to ensure correctness.

Comment on lines 245 to 298
if not original_node.names:
return updated_node

first_module_part: str = full_module_name.split(".", maxsplit=1)[0]
first_alias = updated_node.names[0]
full_module_name: str | None = get_full_name_for_node(first_alias.name)
if not full_module_name or full_module_name.split(".")[0] not in self.core_module_names:
return updated_node

new_aliases: list[cst.ImportAlias] = []
parts: list[str] = full_module_name.split(".")

if len(parts) > 1:
# Case: `import manager.submodule.func, manager.submodule.other_func`
module_path: str = ".".join(parts[:-1])
prefixed_module = _create_prefixed_module(module_path, CORE_PREFIX)

for alias in updated_node.names:
alias_full_name = get_full_name_for_node(alias.name)
if alias_full_name:
imported_name = alias_full_name.split(".")[-1]
new_aliases.append(
cst.ImportAlias(
name=cst.Name(value=imported_name),
asname=alias.asname,
)
)

if first_module_part in self.core_module_names:
return cst.ImportFrom(
module=cst.Name(value="core"),
names=[cst.ImportAlias(name=cst.Name(value=first_module_part))],
module=prefixed_module,
names=new_aliases,
relative=(cst.Dot(), cst.Dot()),
)

return updated_node
# Case: `import manager, other_manager` -> `from ..core import manager, other_manager`
for alias in updated_node.names:
alias_full_name = get_full_name_for_node(alias.name)
if alias_full_name:
new_aliases.append(
cst.ImportAlias(name=cst.Name(value=alias_full_name), asname=alias.asname)
)

return cst.ImportFrom(
module=cst.Name(value="core"),
names=new_aliases,
relative=(cst.Dot(), cst.Dot()),
)
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

This new logic to handle multiple imports on one line has a flaw when dealing with mixed import types. The decision to transform the entire import statement is based only on the first imported module (first_alias). This can lead to incorrect code generation.

Here are a couple of scenarios where it fails:

  1. Mixing core and non-core modules:
    An import like import manager, requests (assuming manager is a core module and requests is not) will be incorrectly transformed into from ..core import manager, requests, which will cause an ImportError because requests is not in ..core.

  2. Mixing top-level and sub-module imports:
    An import like import manager.submodule, manager2 will be incorrectly transformed into from ..core.manager import submodule, manager2, which is wrong because manager2 is not part of the ..core.manager module.

The transformation should only proceed if all modules on the line are of a compatible type and structure. You should either validate all modules before transforming or choose not to transform ambiguous multi-import statements.

Please also add test cases for these failing scenarios to prevent regressions.

Comment on lines 333 to 404
if not original_node.names:
return updated_node

full_module_name: str | None = get_full_name_for_node(original_node.names[0].name)
# Check first alias to see if we need to transform
first_alias = updated_node.names[0]
full_module_name: str | None = get_full_name_for_node(first_alias.name)
if not full_module_name:
return updated_node

if (
full_module_name in self.core_module_names
and full_module_name != self.current_module_name
parts: list[str] = full_module_name.split(".")
first_module_part: str = parts[0]

if not (
first_module_part in self.core_module_names
and first_module_part != self.current_module_name
):
return updated_node

new_aliases: list[cst.ImportAlias] = []

if len(parts) > 1:
# Case: `import constants.func, constants.other_func`
# -> `from .constants import func, other_func`
module_path: str = ".".join(parts[:-1])
module_expr = cst.parse_expression(module_path)

for alias in updated_node.names:
alias_full_name = get_full_name_for_node(alias.name)
if alias_full_name:
imported_name = alias_full_name.split(".")[-1]
new_aliases.append(
cst.ImportAlias(
name=cst.Name(value=imported_name),
asname=alias.asname,
)
)

return cst.ImportFrom(
module=None,
names=[cst.ImportAlias(name=cst.Name(value=full_module_name))],
module=cast("cst.Attribute | cst.Name", module_expr),
names=new_aliases,
relative=(cst.Dot(),),
)

return updated_node
# Case: `import constants, utils` -> `from . import constants, utils`
for alias in updated_node.names:
alias_full_name = get_full_name_for_node(alias.name)
if alias_full_name:
new_aliases.append(
cst.ImportAlias(name=cst.Name(value=alias_full_name), asname=alias.asname)
)

return cst.ImportFrom(
module=None,
names=new_aliases,
relative=(cst.Dot(),),
)
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

Similar to the CorePackageImportTransformer, this transformer's logic for handling multiple imports is flawed because it only inspects the first module on the line to decide how to transform the entire statement.

This can cause incorrect transformations in these cases:

  1. Mixing internal core modules with other modules:
    import constants, some_external_lib would be incorrectly changed to from . import constants, some_external_lib, leading to an ImportError.

  2. Mixing different internal core modules with different structures:
    import constants.func, utils.helper would be incorrectly transformed to from .constants import func, helper, which is invalid because helper is not in constants.

To fix this, the logic needs to validate all imported items on the line to ensure they are compatible before applying the transformation. It would also be beneficial to add negative test cases to cover these scenarios.

# Conflicts:
#	packages/mp/pyproject.toml
#	packages/mp/uv.lock
@haggit-eliyahu haggit-eliyahu force-pushed the small-fix-in-imports-deconstruct branch from 4349b37 to 9682092 Compare January 13, 2026 17:29
@haggit-eliyahu haggit-eliyahu force-pushed the small-fix-in-imports-deconstruct branch from 9682092 to 5e2a9d6 Compare January 14, 2026 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working mp mp related change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants