Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: consistently use _pb2 identifier #883

Merged
merged 5 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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