Skip to content

Conversation

@ohmayr
Copy link
Contributor

@ohmayr ohmayr commented Jan 30, 2026

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.

@product-auto-label product-auto-label bot added the size: s Pull request size is small. label Jan 30, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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 gapic library. It introduces a specific import syntax for protobuf-generated files and api_core modules, which are often accompanied by # type: ignore comments. This modification ensures these special imports are correctly formatted and not inadvertently altered by code formatters, thereby unblocking the planned migration to Ruff.

Highlights

  • Import Statement Generation Logic: The logic for generating import statements in gapic/schema/imp.py has been refactored to introduce conditional formatting based on the module type.
  • Preservation of Type Ignore Comments: For _pb2 files and api_core related imports that require # type: ignore comments, a new import absolute.path as module syntax is now used. This prevents linters like Ruff/isort from combining these imports and inadvertently removing or misplacing the type ignore directive.
  • Ruff Migration Support: This change is a preparatory step designed to unblock and facilitate the migration from black/isort to Ruff, ensuring compatibility with Ruff's import sorting and formatting behavior.
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 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.

Comment on lines 43 to 50
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
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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

@ohmayr ohmayr requested a review from daniel-sanche January 30, 2026 23:23
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.
Copy link
Contributor

@daniel-sanche daniel-sanche Jan 30, 2026

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?

@product-auto-label product-auto-label bot added size: m Pull request size is medium. and removed size: s Pull request size is small. labels Jan 30, 2026
Copy link
Contributor

@daniel-sanche daniel-sanche left a comment

Choose a reason for hiding this comment

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

LGTM

@ohmayr ohmayr marked this pull request as ready for review January 31, 2026 00:02
@ohmayr ohmayr requested a review from a team as a code owner January 31, 2026 00:02
@ohmayr ohmayr enabled auto-merge (squash) January 31, 2026 00:03
@ohmayr ohmayr merged commit bb6d959 into main Jan 31, 2026
139 of 140 checks passed
@ohmayr ohmayr deleted the update-import-statements-for-pb branch January 31, 2026 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants