Skip to content

Commit

Permalink
fix: consistently use _pb2 identifier (#883)
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
busunkim96 authored May 12, 2021
1 parent 7087af5 commit d789c84
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 5 additions & 5 deletions gapic/schema/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ def __str__(self) -> str:
if self.module:
module_name = self.module

# If collisions are registered and conflict with our module,
# use the module alias instead.
if self.module_alias:
module_name = self.module_alias

# This module is from a different proto package
# Most commonly happens for a common proto
# https://pypi.org/project/googleapis-common-protos/
if not self.proto_package.startswith(self.api_naming.proto_package):
module_name = f'{self.module}_pb2'

# If collisions are registered and conflict with our module,
# use the module alias instead.
if self.module_alias:
module_name = self.module_alias

# Return the dot-separated Python identifier.
return '.'.join((module_name,) + self.parent + (self.name,))

Expand Down
11 changes: 11 additions & 0 deletions tests/unit/schema/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ def test_address_str_different_proto_package():
assert str(addr) == 'options_pb2.GetPolicyOptions'


def test_address_str_different_proto_package_with_collision():
addr = metadata.Address(
package=('google', 'rpc'),
module='status',
name='Status',
api_naming=naming.NewNaming(proto_package='foo.bar.baz.v1')
).with_context(collisions=frozenset({'status'}))
# the module alias should be ignored for _pb2 types
assert str(addr) == 'status_pb2.Status'


def test_address_proto():
addr = metadata.Address(package=('foo', 'bar'), module='baz', name='Bacon')
assert addr.proto == 'foo.bar.Bacon'
Expand Down

0 comments on commit d789c84

Please sign in to comment.