-
Notifications
You must be signed in to change notification settings - Fork 78
chore: use import absolute.path as module syntax for pb2 imports #2553
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
Summary of ChangesHello @ohmayr, 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 refines the import statement generation mechanism within the Highlights
Using Gemini Code AssistThe 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
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 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
|
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.
Code Review
This pull request correctly updates the import generation for _pb2 and api_core modules to use an absolute import syntax (import a.b.c as c), which is a good change to ensure # type: ignore comments work reliably with tools like Ruff. However, a critical issue is that the unit tests in tests/unit/schema/test_imp.py have not been updated to reflect these changes, which will cause them to fail. Please update the tests to validate the new behavior. I've also included a suggestion to improve the readability of the standard import generation logic.
gapic/schema/imp.py
Outdated
| import_clause = f"import {self.module}" | ||
| if self.package: | ||
| answer = f"from {'.'.join(self.package)} {answer}" | ||
| import_clause = f"from {'.'.join(self.package)} {import_clause}" | ||
|
|
||
| if self.alias: | ||
| answer += f" as {self.alias}" | ||
| if self.module.endswith("_pb2") or "api_core" in self.package: | ||
| answer += " # type: ignore" | ||
| return answer | ||
| import_clause += f" as {self.alias}" | ||
|
|
||
| return import_clause |
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.
The logic for constructing the standard import string can be made more direct and readable. Instead of building the string incrementally with variable reassignment, you can use a conditional to create the base import string and then append the alias if it exists. This makes the intent clearer.
| import_clause = f"import {self.module}" | |
| if self.package: | |
| answer = f"from {'.'.join(self.package)} {answer}" | |
| import_clause = f"from {'.'.join(self.package)} {import_clause}" | |
| if self.alias: | |
| answer += f" as {self.alias}" | |
| if self.module.endswith("_pb2") or "api_core" in self.package: | |
| answer += " # type: ignore" | |
| return answer | |
| import_clause += f" as {self.alias}" | |
| return import_clause | |
| if self.package: | |
| import_clause = f"from {'.'.join(self.package)} import {self.module}" | |
| else: | |
| import_clause = f"import {self.module}" | |
| if self.alias: | |
| import_clause += f" as {self.alias}" | |
| return import_clause |
| def __str__(self) -> str: | ||
| # Determine if we need to suppress type checking for this import. | ||
| # We do this for protobuf generated files (_pb2) and api_core | ||
| # internals where type information might be missing or incomplete. |
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.
Do we really need to do this in the generated code? Can't we configure it in mypy.ini?
Edit: Looking closer, it looks like this logic was already in place, you're just moving it around. Do you know if mypy.ini is an option though?
daniel-sanche
left a comment
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.
LGTM
This PR updates how we generate the import path for _pb2 files and api-core. This work is meant to unblock the migration from black/isort to ruff.