Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.
26 changes: 20 additions & 6 deletions gapic/schema/imp.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,25 @@ def __eq__(self, other) -> bool:
return self.package == other.package and self.module == other.module

def __str__(self) -> str:
answer = f"import {self.module}"
# 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
Copy Markdown
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?

needs_type_ignore = self.module.endswith("_pb2") or "api_core" in self.package

if needs_type_ignore:
# Use 'import absolute.path as module' syntax to prevent Ruff/isort
# from combining this with other imports. This ensures the
# '# type: ignore' comment remains effective for this specific import.
full_module = ".".join(self.package + (self.module,))
alias = self.alias or self.module
return f"import {full_module} as {alias} # type: ignore"

# Standard import generation
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
Copy Markdown
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

Loading