This repository was archived by the owner on Mar 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
chore: use import absolute.path as module syntax for pb2 imports #2553
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6f54178
chore: use import absolute.path as module syntax for pb2 imports
ohmayr a882890
update tests
ohmayr 6ec5213
minimize diff
ohmayr 4879d53
update test
ohmayr cb55bec
preserve default order
ohmayr 272f3e6
remove __lt__
ohmayr 5d00800
fix typo
ohmayr a0869e5
fix lint
ohmayr a55bf27
fix conditional statement
ohmayr ff35edd
reorder imports in test
ohmayr ba1430b
update goldens
ohmayr 74864b5
Merge branch 'main' into update-import-statements-for-pb
ohmayr 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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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. | ||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
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. 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
|
||||||||||||||||||||||||||||||||||||||||||||||
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.
Uh oh!
There was an error while loading. Please reload this page.
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?