Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit d789c84

Browse files
authored
fix: consistently use _pb2 identifier (#883)
Small change to fix bug noticed in googleapis/python-dialogflow#288. The import was `from google.rpc import status_pb2` but code expected `gr_status`
1 parent 7087af5 commit d789c84

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

gapic/schema/metadata.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,17 @@ def __str__(self) -> str:
8484
if self.module:
8585
module_name = self.module
8686

87+
# If collisions are registered and conflict with our module,
88+
# use the module alias instead.
89+
if self.module_alias:
90+
module_name = self.module_alias
91+
8792
# This module is from a different proto package
8893
# Most commonly happens for a common proto
8994
# https://pypi.org/project/googleapis-common-protos/
9095
if not self.proto_package.startswith(self.api_naming.proto_package):
9196
module_name = f'{self.module}_pb2'
9297

93-
# If collisions are registered and conflict with our module,
94-
# use the module alias instead.
95-
if self.module_alias:
96-
module_name = self.module_alias
97-
9898
# Return the dot-separated Python identifier.
9999
return '.'.join((module_name,) + self.parent + (self.name,))
100100

tests/unit/schema/test_metadata.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ def test_address_str_different_proto_package():
4949
assert str(addr) == 'options_pb2.GetPolicyOptions'
5050

5151

52+
def test_address_str_different_proto_package_with_collision():
53+
addr = metadata.Address(
54+
package=('google', 'rpc'),
55+
module='status',
56+
name='Status',
57+
api_naming=naming.NewNaming(proto_package='foo.bar.baz.v1')
58+
).with_context(collisions=frozenset({'status'}))
59+
# the module alias should be ignored for _pb2 types
60+
assert str(addr) == 'status_pb2.Status'
61+
62+
5263
def test_address_proto():
5364
addr = metadata.Address(package=('foo', 'bar'), module='baz', name='Bacon')
5465
assert addr.proto == 'foo.bar.Bacon'

0 commit comments

Comments
 (0)