From 5f5f581750b93eaaa863c050c2fb59664d227bbc Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Tue, 14 Feb 2023 20:21:28 +0000 Subject: [PATCH 01/22] feat: add generator option proto-plus-dep --- .../services/%service/transports/rest.py.j2 | 4 +- .../%name_%version/%sub/test_%service.py.j2 | 8 ++-- gapic/schema/api.py | 5 +++ gapic/schema/metadata.py | 38 +++++++++++++--- gapic/schema/naming.py | 6 +++ gapic/schema/wrappers.py | 2 +- .../services/%service/transports/rest.py.j2 | 4 +- gapic/templates/setup.py.j2 | 7 ++- .../gapic/%name_%version/%sub/test_macros.j2 | 12 +++--- gapic/utils/options.py | 9 ++++ tests/unit/generator/test_options.py | 27 ++++++++++++ tests/unit/schema/test_api.py | 43 ++++++++++++++++++- tests/unit/schema/test_naming.py | 11 +++++ 13 files changed, 152 insertions(+), 24 deletions(-) diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/rest.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/rest.py.j2 index bf8946c529..68d601fe22 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/rest.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/rest.py.j2 @@ -322,7 +322,7 @@ class {{service.name}}RestTransport({{service.name}}Transport): {% endfor %}{# rule in method.http_options #} ] request, metadata = self._interceptor.pre_{{ method.name|snake_case }}(request, metadata) - {% if method.input.ident.is_external_type %} + {% if not method.input.ident.is_proto_plus_type %} pb_request = request {% else %} pb_request = {{method.input.ident}}.pb(request) @@ -384,7 +384,7 @@ class {{service.name}}RestTransport({{service.name}}Transport): resp = rest_streaming.ResponseIterator(response, {{method.output.ident}}) {% else %} resp = {{method.output.ident}}() - {% if method.output.ident.is_external_type %} + {% if not method.output.ident.is_proto_plus_type %} pb_resp = resp {% else %} pb_resp = {{method.output.ident}}.pb(resp) diff --git a/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index 254fa5e801..e66980dc5f 100644 --- a/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -1118,7 +1118,7 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide {% endif %}{# default is str #} {% endfor %} request = request_type(**request_init) - {% if method.input.ident.is_external_type %} + {% if not method.input.ident.is_proto_plus_type %} pb_request = request {% else %} pb_request = request_type.pb(request) @@ -1189,7 +1189,7 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide with mock.patch.object(path_template, 'transcode') as transcode: # A uri without fields and an empty body will force all the # request fields to show up in the query_params. - {% if method.input.ident.is_external_type %} + {% if not method.input.ident.is_proto_plus_type %} pb_request = request {% else %} pb_request = request_type.pb(request) @@ -1212,7 +1212,7 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide json_return_value = json_format.MessageToJson(return_value) {% else %} - {% if method.output.ident.is_external_type %} + {% if not method.output.ident.is_proto_plus_type %} pb_return_value = return_value {% else %} pb_return_value = {{ method.output.ident }}.pb(return_value) @@ -1287,7 +1287,7 @@ def test_{{ method_name }}_rest_interceptors(null_interceptor): {% if not method.void %} post.assert_not_called() {% endif %} - {% if method.input.ident.is_external_type %} + {% if not method.input.ident.is_proto_plus_type %} pb_message = {{ method.input.ident }}() {% else %} pb_message = {{ method.input.ident }}.pb({{ method.input.ident }}()) diff --git a/gapic/schema/api.py b/gapic/schema/api.py index b9af9e348a..ab8507fc18 100644 --- a/gapic/schema/api.py +++ b/gapic/schema/api.py @@ -485,6 +485,11 @@ def gapic_metadata_json(self, options: Options) -> str: return MessageToJson(self.gapic_metadata(options), sort_keys=True) def requires_package(self, pkg: Tuple[str, ...]) -> bool: + # Quick check to make sure the package checked is different + # from this proto package + if self.naming.proto_package.startswith(pkg): + return False + return any( message.ident.package == pkg for proto in self.all_protos.values() diff --git a/gapic/schema/metadata.py b/gapic/schema/metadata.py index 8dc4d5dc67..29c2b1c939 100644 --- a/gapic/schema/metadata.py +++ b/gapic/schema/metadata.py @@ -27,6 +27,7 @@ """ import dataclasses +import re from typing import FrozenSet, Tuple, Optional from google.protobuf import descriptor_pb2 @@ -89,10 +90,8 @@ def __str__(self) -> str: 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 self.is_external_type: + # Add _pb2 suffix except when it is a proto-plus type + if not self.is_proto_plus_type: module_name = f'{self.module}_pb2' # Return the dot-separated Python identifier. @@ -103,8 +102,13 @@ def __str__(self) -> str: return '.'.join(self.parent + (self.name,)) @property - def is_external_type(self): - return not self.proto_package.startswith(self.api_naming.proto_package) + def is_proto_plus_type(self): + if self.proto_package.startswith(self.api_naming.proto_package) or ( + hasattr(self.api_naming, "proto_plus_deps") + and self.proto_package in self.api_naming.proto_plus_deps + ): + return True + return False @cached_property def __cached_string_repr(self): @@ -188,6 +192,28 @@ def python_import(self) -> imp.Import: alias=self.module_alias, ) + if self.is_proto_plus_type: + # We need to change the import statement to use an + # underscore between the module and the version. For example, + # change google.cloud.documentai.v1 to google.cloud.documentai_v1. + # Check if the package name contains a version. + version_regex = "^v\d(alpha|beta)?\d?" + regex_match = re.match(version_regex, self.package[-1]) + + if regex_match: + versioned_module = f"{self.package[-2]}_{regex_match[0]}" + return imp.Import( + package=self.package[:-2] + + (versioned_module, 'types'), + module=self.module, + alias=self.module_alias, + ) + else: + return imp.Import( + package=self.package + ('types',), + module=self.module, + ) + # Return the standard import. return imp.Import( package=self.package, diff --git a/gapic/schema/naming.py b/gapic/schema/naming.py index fa6000f88a..1d055490ff 100644 --- a/gapic/schema/naming.py +++ b/gapic/schema/naming.py @@ -43,6 +43,7 @@ class Naming(abc.ABC): product_name: str = '' proto_package: str = '' _warehouse_package_name: str = '' + proto_plus_deps: Tuple[str, ...] = dataclasses.field(default_factory=tuple) def __post_init__(self): if not self.product_name: @@ -146,6 +147,11 @@ def build( package_info = dataclasses.replace(package_info, _warehouse_package_name=opts.warehouse_package_name ) + if opts.proto_plus_deps: + package_info = dataclasses.replace( + package_info, + proto_plus_deps=opts.proto_plus_deps, + ) # Done; return the naming information. return package_info diff --git a/gapic/schema/wrappers.py b/gapic/schema/wrappers.py index b32af50b94..50127732aa 100644 --- a/gapic/schema/wrappers.py +++ b/gapic/schema/wrappers.py @@ -77,7 +77,7 @@ def __hash__(self): def name(self) -> str: """Used to prevent collisions with python keywords""" name = self.field_pb.name - return name + "_" if name in utils.RESERVED_NAMES and not self.meta.address.is_external_type else name + return name + "_" if name in utils.RESERVED_NAMES and self.meta.address.is_proto_plus_type else name @utils.cached_property def ident(self) -> metadata.FieldIdentifier: diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 index 8d3661c0fc..d8ef104417 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 @@ -352,7 +352,7 @@ class {{service.name}}RestTransport({{service.name}}Transport): {% endfor %}{# rule in method.http_options #} ] request, metadata = self._interceptor.pre_{{ method.name|snake_case }}(request, metadata) - {% if method.input.ident.is_external_type %} + {% if not method.input.ident.is_proto_plus_type %} pb_request = request {% else %} pb_request = {{method.input.ident}}.pb(request) @@ -414,7 +414,7 @@ class {{service.name}}RestTransport({{service.name}}Transport): resp = rest_streaming.ResponseIterator(response, {{method.output.ident}}) {% else %} resp = {{method.output.ident}}() - {% if method.output.ident.is_external_type %} + {% if not method.output.ident.is_proto_plus_type %} pb_resp = resp {% else %} pb_resp = {{method.output.ident}}.pb(resp) diff --git a/gapic/templates/setup.py.j2 b/gapic/templates/setup.py.j2 index 4e39c141b4..9c750f55dd 100644 --- a/gapic/templates/setup.py.j2 +++ b/gapic/templates/setup.py.j2 @@ -33,10 +33,13 @@ dependencies = [ "protobuf>=3.19.5,<5.0.0dev,!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5", {# TODO: Remove after https://github.com/googleapis/gapic-generator-python/pull/1240 is merged. #} {% if api.requires_package(('google', 'iam', 'v1')) or opts.add_iam_methods or api.has_iam_mixin %} - 'grpc-google-iam-v1 >= 0.12.4, < 1.0.0dev', + "grpc-google-iam-v1 >= 0.12.4, < 1.0.0dev", {% endif %} {% if api.requires_package(('google', 'cloud', 'documentai', 'v1')) %} - 'google-cloud-documentai >= 1.2.1, < 3.0.0dev', + "google-cloud-documentai >= 1.2.1, < 3.0.0dev", + {% endif %} + {% if api.requires_package(('google', 'apps', 'script', 'type')) %} + "google-apps-script-type>=0.2.0, <1.0.0dev", {% endif %} ] url = "https://github.com/googleapis/python-{{ api.naming.warehouse_package_name|replace("google-cloud-", "")|replace("google-", "") }}" diff --git a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2 b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2 index eeda2a3863..98c654e425 100644 --- a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2 +++ b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2 @@ -929,7 +929,7 @@ def test_{{ method_name }}_rest(request_type): {% elif method.lro %} json_return_value = json_format.MessageToJson(return_value) {% else %} - {% if method.output.ident.is_external_type %} + {% if not method.output.ident.is_proto_plus_type %} pb_return_value = return_value {% else %} pb_return_value = {{ method.output.ident }}.pb(return_value) @@ -1009,7 +1009,7 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide {% endif %}{# default is str #} {% endfor %} request = request_type(**request_init) - {% if method.input.ident.is_external_type %} + {% if not method.input.ident.is_proto_plus_type %} pb_request = request {% else %} pb_request = request_type.pb(request) @@ -1080,7 +1080,7 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide with mock.patch.object(path_template, 'transcode') as transcode: # A uri without fields and an empty body will force all the # request fields to show up in the query_params. - {% if method.input.ident.is_external_type %} + {% if not method.input.ident.is_proto_plus_type %} pb_request = request {% else %} pb_request = request_type.pb(request) @@ -1103,7 +1103,7 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide json_return_value = json_format.MessageToJson(return_value) {% else %} - {% if method.output.ident.is_external_type %} + {% if not method.output.ident.is_proto_plus_type %} pb_return_value = return_value {% else %} pb_return_value = {{ method.output.ident }}.pb(return_value) @@ -1178,7 +1178,7 @@ def test_{{ method_name }}_rest_interceptors(null_interceptor): {% if not method.void %} post.assert_not_called() {% endif %} - {% if method.input.ident.is_external_type %} + {% if not method.input.ident.is_proto_plus_type %} pb_message = {{ method.input.ident }}() {% else %} pb_message = {{ method.input.ident }}.pb({{ method.input.ident }}()) @@ -1295,7 +1295,7 @@ def test_{{ method_name }}_rest_flattened(): {% elif method.lro %} json_return_value = json_format.MessageToJson(return_value) {% else %} - {% if method.output.ident.is_external_type %} + {% if not method.output.ident.is_proto_plus_type %} pb_return_value = return_value {% else %} pb_return_value = {{ method.output.ident }}.pb(return_value) diff --git a/gapic/utils/options.py b/gapic/utils/options.py index 1785e93a55..fa302ed1f5 100644 --- a/gapic/utils/options.py +++ b/gapic/utils/options.py @@ -49,6 +49,7 @@ class Options: service_yaml_config: Dict[str, Any] = dataclasses.field( default_factory=dict) rest_numeric_enums: bool = False + proto_plus_deps: Tuple[str, ...] = dataclasses.field(default=()) # Class constants PYTHON_GAPIC_PREFIX: str = 'python-gapic-' @@ -66,6 +67,9 @@ class Options: 'warehouse-package-name', # change the package name on PyPI # when transport includes "rest", request that response enums be JSON-encoded as numbers 'rest-numeric-enums', + # proto plus dependencies delineated by '+' + # For example, 'google.cloud.api.v1+google.cloud.anotherapi.v2' + 'proto-plus-deps', )) @classmethod @@ -161,6 +165,10 @@ def tweak_path(p): if old_naming: autogen_snippets = False + proto_plus_deps = opts.pop('proto-plus-deps', []) + if len(proto_plus_deps): + proto_plus_deps = tuple(proto_plus_deps[0].split('+')) + answer = Options( name=opts.pop('name', ['']).pop(), namespace=tuple(opts.pop('namespace', [])), @@ -182,6 +190,7 @@ def tweak_path(p): transport=opts.pop('transport', ['grpc'])[0].split('+'), service_yaml_config=service_yaml_config, rest_numeric_enums=bool(opts.pop('rest-numeric-enums', False)), + proto_plus_deps=proto_plus_deps, ) # Note: if we ever need to recursively check directories for sample diff --git a/tests/unit/generator/test_options.py b/tests/unit/generator/test_options.py index 91da98de51..215f160a63 100644 --- a/tests/unit/generator/test_options.py +++ b/tests/unit/generator/test_options.py @@ -223,3 +223,30 @@ def test_options_autogen_snippets_false_for_old_naming(): # Even if autogen-snippets is set to True, do not enable snippetgen options = Options.build("old-naming,autogen-snippets=True") assert not options.autogen_snippets + +def test_options_proto_plus_deps(): + opts = Options.build("proto-plus-deps=") + assert opts.proto_plus_deps == ('',) + + opts = Options.build("proto-plus-deps=google.apps.script.type.calendar") + assert opts.proto_plus_deps == ('google.apps.script.type.calendar',) + + opts = Options.build( + "proto-plus-deps=\ +google.apps.script.type.calendar+\ +google.apps.script.type.docs+\ +google.apps.script.type.drive+\ +google.apps.script.type.gmail+\ +google.apps.script.type.sheets+\ +google.apps.script.type.slides+\ +google.apps.script.type" + ) + assert opts.proto_plus_deps == ( + "google.apps.script.type.calendar", + "google.apps.script.type.docs", + "google.apps.script.type.drive", + "google.apps.script.type.gmail", + "google.apps.script.type.sheets", + "google.apps.script.type.slides", + "google.apps.script.type" + ) diff --git a/tests/unit/schema/test_api.py b/tests/unit/schema/test_api.py index 607d4858bb..ce023141fc 100644 --- a/tests/unit/schema/test_api.py +++ b/tests/unit/schema/test_api.py @@ -108,7 +108,9 @@ def test_api_build(): imp.Import(package=('google', 'dep'), module='dep_pb2'), ) - assert api_schema.requires_package(('google', 'example', 'v1')) + # The package should not require itself + assert not api_schema.requires_package(('google', 'example', 'v1')) + assert not api_schema.requires_package(('elgoog', 'example', 'v1')) # Establish that the subpackages work. @@ -722,6 +724,16 @@ def test_python_modules_nested(): package='google.dep', messages=(make_message_pb2(name='ImportedMessage', fields=()),), ), + make_file_pb2( + name='bar.proto', + package='google.bar', + messages=(make_message_pb2(name='AnotherImportedMessage', fields=()),), + ), + make_file_pb2( + name='baz.v2.proto', + package='google.baz.v2', + messages=(make_message_pb2(name='YetAnotherImportedMessage', fields=()),), + ), make_file_pb2( name='common.proto', package='google.example.v1.common', @@ -752,6 +764,24 @@ def test_python_modules_nested(): type_name='.google.dep.ImportedMessage'), ), ), + make_message_pb2( + name='Bar', + fields=( + make_field_pb2( + name='another_imported_message', + number=1, + type_name='.google.bar.AnotherImportedMessage'), + ), + ), + make_message_pb2( + name='Baz', + fields=( + make_field_pb2( + name='yet_another_imported_message', + number=1, + type_name='.google.baz.v2.YetAnotherImportedMessage'), + ), + ), ), ), make_message_pb2( @@ -781,6 +811,17 @@ def test_python_modules_nested(): api_schema = api.API.build(fd, package='google.example.v1') assert api_schema.protos['foo.proto'].python_modules == ( + imp.Import(package=('google', 'bar'), module='bar_pb2'), + imp.Import(package=('google', 'baz', 'v2'), module='baz_v2_pb2'), + imp.Import(package=('google', 'dep'), module='dep_pb2'), + ) + # Ensure that we can change the import statements in case the dependency + # uses proto-plus types + api_schema = api.API.build(fd, package='google.example.v1', opts=Options(proto_plus_deps=('google.bar+google.baz.v2'))) + + assert api_schema.protos['foo.proto'].python_modules == ( + imp.Import(package=('google', 'bar', 'types'), module='bar'), + imp.Import(package=('google', 'baz_v2', 'types'), module='baz_v2'), imp.Import(package=('google', 'dep'), module='dep_pb2'), ) diff --git a/tests/unit/schema/test_naming.py b/tests/unit/schema/test_naming.py index c0487b7d26..5a839e4c8c 100644 --- a/tests/unit/schema/test_naming.py +++ b/tests/unit/schema/test_naming.py @@ -228,6 +228,17 @@ def test_cli_override_warehouse_package_name(): assert n.warehouse_package_name == "google-cloud-foo" +def test_cli_override_proto_plus_deps(): + FileDesc = descriptor_pb2.FileDescriptorProto + proto1 = FileDesc(package='google.translation') + n = naming.Naming.build( + proto1, + opts=Options( + proto_plus_deps=('google.dep1', 'google.dep2')), + ) + assert n.proto_plus_deps == ('google.dep1', 'google.dep2') + + def test_build_factory(): proto = descriptor_pb2.FileDescriptorProto( package='google.mollusc.v1alpha1' From 855e664d2131ab8e3cd6e7fb574ab3be95de46ea Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Tue, 28 Feb 2023 20:36:05 +0000 Subject: [PATCH 02/22] fix mypy --- gapic/utils/options.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gapic/utils/options.py b/gapic/utils/options.py index fa302ed1f5..daa77ff6b5 100644 --- a/gapic/utils/options.py +++ b/gapic/utils/options.py @@ -49,7 +49,7 @@ class Options: service_yaml_config: Dict[str, Any] = dataclasses.field( default_factory=dict) rest_numeric_enums: bool = False - proto_plus_deps: Tuple[str, ...] = dataclasses.field(default=()) + proto_plus_deps: Tuple[str, ...] = dataclasses.field(default=('',)) # Class constants PYTHON_GAPIC_PREFIX: str = 'python-gapic-' @@ -165,7 +165,7 @@ def tweak_path(p): if old_naming: autogen_snippets = False - proto_plus_deps = opts.pop('proto-plus-deps', []) + proto_plus_deps = tuple(opts.pop('proto-plus-deps', '')) if len(proto_plus_deps): proto_plus_deps = tuple(proto_plus_deps[0].split('+')) From 9711dd676f91d40203bca942cb5402c0ecf45f6c Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 1 Mar 2023 10:23:12 +0000 Subject: [PATCH 03/22] fix style --- gapic/schema/metadata.py | 4 ++-- tests/unit/generator/test_options.py | 1 + tests/unit/schema/test_api.py | 22 +++++++++++++++------- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/gapic/schema/metadata.py b/gapic/schema/metadata.py index 29c2b1c939..805fbb93ac 100644 --- a/gapic/schema/metadata.py +++ b/gapic/schema/metadata.py @@ -199,11 +199,11 @@ def python_import(self) -> imp.Import: # Check if the package name contains a version. version_regex = "^v\d(alpha|beta)?\d?" regex_match = re.match(version_regex, self.package[-1]) - + if regex_match: versioned_module = f"{self.package[-2]}_{regex_match[0]}" return imp.Import( - package=self.package[:-2] + + package=self.package[:-2] + (versioned_module, 'types'), module=self.module, alias=self.module_alias, diff --git a/tests/unit/generator/test_options.py b/tests/unit/generator/test_options.py index 215f160a63..6716e21fe6 100644 --- a/tests/unit/generator/test_options.py +++ b/tests/unit/generator/test_options.py @@ -224,6 +224,7 @@ def test_options_autogen_snippets_false_for_old_naming(): options = Options.build("old-naming,autogen-snippets=True") assert not options.autogen_snippets + def test_options_proto_plus_deps(): opts = Options.build("proto-plus-deps=") assert opts.proto_plus_deps == ('',) diff --git a/tests/unit/schema/test_api.py b/tests/unit/schema/test_api.py index ce023141fc..586c9b596a 100644 --- a/tests/unit/schema/test_api.py +++ b/tests/unit/schema/test_api.py @@ -727,12 +727,14 @@ def test_python_modules_nested(): make_file_pb2( name='bar.proto', package='google.bar', - messages=(make_message_pb2(name='AnotherImportedMessage', fields=()),), + messages=(make_message_pb2( + name='AnotherImportedMessage', fields=()),), ), make_file_pb2( name='baz.v2.proto', package='google.baz.v2', - messages=(make_message_pb2(name='YetAnotherImportedMessage', fields=()),), + messages=(make_message_pb2( + name='YetAnotherImportedMessage', fields=()),), ), make_file_pb2( name='common.proto', @@ -812,16 +814,22 @@ def test_python_modules_nested(): assert api_schema.protos['foo.proto'].python_modules == ( imp.Import(package=('google', 'bar'), module='bar_pb2'), - imp.Import(package=('google', 'baz', 'v2'), module='baz_v2_pb2'), + imp.Import(package=('google', 'baz', 'v2'), module='baz_pb2'), imp.Import(package=('google', 'dep'), module='dep_pb2'), ) - # Ensure that we can change the import statements in case the dependency - # uses proto-plus types - api_schema = api.API.build(fd, package='google.example.v1', opts=Options(proto_plus_deps=('google.bar+google.baz.v2'))) + + # Ensure that we can change the import statements to cater for a + # dependency that uses proto-plus types. + # For example, + # `from google.bar import bar_pb2` becomes `from google.bar.types import bar`` + # `from google.baz.v2 import baz_pb2` becomes `from google.baz_v2.types improt baz` + api_schema = api.API.build(fd, package='google.example.v1', opts=Options( + proto_plus_deps=('google.bar+google.baz.v2')) + ) assert api_schema.protos['foo.proto'].python_modules == ( imp.Import(package=('google', 'bar', 'types'), module='bar'), - imp.Import(package=('google', 'baz_v2', 'types'), module='baz_v2'), + imp.Import(package=('google', 'baz_v2', 'types'), module='baz'), imp.Import(package=('google', 'dep'), module='dep_pb2'), ) From 091356baebfb06759bc117fa59b23b096d92d552 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 1 Mar 2023 10:40:31 +0000 Subject: [PATCH 04/22] update goldens --- tests/integration/goldens/asset/setup.py | 1 - tests/integration/goldens/asset/testing/constraints-3.10.txt | 1 - tests/integration/goldens/asset/testing/constraints-3.11.txt | 1 - tests/integration/goldens/asset/testing/constraints-3.12.txt | 1 - tests/integration/goldens/asset/testing/constraints-3.7.txt | 1 - tests/integration/goldens/asset/testing/constraints-3.8.txt | 1 - tests/integration/goldens/asset/testing/constraints-3.9.txt | 1 - 7 files changed, 7 deletions(-) diff --git a/tests/integration/goldens/asset/setup.py b/tests/integration/goldens/asset/setup.py index fb0f7e544c..a67dcb103f 100755 --- a/tests/integration/goldens/asset/setup.py +++ b/tests/integration/goldens/asset/setup.py @@ -40,7 +40,6 @@ "proto-plus >= 1.22.0, <2.0.0dev", "proto-plus >= 1.22.2, <2.0.0dev; python_version>='3.11'", "protobuf>=3.19.5,<5.0.0dev,!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5", - 'grpc-google-iam-v1 >= 0.12.4, < 1.0.0dev', ] url = "https://github.com/googleapis/python-asset" diff --git a/tests/integration/goldens/asset/testing/constraints-3.10.txt b/tests/integration/goldens/asset/testing/constraints-3.10.txt index ad3f0fa58e..ed7f9aed25 100755 --- a/tests/integration/goldens/asset/testing/constraints-3.10.txt +++ b/tests/integration/goldens/asset/testing/constraints-3.10.txt @@ -4,4 +4,3 @@ google-api-core proto-plus protobuf -grpc-google-iam-v1 diff --git a/tests/integration/goldens/asset/testing/constraints-3.11.txt b/tests/integration/goldens/asset/testing/constraints-3.11.txt index ad3f0fa58e..ed7f9aed25 100755 --- a/tests/integration/goldens/asset/testing/constraints-3.11.txt +++ b/tests/integration/goldens/asset/testing/constraints-3.11.txt @@ -4,4 +4,3 @@ google-api-core proto-plus protobuf -grpc-google-iam-v1 diff --git a/tests/integration/goldens/asset/testing/constraints-3.12.txt b/tests/integration/goldens/asset/testing/constraints-3.12.txt index ad3f0fa58e..ed7f9aed25 100644 --- a/tests/integration/goldens/asset/testing/constraints-3.12.txt +++ b/tests/integration/goldens/asset/testing/constraints-3.12.txt @@ -4,4 +4,3 @@ google-api-core proto-plus protobuf -grpc-google-iam-v1 diff --git a/tests/integration/goldens/asset/testing/constraints-3.7.txt b/tests/integration/goldens/asset/testing/constraints-3.7.txt index 2beecf99e0..6c44adfea7 100755 --- a/tests/integration/goldens/asset/testing/constraints-3.7.txt +++ b/tests/integration/goldens/asset/testing/constraints-3.7.txt @@ -7,4 +7,3 @@ google-api-core==1.34.0 proto-plus==1.22.0 protobuf==3.19.5 -grpc-google-iam-v1==0.12.4 diff --git a/tests/integration/goldens/asset/testing/constraints-3.8.txt b/tests/integration/goldens/asset/testing/constraints-3.8.txt index ad3f0fa58e..ed7f9aed25 100755 --- a/tests/integration/goldens/asset/testing/constraints-3.8.txt +++ b/tests/integration/goldens/asset/testing/constraints-3.8.txt @@ -4,4 +4,3 @@ google-api-core proto-plus protobuf -grpc-google-iam-v1 diff --git a/tests/integration/goldens/asset/testing/constraints-3.9.txt b/tests/integration/goldens/asset/testing/constraints-3.9.txt index ad3f0fa58e..ed7f9aed25 100755 --- a/tests/integration/goldens/asset/testing/constraints-3.9.txt +++ b/tests/integration/goldens/asset/testing/constraints-3.9.txt @@ -4,4 +4,3 @@ google-api-core proto-plus protobuf -grpc-google-iam-v1 From 68e39cb2e5af66576e6d86f400ac48e27c4187cd Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 1 Mar 2023 10:45:20 +0000 Subject: [PATCH 05/22] fix tests --- tests/unit/schema/test_api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/schema/test_api.py b/tests/unit/schema/test_api.py index 586c9b596a..602d9302f5 100644 --- a/tests/unit/schema/test_api.py +++ b/tests/unit/schema/test_api.py @@ -814,7 +814,7 @@ def test_python_modules_nested(): assert api_schema.protos['foo.proto'].python_modules == ( imp.Import(package=('google', 'bar'), module='bar_pb2'), - imp.Import(package=('google', 'baz', 'v2'), module='baz_pb2'), + imp.Import(package=('google', 'baz', 'v2'), module='baz_v2_pb2'), imp.Import(package=('google', 'dep'), module='dep_pb2'), ) @@ -822,14 +822,14 @@ def test_python_modules_nested(): # dependency that uses proto-plus types. # For example, # `from google.bar import bar_pb2` becomes `from google.bar.types import bar`` - # `from google.baz.v2 import baz_pb2` becomes `from google.baz_v2.types improt baz` + # `from google.baz.v2 import baz_pb2` becomes `from google.baz_v2.types improt baz_v2` api_schema = api.API.build(fd, package='google.example.v1', opts=Options( proto_plus_deps=('google.bar+google.baz.v2')) ) assert api_schema.protos['foo.proto'].python_modules == ( imp.Import(package=('google', 'bar', 'types'), module='bar'), - imp.Import(package=('google', 'baz_v2', 'types'), module='baz'), + imp.Import(package=('google', 'baz_v2', 'types'), module='baz_v2'), imp.Import(package=('google', 'dep'), module='dep_pb2'), ) From de80fc932e0ea295cefa391927d103825464309f Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 1 Mar 2023 10:57:17 +0000 Subject: [PATCH 06/22] clean up --- .../services/%service/transports/rest.py.j2 | 6 +++--- .../%name_%version/%sub/test_%service.py.j2 | 18 +++++++++--------- .../services/%service/transports/rest.py.j2 | 6 +++--- .../gapic/%name_%version/%sub/test_macros.j2 | 18 +++++++++--------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/rest.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/rest.py.j2 index 8aca6b7290..156fc7d15a 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/rest.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/rest.py.j2 @@ -322,10 +322,10 @@ class {{service.name}}RestTransport({{service.name}}Transport): {% endfor %}{# rule in method.http_options #} ] request, metadata = self._interceptor.pre_{{ method.name|snake_case }}(request, metadata) - {% if not method.input.ident.is_proto_plus_type %} - pb_request = request - {% else %} + {% if method.input.ident.is_proto_plus_type %} pb_request = {{method.input.ident}}.pb(request) + {% else %} + pb_request = request {% endif %} transcoded_request = path_template.transcode(http_options, pb_request) diff --git a/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index e66980dc5f..f511be153b 100644 --- a/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -1118,10 +1118,10 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide {% endif %}{# default is str #} {% endfor %} request = request_type(**request_init) - {% if not method.input.ident.is_proto_plus_type %} - pb_request = request - {% else %} + {% if method.input.ident.is_proto_plus_type %} pb_request = request_type.pb(request) + {% else %} + pb_request = request {% endif %} jsonified_request = json.loads(json_format.MessageToJson( pb_request, @@ -1189,10 +1189,10 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide with mock.patch.object(path_template, 'transcode') as transcode: # A uri without fields and an empty body will force all the # request fields to show up in the query_params. - {% if not method.input.ident.is_proto_plus_type %} - pb_request = request - {% else %} + {% if method.input.ident.is_proto_plus_type %} pb_request = request_type.pb(request) + {% else %} + pb_request = request {% endif %} transcode_result = { 'uri': 'v1/sample_method', @@ -1287,10 +1287,10 @@ def test_{{ method_name }}_rest_interceptors(null_interceptor): {% if not method.void %} post.assert_not_called() {% endif %} - {% if not method.input.ident.is_proto_plus_type %} - pb_message = {{ method.input.ident }}() - {% else %} + {% if method.input.ident.is_proto_plus_type %} pb_message = {{ method.input.ident }}.pb({{ method.input.ident }}()) + {% else %} + pb_message = {{ method.input.ident }}() {% endif %} transcode.return_value = { "method": "post", diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 index e7fdf3213e..32ae7c8fb3 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 @@ -356,10 +356,10 @@ class {{service.name}}RestTransport({{service.name}}Transport): {% endfor %}{# rule in method.http_options #} ] request, metadata = self._interceptor.pre_{{ method.name|snake_case }}(request, metadata) - {% if not method.input.ident.is_proto_plus_type %} - pb_request = request - {% else %} + {% if method.input.ident.is_proto_plus_type %} pb_request = {{method.input.ident}}.pb(request) + {% else %} + pb_request = request {% endif %} transcoded_request = path_template.transcode(http_options, pb_request) diff --git a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2 b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2 index 98c654e425..bbdb5f1164 100644 --- a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2 +++ b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2 @@ -1009,10 +1009,10 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide {% endif %}{# default is str #} {% endfor %} request = request_type(**request_init) - {% if not method.input.ident.is_proto_plus_type %} - pb_request = request - {% else %} + {% if method.input.ident.is_proto_plus_type %} pb_request = request_type.pb(request) + {% else %} + pb_request = request {% endif %} jsonified_request = json.loads(json_format.MessageToJson( pb_request, @@ -1080,10 +1080,10 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide with mock.patch.object(path_template, 'transcode') as transcode: # A uri without fields and an empty body will force all the # request fields to show up in the query_params. - {% if not method.input.ident.is_proto_plus_type %} - pb_request = request - {% else %} + {% if method.input.ident.is_proto_plus_type %} pb_request = request_type.pb(request) + {% else %} + pb_request = request {% endif %} transcode_result = { 'uri': 'v1/sample_method', @@ -1178,10 +1178,10 @@ def test_{{ method_name }}_rest_interceptors(null_interceptor): {% if not method.void %} post.assert_not_called() {% endif %} - {% if not method.input.ident.is_proto_plus_type %} - pb_message = {{ method.input.ident }}() - {% else %} + {% if method.input.ident.is_proto_plus_type %} pb_message = {{ method.input.ident }}.pb({{ method.input.ident }}()) + {% else %} + pb_message = {{ method.input.ident }}() {% endif %} transcode.return_value = { "method": "post", From 15d3563e60f91e212541601f815d171e0128139b Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 1 Mar 2023 10:58:42 +0000 Subject: [PATCH 07/22] clean up --- .../services/%service/transports/rest.py.j2 | 6 +++--- .../%name_%version/%sub/test_%service.py.j2 | 6 +++--- .../services/%service/transports/rest.py.j2 | 6 +++--- .../gapic/%name_%version/%sub/test_macros.j2 | 18 +++++++++--------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/rest.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/rest.py.j2 index 156fc7d15a..e10fa77c29 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/rest.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/rest.py.j2 @@ -384,10 +384,10 @@ class {{service.name}}RestTransport({{service.name}}Transport): resp = rest_streaming.ResponseIterator(response, {{method.output.ident}}) {% else %} resp = {{method.output.ident}}() - {% if not method.output.ident.is_proto_plus_type %} - pb_resp = resp - {% else %} + {% if method.output.ident.is_proto_plus_type %} pb_resp = {{method.output.ident}}.pb(resp) + {% else %} + pb_resp = resp {% endif %} json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) diff --git a/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index f511be153b..c174d37e88 100644 --- a/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -1212,10 +1212,10 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide json_return_value = json_format.MessageToJson(return_value) {% else %} - {% if not method.output.ident.is_proto_plus_type %} - pb_return_value = return_value - {% else %} + {% if method.output.ident.is_proto_plus_type %} pb_return_value = {{ method.output.ident }}.pb(return_value) + {% else %} + pb_return_value = return_value {% endif %} json_return_value = json_format.MessageToJson(pb_return_value) {% endif %} diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 index 32ae7c8fb3..566b93ccda 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 @@ -418,10 +418,10 @@ class {{service.name}}RestTransport({{service.name}}Transport): resp = rest_streaming.ResponseIterator(response, {{method.output.ident}}) {% else %} resp = {{method.output.ident}}() - {% if not method.output.ident.is_proto_plus_type %} - pb_resp = resp - {% else %} + {% if method.output.ident.is_proto_plus_type %} pb_resp = {{method.output.ident}}.pb(resp) + {% else %} + pb_resp = resp {% endif %} json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) diff --git a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2 b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2 index bbdb5f1164..8c10abdf20 100644 --- a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2 +++ b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2 @@ -929,10 +929,10 @@ def test_{{ method_name }}_rest(request_type): {% elif method.lro %} json_return_value = json_format.MessageToJson(return_value) {% else %} - {% if not method.output.ident.is_proto_plus_type %} - pb_return_value = return_value - {% else %} + {% if method.output.ident.is_proto_plus_type %} pb_return_value = {{ method.output.ident }}.pb(return_value) + {% else %} + pb_return_value = return_value {% endif %} json_return_value = json_format.MessageToJson(pb_return_value) {% endif %} @@ -1103,10 +1103,10 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide json_return_value = json_format.MessageToJson(return_value) {% else %} - {% if not method.output.ident.is_proto_plus_type %} - pb_return_value = return_value - {% else %} + {% if method.output.ident.is_proto_plus_type %} pb_return_value = {{ method.output.ident }}.pb(return_value) + {% else %} + pb_return_value = return_value {% endif %} json_return_value = json_format.MessageToJson(pb_return_value) {% endif %} @@ -1295,10 +1295,10 @@ def test_{{ method_name }}_rest_flattened(): {% elif method.lro %} json_return_value = json_format.MessageToJson(return_value) {% else %} - {% if not method.output.ident.is_proto_plus_type %} - pb_return_value = return_value - {% else %} + {% if method.output.ident.is_proto_plus_type %} pb_return_value = {{ method.output.ident }}.pb(return_value) + {% else %} + pb_return_value = return_value {% endif %} json_return_value = json_format.MessageToJson(pb_return_value) {% endif %} From 4c10b61f8a210bbe92be558e5c9021fb564cfd3a Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 2 Mar 2023 20:24:37 +0000 Subject: [PATCH 08/22] add support for dependency google-cloud-kms --- gapic/schema/api.py | 5 ----- gapic/templates/setup.py.j2 | 7 +++++-- gapic/templates/testing/constraints-3.7.txt.j2 | 3 +++ tests/unit/schema/test_api.py | 3 +-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/gapic/schema/api.py b/gapic/schema/api.py index ab8507fc18..b9af9e348a 100644 --- a/gapic/schema/api.py +++ b/gapic/schema/api.py @@ -485,11 +485,6 @@ def gapic_metadata_json(self, options: Options) -> str: return MessageToJson(self.gapic_metadata(options), sort_keys=True) def requires_package(self, pkg: Tuple[str, ...]) -> bool: - # Quick check to make sure the package checked is different - # from this proto package - if self.naming.proto_package.startswith(pkg): - return False - return any( message.ident.package == pkg for proto in self.all_protos.values() diff --git a/gapic/templates/setup.py.j2 b/gapic/templates/setup.py.j2 index 9c750f55dd..d1f3db6c25 100644 --- a/gapic/templates/setup.py.j2 +++ b/gapic/templates/setup.py.j2 @@ -35,12 +35,15 @@ dependencies = [ {% if api.requires_package(('google', 'iam', 'v1')) or opts.add_iam_methods or api.has_iam_mixin %} "grpc-google-iam-v1 >= 0.12.4, < 1.0.0dev", {% endif %} - {% if api.requires_package(('google', 'cloud', 'documentai', 'v1')) %} + {% if api.requires_package(('google', 'cloud', 'documentai', 'v1')) and api.naming.warehouse_package_name != 'google-cloud-documentai' %} "google-cloud-documentai >= 1.2.1, < 3.0.0dev", {% endif %} - {% if api.requires_package(('google', 'apps', 'script', 'type')) %} + {% if api.requires_package(('google', 'apps', 'script', 'type')) and api.naming.warehouse_package_name != 'google-apps-script-type' %} "google-apps-script-type>=0.2.0, <1.0.0dev", {% endif %} + {% if api.requires_package(('google', 'cloud', 'kms', 'v1')) and api.naming.warehouse_package_name != 'google-cloud-kms' %} + "google-cloud-kms>=2.3.0, <3.0.0dev", + {% endif %} ] url = "https://github.com/googleapis/python-{{ api.naming.warehouse_package_name|replace("google-cloud-", "")|replace("google-", "") }}" diff --git a/gapic/templates/testing/constraints-3.7.txt.j2 b/gapic/templates/testing/constraints-3.7.txt.j2 index 73c04e56c3..301b7dd5b9 100644 --- a/gapic/templates/testing/constraints-3.7.txt.j2 +++ b/gapic/templates/testing/constraints-3.7.txt.j2 @@ -13,3 +13,6 @@ grpc-google-iam-v1==0.12.4 {% if api.requires_package(('google', 'cloud', 'documentai', 'v1')) %} google-cloud-documentai==1.2.1 {% endif %} +{% if api.requires_package(('google', 'cloud', 'kms', 'v1')) %} +google-cloud-kms==2.3.0 +{% endif %} diff --git a/tests/unit/schema/test_api.py b/tests/unit/schema/test_api.py index 602d9302f5..c93ff18eda 100644 --- a/tests/unit/schema/test_api.py +++ b/tests/unit/schema/test_api.py @@ -108,8 +108,7 @@ def test_api_build(): imp.Import(package=('google', 'dep'), module='dep_pb2'), ) - # The package should not require itself - assert not api_schema.requires_package(('google', 'example', 'v1')) + assert api_schema.requires_package(('google', 'example', 'v1')) assert not api_schema.requires_package(('elgoog', 'example', 'v1')) From 1e8eb7f42ece7c5b006d276f9bcb48aa16048808 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Mon, 6 Mar 2023 11:52:08 +0000 Subject: [PATCH 09/22] Address review comments --- gapic/schema/metadata.py | 2 +- gapic/templates/setup.py.j2 | 19 +-- .../templates/testing/_default_constraints.j2 | 12 +- .../templates/testing/constraints-3.7.txt.j2 | 13 +- tests/integration/goldens/asset/setup.py | 1 + .../asset/testing/constraints-3.10.txt | 1 + .../asset/testing/constraints-3.11.txt | 1 + .../asset/testing/constraints-3.12.txt | 1 + .../goldens/asset/testing/constraints-3.7.txt | 1 + .../goldens/asset/testing/constraints-3.8.txt | 1 + .../goldens/asset/testing/constraints-3.9.txt | 1 + tests/unit/schema/test_api.py | 153 +++++++++++++++--- 12 files changed, 163 insertions(+), 43 deletions(-) diff --git a/gapic/schema/metadata.py b/gapic/schema/metadata.py index 805fbb93ac..7820017d0e 100644 --- a/gapic/schema/metadata.py +++ b/gapic/schema/metadata.py @@ -197,7 +197,7 @@ def python_import(self) -> imp.Import: # underscore between the module and the version. For example, # change google.cloud.documentai.v1 to google.cloud.documentai_v1. # Check if the package name contains a version. - version_regex = "^v\d(alpha|beta)?\d?" + version_regex = "^v\d[^/]*$" regex_match = re.match(version_regex, self.package[-1]) if regex_match: diff --git a/gapic/templates/setup.py.j2 b/gapic/templates/setup.py.j2 index d1f3db6c25..46d64b3001 100644 --- a/gapic/templates/setup.py.j2 +++ b/gapic/templates/setup.py.j2 @@ -1,5 +1,5 @@ {% extends '_base.py.j2' %} - +{% from '_pypi_packages.j2' import pypi_packages %} {% block content %} import io @@ -31,19 +31,14 @@ dependencies = [ "proto-plus >= 1.22.0, <2.0.0dev", "proto-plus >= 1.22.2, <2.0.0dev; python_version>='3.11'", "protobuf>=3.19.5,<5.0.0dev,!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5", - {# TODO: Remove after https://github.com/googleapis/gapic-generator-python/pull/1240 is merged. #} - {% if api.requires_package(('google', 'iam', 'v1')) or opts.add_iam_methods or api.has_iam_mixin %} - "grpc-google-iam-v1 >= 0.12.4, < 1.0.0dev", - {% endif %} - {% if api.requires_package(('google', 'cloud', 'documentai', 'v1')) and api.naming.warehouse_package_name != 'google-cloud-documentai' %} - "google-cloud-documentai >= 1.2.1, < 3.0.0dev", - {% endif %} - {% if api.requires_package(('google', 'apps', 'script', 'type')) and api.naming.warehouse_package_name != 'google-apps-script-type' %} - "google-apps-script-type>=0.2.0, <1.0.0dev", + {% for package in pypi_packages %} + {# Quick check to make sure the package is different from this setup.py #} + {% if not api.naming.warehouse_package_name in pypi_packages[package] %} + {% if api.requires_package(package) %} + "{{ pypi_packages[package][0] }} >= {{ pypi_packages[package][1] }}, <{{ pypi_packages[package][2] }}", {% endif %} - {% if api.requires_package(('google', 'cloud', 'kms', 'v1')) and api.naming.warehouse_package_name != 'google-cloud-kms' %} - "google-cloud-kms>=2.3.0, <3.0.0dev", {% endif %} + {% endfor %} ] url = "https://github.com/googleapis/python-{{ api.naming.warehouse_package_name|replace("google-cloud-", "")|replace("google-", "") }}" diff --git a/gapic/templates/testing/_default_constraints.j2 b/gapic/templates/testing/_default_constraints.j2 index fe0a64d4b5..998169eacf 100644 --- a/gapic/templates/testing/_default_constraints.j2 +++ b/gapic/templates/testing/_default_constraints.j2 @@ -1,11 +1,13 @@ +{% from '_pypi_packages.j2' import pypi_packages %} # This constraints file is required for unit tests. # List all library dependencies and extras in this file. google-api-core proto-plus protobuf -{% if api.requires_package(('google', 'iam', 'v1')) or opts.add_iam_methods or api.has_iam_mixin %} -grpc-google-iam-v1 +{% for package in pypi_packages %} +{% if not api.naming.warehouse_package_name in pypi_packages[package] %} +{% if api.requires_package(package) %} +{{ pypi_packages[package][0] }} {% endif %} -{% if api.requires_package(('google', 'cloud', 'documentai', 'v1')) %} -google-cloud-documentai -{% endif %} \ No newline at end of file +{% endif %} +{% endfor %} diff --git a/gapic/templates/testing/constraints-3.7.txt.j2 b/gapic/templates/testing/constraints-3.7.txt.j2 index 301b7dd5b9..3b3be987bf 100644 --- a/gapic/templates/testing/constraints-3.7.txt.j2 +++ b/gapic/templates/testing/constraints-3.7.txt.j2 @@ -1,3 +1,4 @@ +{% from '_pypi_packages.j2' import pypi_packages %} # This constraints file is used to check that lower bounds # are correct in setup.py # List all library dependencies and extras in this file. @@ -7,12 +8,10 @@ google-api-core==1.34.0 proto-plus==1.22.0 protobuf==3.19.5 -{% if api.requires_package(('google', 'iam', 'v1')) or opts.add_iam_methods or api.has_iam_mixin %} -grpc-google-iam-v1==0.12.4 +{% for package in pypi_packages %} +{% if not api.naming.warehouse_package_name in pypi_packages[package] %} +{% if api.requires_package(package) %} +{{ pypi_packages[package][0] }}=={{ pypi_packages[package][1] }} {% endif %} -{% if api.requires_package(('google', 'cloud', 'documentai', 'v1')) %} -google-cloud-documentai==1.2.1 -{% endif %} -{% if api.requires_package(('google', 'cloud', 'kms', 'v1')) %} -google-cloud-kms==2.3.0 {% endif %} +{% endfor %} diff --git a/tests/integration/goldens/asset/setup.py b/tests/integration/goldens/asset/setup.py index a67dcb103f..ccbd7491c3 100755 --- a/tests/integration/goldens/asset/setup.py +++ b/tests/integration/goldens/asset/setup.py @@ -40,6 +40,7 @@ "proto-plus >= 1.22.0, <2.0.0dev", "proto-plus >= 1.22.2, <2.0.0dev; python_version>='3.11'", "protobuf>=3.19.5,<5.0.0dev,!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5", + "grpc-google-iam-v1 >= 0.12.4, <1.0.0dev", ] url = "https://github.com/googleapis/python-asset" diff --git a/tests/integration/goldens/asset/testing/constraints-3.10.txt b/tests/integration/goldens/asset/testing/constraints-3.10.txt index ed7f9aed25..ad3f0fa58e 100755 --- a/tests/integration/goldens/asset/testing/constraints-3.10.txt +++ b/tests/integration/goldens/asset/testing/constraints-3.10.txt @@ -4,3 +4,4 @@ google-api-core proto-plus protobuf +grpc-google-iam-v1 diff --git a/tests/integration/goldens/asset/testing/constraints-3.11.txt b/tests/integration/goldens/asset/testing/constraints-3.11.txt index ed7f9aed25..ad3f0fa58e 100755 --- a/tests/integration/goldens/asset/testing/constraints-3.11.txt +++ b/tests/integration/goldens/asset/testing/constraints-3.11.txt @@ -4,3 +4,4 @@ google-api-core proto-plus protobuf +grpc-google-iam-v1 diff --git a/tests/integration/goldens/asset/testing/constraints-3.12.txt b/tests/integration/goldens/asset/testing/constraints-3.12.txt index ed7f9aed25..ad3f0fa58e 100755 --- a/tests/integration/goldens/asset/testing/constraints-3.12.txt +++ b/tests/integration/goldens/asset/testing/constraints-3.12.txt @@ -4,3 +4,4 @@ google-api-core proto-plus protobuf +grpc-google-iam-v1 diff --git a/tests/integration/goldens/asset/testing/constraints-3.7.txt b/tests/integration/goldens/asset/testing/constraints-3.7.txt index 6c44adfea7..2beecf99e0 100755 --- a/tests/integration/goldens/asset/testing/constraints-3.7.txt +++ b/tests/integration/goldens/asset/testing/constraints-3.7.txt @@ -7,3 +7,4 @@ google-api-core==1.34.0 proto-plus==1.22.0 protobuf==3.19.5 +grpc-google-iam-v1==0.12.4 diff --git a/tests/integration/goldens/asset/testing/constraints-3.8.txt b/tests/integration/goldens/asset/testing/constraints-3.8.txt index ed7f9aed25..ad3f0fa58e 100755 --- a/tests/integration/goldens/asset/testing/constraints-3.8.txt +++ b/tests/integration/goldens/asset/testing/constraints-3.8.txt @@ -4,3 +4,4 @@ google-api-core proto-plus protobuf +grpc-google-iam-v1 diff --git a/tests/integration/goldens/asset/testing/constraints-3.9.txt b/tests/integration/goldens/asset/testing/constraints-3.9.txt index ed7f9aed25..ad3f0fa58e 100755 --- a/tests/integration/goldens/asset/testing/constraints-3.9.txt +++ b/tests/integration/goldens/asset/testing/constraints-3.9.txt @@ -4,3 +4,4 @@ google-api-core proto-plus protobuf +grpc-google-iam-v1 diff --git a/tests/unit/schema/test_api.py b/tests/unit/schema/test_api.py index c93ff18eda..fd1c074a67 100644 --- a/tests/unit/schema/test_api.py +++ b/tests/unit/schema/test_api.py @@ -724,16 +724,52 @@ def test_python_modules_nested(): messages=(make_message_pb2(name='ImportedMessage', fields=()),), ), make_file_pb2( - name='bar.proto', - package='google.bar', + name='baa.proto', + package='google.baa', messages=(make_message_pb2( - name='AnotherImportedMessage', fields=()),), + name='ImportedMessageBaa', fields=()),), ), make_file_pb2( - name='baz.v2.proto', - package='google.baz.v2', + name='bab.v1.proto', + package='google.bab.v1', messages=(make_message_pb2( - name='YetAnotherImportedMessage', fields=()),), + name='ImportedMessageBab', fields=()),), + ), + make_file_pb2( + name='bac.v10.proto', + package='google.bac.v10', + messages=(make_message_pb2( + name='ImportedMessageBac', fields=()),), + ), + make_file_pb2( + name='bad.v2beta.proto', + package='google.bad.v2beta', + messages=(make_message_pb2( + name='ImportedMessageBad', fields=()),), + ), + make_file_pb2( + name='bae.v2beta20.proto', + package='google.bae.v2beta20', + messages=(make_message_pb2( + name='ImportedMessageBae', fields=()),), + ), + make_file_pb2( + name='baf.v20beta.proto', + package='google.baf.v20beta', + messages=(make_message_pb2( + name='ImportedMessageBaf', fields=()),), + ), + make_file_pb2( + name='bag.v20p1.proto', + package='google.bag.v20p1', + messages=(make_message_pb2( + name='ImportedMessageBag', fields=()),), + ), + make_file_pb2( + name='bah.v20p1alpha3p5.proto', + package='google.bah.v20p1alpha3p5', + messages=(make_message_pb2( + name='ImportedMessageBah', fields=()),), ), make_file_pb2( name='common.proto', @@ -766,21 +802,75 @@ def test_python_modules_nested(): ), ), make_message_pb2( - name='Bar', + name='Baa', fields=( make_field_pb2( - name='another_imported_message', + name='imported_message_baa', number=1, - type_name='.google.bar.AnotherImportedMessage'), + type_name='.google.baa.ImportedMessageBaa'), ), ), make_message_pb2( - name='Baz', + name='Bab', fields=( make_field_pb2( - name='yet_another_imported_message', + name='imported_message_bab', number=1, - type_name='.google.baz.v2.YetAnotherImportedMessage'), + type_name='.google.bab.v1.ImportedMessageBab'), + ), + ), + make_message_pb2( + name='Bac', + fields=( + make_field_pb2( + name='imported_message_bac', + number=1, + type_name='.google.bac.v10.ImportedMessageBac'), + ), + ), + make_message_pb2( + name='Bad', + fields=( + make_field_pb2( + name='imported_message_bad', + number=1, + type_name='.google.bad.v2beta.ImportedMessageBad'), + ), + ), + make_message_pb2( + name='Bae', + fields=( + make_field_pb2( + name='imported_message_bae', + number=1, + type_name='.google.bae.v2beta20.ImportedMessageBae'), + ), + ), + make_message_pb2( + name='Baf', + fields=( + make_field_pb2( + name='imported_message_baf', + number=1, + type_name='.google.baf.v20beta.ImportedMessageBaf'), + ), + ), + make_message_pb2( + name='Bag', + fields=( + make_field_pb2( + name='imported_message_bag', + number=1, + type_name='.google.bag.v20p1.ImportedMessageBag'), + ), + ), + make_message_pb2( + name='Bah', + fields=( + make_field_pb2( + name='imported_message_bah', + number=1, + type_name='.google.bah.v20p1alpha3p5.ImportedMessageBah'), ), ), ), @@ -812,8 +902,14 @@ def test_python_modules_nested(): api_schema = api.API.build(fd, package='google.example.v1') assert api_schema.protos['foo.proto'].python_modules == ( - imp.Import(package=('google', 'bar'), module='bar_pb2'), - imp.Import(package=('google', 'baz', 'v2'), module='baz_v2_pb2'), + imp.Import(package=('google', 'baa'), module='baa_pb2'), + imp.Import(package=('google', 'bab', 'v1'), module='bab_v1_pb2'), + imp.Import(package=('google', 'bac', 'v10'), module='bac_v10_pb2'), + imp.Import(package=('google', 'bad', 'v2beta'), module='bad_v2beta_pb2'), + imp.Import(package=('google', 'bae', 'v2beta20'), module='bae_v2beta20_pb2'), + imp.Import(package=('google', 'baf', 'v20beta'), module='baf_v20beta_pb2'), + imp.Import(package=('google', 'bag', 'v20p1'), module='bag_v20p1_pb2'), + imp.Import(package=('google', 'bah', 'v20p1alpha3p5'), module='bah_v20p1alpha3p5_pb2'), imp.Import(package=('google', 'dep'), module='dep_pb2'), ) @@ -822,13 +918,34 @@ def test_python_modules_nested(): # For example, # `from google.bar import bar_pb2` becomes `from google.bar.types import bar`` # `from google.baz.v2 import baz_pb2` becomes `from google.baz_v2.types improt baz_v2` - api_schema = api.API.build(fd, package='google.example.v1', opts=Options( - proto_plus_deps=('google.bar+google.baz.v2')) + api_schema = api.API.build( + fd, + package="google.example.v1", + opts=Options( + proto_plus_deps="+".join( + ( + "google.baa", + "google.bab.v1", + "google.bac.v10", + "google.bad.v2beta", + "google.bae.v2beta20", + "google.baf.v20beta", + "google.bag.v20p1", + "google.bah.v20p1alpha3p5", + ) + ) + ), ) assert api_schema.protos['foo.proto'].python_modules == ( - imp.Import(package=('google', 'bar', 'types'), module='bar'), - imp.Import(package=('google', 'baz_v2', 'types'), module='baz_v2'), + imp.Import(package=('google', 'baa', 'types'), module='baa'), + imp.Import(package=('google', 'bab_v1', 'types'), module='bab_v1'), + imp.Import(package=('google', 'bac_v10', 'types'), module='bac_v10'), + imp.Import(package=('google', 'bad_v2beta', 'types'), module='bad_v2beta'), + imp.Import(package=('google', 'bae_v2beta20', 'types'), module='bae_v2beta20'), + imp.Import(package=('google', 'baf_v20beta', 'types'), module='baf_v20beta'), + imp.Import(package=('google', 'bag_v20p1', 'types'), module='bag_v20p1'), + imp.Import(package=('google', 'bah_v20p1alpha3p5', 'types'), module='bah_v20p1alpha3p5'), imp.Import(package=('google', 'dep'), module='dep_pb2'), ) From 4012b67edc588e28f111ed1af33c3f119993e09b Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Mon, 6 Mar 2023 11:52:21 +0000 Subject: [PATCH 10/22] add missing file --- gapic/templates/_pypi_packages.j2 | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 gapic/templates/_pypi_packages.j2 diff --git a/gapic/templates/_pypi_packages.j2 b/gapic/templates/_pypi_packages.j2 new file mode 100644 index 0000000000..ea2105c60c --- /dev/null +++ b/gapic/templates/_pypi_packages.j2 @@ -0,0 +1,11 @@ + +{% set pypi_packages = dict([ + (("google", "apps", "script", "type"), ("google-apps-script-type", "0.2.0", "1.0.0dev")), + (("google", "cloud", "documentai", "v1"), ("google-cloud-documentai", "1.2.1", "3.0.0dev")), + (("google", "cloud", "kms", "v1"), ("google-cloud-kms", "2.3.0, <3.0.0dev")), + (("google", "iam", "v1"), ("grpc-google-iam-v1", "0.12.4", "1.0.0dev")), +]) %} \ No newline at end of file From 31f5ffdcdfeb2fd912cb2fb813eaaf63efba35e6 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 8 Mar 2023 13:41:18 +0000 Subject: [PATCH 11/22] run formatting tool --- tests/unit/schema/test_api.py | 249 +++++++++++++++++----------------- 1 file changed, 128 insertions(+), 121 deletions(-) diff --git a/tests/unit/schema/test_api.py b/tests/unit/schema/test_api.py index fd1c074a67..f07a5bd222 100644 --- a/tests/unit/schema/test_api.py +++ b/tests/unit/schema/test_api.py @@ -719,198 +719,203 @@ def test_undefined_type(): def test_python_modules_nested(): fd = ( make_file_pb2( - name='dep.proto', - package='google.dep', - messages=(make_message_pb2(name='ImportedMessage', fields=()),), + name="dep.proto", + package="google.dep", + messages=(make_message_pb2(name="ImportedMessage", fields=()),), ), make_file_pb2( - name='baa.proto', - package='google.baa', - messages=(make_message_pb2( - name='ImportedMessageBaa', fields=()),), + name="baa.proto", + package="google.baa", + messages=(make_message_pb2(name="ImportedMessageBaa", fields=()),), ), make_file_pb2( - name='bab.v1.proto', - package='google.bab.v1', - messages=(make_message_pb2( - name='ImportedMessageBab', fields=()),), + name="bab.v1.proto", + package="google.bab.v1", + messages=(make_message_pb2(name="ImportedMessageBab", fields=()),), ), make_file_pb2( - name='bac.v10.proto', - package='google.bac.v10', - messages=(make_message_pb2( - name='ImportedMessageBac', fields=()),), + name="bac.v10.proto", + package="google.bac.v10", + messages=(make_message_pb2(name="ImportedMessageBac", fields=()),), ), make_file_pb2( - name='bad.v2beta.proto', - package='google.bad.v2beta', - messages=(make_message_pb2( - name='ImportedMessageBad', fields=()),), + name="bad.v2beta.proto", + package="google.bad.v2beta", + messages=(make_message_pb2(name="ImportedMessageBad", fields=()),), ), make_file_pb2( - name='bae.v2beta20.proto', - package='google.bae.v2beta20', - messages=(make_message_pb2( - name='ImportedMessageBae', fields=()),), + name="bae.v2beta20.proto", + package="google.bae.v2beta20", + messages=(make_message_pb2(name="ImportedMessageBae", fields=()),), ), make_file_pb2( - name='baf.v20beta.proto', - package='google.baf.v20beta', - messages=(make_message_pb2( - name='ImportedMessageBaf', fields=()),), + name="baf.v20beta.proto", + package="google.baf.v20beta", + messages=(make_message_pb2(name="ImportedMessageBaf", fields=()),), ), make_file_pb2( - name='bag.v20p1.proto', - package='google.bag.v20p1', - messages=(make_message_pb2( - name='ImportedMessageBag', fields=()),), + name="bag.v20p1.proto", + package="google.bag.v20p1", + messages=(make_message_pb2(name="ImportedMessageBag", fields=()),), ), make_file_pb2( - name='bah.v20p1alpha3p5.proto', - package='google.bah.v20p1alpha3p5', - messages=(make_message_pb2( - name='ImportedMessageBah', fields=()),), + name="bah.v20p1alpha3p5.proto", + package="google.bah.v20p1alpha3p5", + messages=(make_message_pb2(name="ImportedMessageBah", fields=()),), ), make_file_pb2( - name='common.proto', - package='google.example.v1.common', - messages=(make_message_pb2(name='Bar'),), + name="common.proto", + package="google.example.v1.common", + messages=(make_message_pb2(name="Bar"),), ), make_file_pb2( - name='foo.proto', - package='google.example.v1', + name="foo.proto", + package="google.example.v1", messages=( make_message_pb2( - name='GetFooRequest', + name="GetFooRequest", fields=( - make_field_pb2(name='primitive', number=2, type=1), + make_field_pb2(name="primitive", number=2, type=1), make_field_pb2( - name='foo', + name="foo", number=3, type=1, - type_name='.google.example.v1.GetFooRequest.Foo', + type_name=".google.example.v1.GetFooRequest.Foo", ), ), nested_type=( make_message_pb2( - name='Foo', + name="Foo", fields=( make_field_pb2( - name='imported_message', + name="imported_message", number=1, - type_name='.google.dep.ImportedMessage'), + type_name=".google.dep.ImportedMessage", + ), ), ), make_message_pb2( - name='Baa', + name="Baa", fields=( make_field_pb2( - name='imported_message_baa', + name="imported_message_baa", number=1, - type_name='.google.baa.ImportedMessageBaa'), + type_name=".google.baa.ImportedMessageBaa", + ), ), ), make_message_pb2( - name='Bab', + name="Bab", fields=( make_field_pb2( - name='imported_message_bab', + name="imported_message_bab", number=1, - type_name='.google.bab.v1.ImportedMessageBab'), + type_name=".google.bab.v1.ImportedMessageBab", + ), ), ), make_message_pb2( - name='Bac', - fields=( - make_field_pb2( - name='imported_message_bac', - number=1, - type_name='.google.bac.v10.ImportedMessageBac'), + name="Bac", + fields=( + make_field_pb2( + name="imported_message_bac", + number=1, + type_name=".google.bac.v10.ImportedMessageBac", + ), ), ), make_message_pb2( - name='Bad', - fields=( - make_field_pb2( - name='imported_message_bad', - number=1, - type_name='.google.bad.v2beta.ImportedMessageBad'), + name="Bad", + fields=( + make_field_pb2( + name="imported_message_bad", + number=1, + type_name=".google.bad.v2beta.ImportedMessageBad", + ), ), ), make_message_pb2( - name='Bae', - fields=( - make_field_pb2( - name='imported_message_bae', - number=1, - type_name='.google.bae.v2beta20.ImportedMessageBae'), + name="Bae", + fields=( + make_field_pb2( + name="imported_message_bae", + number=1, + type_name=".google.bae.v2beta20.ImportedMessageBae", + ), ), ), make_message_pb2( - name='Baf', - fields=( - make_field_pb2( - name='imported_message_baf', - number=1, - type_name='.google.baf.v20beta.ImportedMessageBaf'), + name="Baf", + fields=( + make_field_pb2( + name="imported_message_baf", + number=1, + type_name=".google.baf.v20beta.ImportedMessageBaf", + ), ), ), make_message_pb2( - name='Bag', - fields=( - make_field_pb2( - name='imported_message_bag', - number=1, - type_name='.google.bag.v20p1.ImportedMessageBag'), + name="Bag", + fields=( + make_field_pb2( + name="imported_message_bag", + number=1, + type_name=".google.bag.v20p1.ImportedMessageBag", + ), ), ), make_message_pb2( - name='Bah', - fields=( - make_field_pb2( - name='imported_message_bah', - number=1, - type_name='.google.bah.v20p1alpha3p5.ImportedMessageBah'), + name="Bah", + fields=( + make_field_pb2( + name="imported_message_bah", + number=1, + type_name=".google.bah.v20p1alpha3p5.ImportedMessageBah", + ), ), ), ), ), make_message_pb2( - name='GetFooResponse', + name="GetFooResponse", fields=( make_field_pb2( - name='foo', + name="foo", number=1, - type_name='.google.example.v1.GetFooRequest.Foo', + type_name=".google.example.v1.GetFooRequest.Foo", ), ), ), ), - services=(descriptor_pb2.ServiceDescriptorProto( - name='FooService', - method=( - descriptor_pb2.MethodDescriptorProto( - name='GetFoo', - input_type='google.example.v1.GetFooRequest', - output_type='google.example.v1.GetFooResponse', + services=( + descriptor_pb2.ServiceDescriptorProto( + name="FooService", + method=( + descriptor_pb2.MethodDescriptorProto( + name="GetFoo", + input_type="google.example.v1.GetFooRequest", + output_type="google.example.v1.GetFooResponse", + ), ), ), - ),), + ), ), ) - api_schema = api.API.build(fd, package='google.example.v1') + api_schema = api.API.build(fd, package="google.example.v1") - assert api_schema.protos['foo.proto'].python_modules == ( - imp.Import(package=('google', 'baa'), module='baa_pb2'), - imp.Import(package=('google', 'bab', 'v1'), module='bab_v1_pb2'), - imp.Import(package=('google', 'bac', 'v10'), module='bac_v10_pb2'), - imp.Import(package=('google', 'bad', 'v2beta'), module='bad_v2beta_pb2'), - imp.Import(package=('google', 'bae', 'v2beta20'), module='bae_v2beta20_pb2'), - imp.Import(package=('google', 'baf', 'v20beta'), module='baf_v20beta_pb2'), - imp.Import(package=('google', 'bag', 'v20p1'), module='bag_v20p1_pb2'), - imp.Import(package=('google', 'bah', 'v20p1alpha3p5'), module='bah_v20p1alpha3p5_pb2'), - imp.Import(package=('google', 'dep'), module='dep_pb2'), + assert api_schema.protos["foo.proto"].python_modules == ( + imp.Import(package=("google", "baa"), module="baa_pb2"), + imp.Import(package=("google", "bab", "v1"), module="bab_v1_pb2"), + imp.Import(package=("google", "bac", "v10"), module="bac_v10_pb2"), + imp.Import(package=("google", "bad", "v2beta"), module="bad_v2beta_pb2"), + imp.Import(package=("google", "bae", "v2beta20"), module="bae_v2beta20_pb2"), + imp.Import(package=("google", "baf", "v20beta"), module="baf_v20beta_pb2"), + imp.Import(package=("google", "bag", "v20p1"), module="bag_v20p1_pb2"), + imp.Import( + package=("google", "bah", "v20p1alpha3p5"), module="bah_v20p1alpha3p5_pb2" + ), + imp.Import(package=("google", "dep"), module="dep_pb2"), ) # Ensure that we can change the import statements to cater for a @@ -937,16 +942,18 @@ def test_python_modules_nested(): ), ) - assert api_schema.protos['foo.proto'].python_modules == ( - imp.Import(package=('google', 'baa', 'types'), module='baa'), - imp.Import(package=('google', 'bab_v1', 'types'), module='bab_v1'), - imp.Import(package=('google', 'bac_v10', 'types'), module='bac_v10'), - imp.Import(package=('google', 'bad_v2beta', 'types'), module='bad_v2beta'), - imp.Import(package=('google', 'bae_v2beta20', 'types'), module='bae_v2beta20'), - imp.Import(package=('google', 'baf_v20beta', 'types'), module='baf_v20beta'), - imp.Import(package=('google', 'bag_v20p1', 'types'), module='bag_v20p1'), - imp.Import(package=('google', 'bah_v20p1alpha3p5', 'types'), module='bah_v20p1alpha3p5'), - imp.Import(package=('google', 'dep'), module='dep_pb2'), + assert api_schema.protos["foo.proto"].python_modules == ( + imp.Import(package=("google", "baa", "types"), module="baa"), + imp.Import(package=("google", "bab_v1", "types"), module="bab_v1"), + imp.Import(package=("google", "bac_v10", "types"), module="bac_v10"), + imp.Import(package=("google", "bad_v2beta", "types"), module="bad_v2beta"), + imp.Import(package=("google", "bae_v2beta20", "types"), module="bae_v2beta20"), + imp.Import(package=("google", "baf_v20beta", "types"), module="baf_v20beta"), + imp.Import(package=("google", "bag_v20p1", "types"), module="bag_v20p1"), + imp.Import( + package=("google", "bah_v20p1alpha3p5", "types"), module="bah_v20p1alpha3p5" + ), + imp.Import(package=("google", "dep"), module="dep_pb2"), ) From 19cfea6431b5264165f89757f5707bdff1a4dc2b Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 8 Mar 2023 13:44:27 +0000 Subject: [PATCH 12/22] style --- tests/unit/schema/test_api.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/tests/unit/schema/test_api.py b/tests/unit/schema/test_api.py index f07a5bd222..f92e790734 100644 --- a/tests/unit/schema/test_api.py +++ b/tests/unit/schema/test_api.py @@ -908,9 +908,12 @@ def test_python_modules_nested(): imp.Import(package=("google", "baa"), module="baa_pb2"), imp.Import(package=("google", "bab", "v1"), module="bab_v1_pb2"), imp.Import(package=("google", "bac", "v10"), module="bac_v10_pb2"), - imp.Import(package=("google", "bad", "v2beta"), module="bad_v2beta_pb2"), - imp.Import(package=("google", "bae", "v2beta20"), module="bae_v2beta20_pb2"), - imp.Import(package=("google", "baf", "v20beta"), module="baf_v20beta_pb2"), + imp.Import(package=("google", "bad", "v2beta"), + module="bad_v2beta_pb2"), + imp.Import(package=("google", "bae", "v2beta20"), + module="bae_v2beta20_pb2"), + imp.Import(package=("google", "baf", "v20beta"), + module="baf_v20beta_pb2"), imp.Import(package=("google", "bag", "v20p1"), module="bag_v20p1_pb2"), imp.Import( package=("google", "bah", "v20p1alpha3p5"), module="bah_v20p1alpha3p5_pb2" @@ -946,10 +949,14 @@ def test_python_modules_nested(): imp.Import(package=("google", "baa", "types"), module="baa"), imp.Import(package=("google", "bab_v1", "types"), module="bab_v1"), imp.Import(package=("google", "bac_v10", "types"), module="bac_v10"), - imp.Import(package=("google", "bad_v2beta", "types"), module="bad_v2beta"), - imp.Import(package=("google", "bae_v2beta20", "types"), module="bae_v2beta20"), - imp.Import(package=("google", "baf_v20beta", "types"), module="baf_v20beta"), - imp.Import(package=("google", "bag_v20p1", "types"), module="bag_v20p1"), + imp.Import(package=("google", "bad_v2beta", + "types"), module="bad_v2beta"), + imp.Import(package=("google", "bae_v2beta20", + "types"), module="bae_v2beta20"), + imp.Import(package=("google", "baf_v20beta", + "types"), module="baf_v20beta"), + imp.Import(package=("google", "bag_v20p1", + "types"), module="bag_v20p1"), imp.Import( package=("google", "bah_v20p1alpha3p5", "types"), module="bah_v20p1alpha3p5" ), From ea60dc7bc8daa7850bfa46f298583efca766896f Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 8 Mar 2023 16:52:20 +0000 Subject: [PATCH 13/22] address review comments --- gapic/templates/_pypi_packages.j2 | 13 +++++++------ gapic/templates/setup.py.j2 | 8 ++++---- gapic/templates/testing/_default_constraints.j2 | 9 +++++---- gapic/templates/testing/constraints-3.7.txt.j2 | 9 +++++---- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/gapic/templates/_pypi_packages.j2 b/gapic/templates/_pypi_packages.j2 index ea2105c60c..63d017dabe 100644 --- a/gapic/templates/_pypi_packages.j2 +++ b/gapic/templates/_pypi_packages.j2 @@ -3,9 +3,10 @@ Dict of PyPI packages available by API in the form , Where the first contains the API and the second tuple contains the PyPI package name, the minimum allowed version and the maximum allowed version. --> -{% set pypi_packages = dict([ - (("google", "apps", "script", "type"), ("google-apps-script-type", "0.2.0", "1.0.0dev")), - (("google", "cloud", "documentai", "v1"), ("google-cloud-documentai", "1.2.1", "3.0.0dev")), - (("google", "cloud", "kms", "v1"), ("google-cloud-kms", "2.3.0, <3.0.0dev")), - (("google", "iam", "v1"), ("grpc-google-iam-v1", "0.12.4", "1.0.0dev")), -]) %} \ No newline at end of file +{% set pypi_packages = { + ("google", "apps", "script", "type"): {"package_name": "google-apps-script-type", "lower_bound": "0.2.0", "upper_bound": "1.0.0dev"}, + ("google", "cloud", "documentai", "v1"): {"package_name": "google-cloud-documentai", "lower_bound": "1.2.1", "upper_bound": "3.0.0dev"}, + ("google", "cloud", "kms", "v1"): {"package_name": "google-cloud-kms", "lower_bound": "2.3.0", "upper_bound": "3.0.0dev"}, + ("google", "iam", "v1"): {"package_name": "grpc-google-iam-v1", "lower_bound": "0.12.4", "upper_bound": "1.0.0dev"} +} +%} \ No newline at end of file diff --git a/gapic/templates/setup.py.j2 b/gapic/templates/setup.py.j2 index 46d64b3001..72b5c44a87 100644 --- a/gapic/templates/setup.py.j2 +++ b/gapic/templates/setup.py.j2 @@ -31,11 +31,11 @@ dependencies = [ "proto-plus >= 1.22.0, <2.0.0dev", "proto-plus >= 1.22.2, <2.0.0dev; python_version>='3.11'", "protobuf>=3.19.5,<5.0.0dev,!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5", - {% for package in pypi_packages %} + {% for package_tuple, package_info in pypi_packages.items() %} {# Quick check to make sure the package is different from this setup.py #} - {% if not api.naming.warehouse_package_name in pypi_packages[package] %} - {% if api.requires_package(package) %} - "{{ pypi_packages[package][0] }} >= {{ pypi_packages[package][1] }}, <{{ pypi_packages[package][2] }}", + {% if api.naming.warehouse_package_name != package_info.package_name %} + {% if api.requires_package(package_tuple) %} + "{{ package_info.package_name }} >= {{ package_info.lower_bound }}, <{{ package_info.upper_bound }}", {% endif %} {% endif %} {% endfor %} diff --git a/gapic/templates/testing/_default_constraints.j2 b/gapic/templates/testing/_default_constraints.j2 index 998169eacf..6a62cfc52d 100644 --- a/gapic/templates/testing/_default_constraints.j2 +++ b/gapic/templates/testing/_default_constraints.j2 @@ -4,10 +4,11 @@ google-api-core proto-plus protobuf -{% for package in pypi_packages %} -{% if not api.naming.warehouse_package_name in pypi_packages[package] %} -{% if api.requires_package(package) %} -{{ pypi_packages[package][0] }} +{% for package_tuple, package_info in pypi_packages.items() %} +{# Quick check to make sure the package is different from this setup.py #} +{% if api.naming.warehouse_package_name != package_info.package_name %} +{% if api.requires_package(package_tuple) %} +{{ package_info.package_name }} {% endif %} {% endif %} {% endfor %} diff --git a/gapic/templates/testing/constraints-3.7.txt.j2 b/gapic/templates/testing/constraints-3.7.txt.j2 index 3b3be987bf..ae6c992c40 100644 --- a/gapic/templates/testing/constraints-3.7.txt.j2 +++ b/gapic/templates/testing/constraints-3.7.txt.j2 @@ -8,10 +8,11 @@ google-api-core==1.34.0 proto-plus==1.22.0 protobuf==3.19.5 -{% for package in pypi_packages %} -{% if not api.naming.warehouse_package_name in pypi_packages[package] %} -{% if api.requires_package(package) %} -{{ pypi_packages[package][0] }}=={{ pypi_packages[package][1] }} +{% for package_tuple, package_info in pypi_packages.items() %} +{# Quick check to make sure the package is different from this setup.py #} +{% if api.naming.warehouse_package_name != package_info.package_name %} +{% if api.requires_package(package_tuple) %} +{{ package_info.package_name }}=={{ package_info.lower_bound }} {% endif %} {% endif %} {% endfor %} From 4b6b7f5d8a640cb722bc7f91861b756ccb18bd9e Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 8 Mar 2023 16:52:52 +0000 Subject: [PATCH 14/22] formatting --- gapic/templates/_pypi_packages.j2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gapic/templates/_pypi_packages.j2 b/gapic/templates/_pypi_packages.j2 index 63d017dabe..97239d2578 100644 --- a/gapic/templates/_pypi_packages.j2 +++ b/gapic/templates/_pypi_packages.j2 @@ -9,4 +9,4 @@ PyPI package name, the minimum allowed version and the maximum allowed version. ("google", "cloud", "kms", "v1"): {"package_name": "google-cloud-kms", "lower_bound": "2.3.0", "upper_bound": "3.0.0dev"}, ("google", "iam", "v1"): {"package_name": "grpc-google-iam-v1", "lower_bound": "0.12.4", "upper_bound": "1.0.0dev"} } -%} \ No newline at end of file +%} From be2dee94e49dc0b14fd20b0276e760648e506054 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 9 Mar 2023 16:54:17 +0000 Subject: [PATCH 15/22] address review feedback --- tests/unit/schema/test_api.py | 68 +++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/tests/unit/schema/test_api.py b/tests/unit/schema/test_api.py index f92e790734..3295f6c419 100644 --- a/tests/unit/schema/test_api.py +++ b/tests/unit/schema/test_api.py @@ -763,6 +763,16 @@ def test_python_modules_nested(): package="google.bah.v20p1alpha3p5", messages=(make_message_pb2(name="ImportedMessageBah", fields=()),), ), + make_file_pb2( + name="bah.v20p1.bai.proto", + package="google.bah.v20p1.bai", + messages=(make_message_pb2(name="ImportedMessageBai", fields=()),), + ), + make_file_pb2( + name="bah.v20p1.baj.v3.proto", + package="google.bah.v20p1.baj.v3", + messages=(make_message_pb2(name="ImportedMessageBaj", fields=()),), + ), make_file_pb2( name="common.proto", package="google.example.v1.common", @@ -874,6 +884,26 @@ def test_python_modules_nested(): ), ), ), + make_message_pb2( + name="Bai", + fields=( + make_field_pb2( + name="imported_message_bai", + number=1, + type_name=".google.bah.v20p1.bai.ImportedMessageBai", + ), + ), + ), + make_message_pb2( + name="Baj", + fields=( + make_field_pb2( + name="imported_message_baj", + number=1, + type_name=".google.bah.v20p1.baj.v3.ImportedMessageBaj", + ), + ), + ), ), ), make_message_pb2( @@ -908,13 +938,17 @@ def test_python_modules_nested(): imp.Import(package=("google", "baa"), module="baa_pb2"), imp.Import(package=("google", "bab", "v1"), module="bab_v1_pb2"), imp.Import(package=("google", "bac", "v10"), module="bac_v10_pb2"), - imp.Import(package=("google", "bad", "v2beta"), - module="bad_v2beta_pb2"), - imp.Import(package=("google", "bae", "v2beta20"), - module="bae_v2beta20_pb2"), - imp.Import(package=("google", "baf", "v20beta"), - module="baf_v20beta_pb2"), + imp.Import(package=("google", "bad", "v2beta"), module="bad_v2beta_pb2"), + imp.Import(package=("google", "bae", "v2beta20"), module="bae_v2beta20_pb2"), + imp.Import(package=("google", "baf", "v20beta"), module="baf_v20beta_pb2"), imp.Import(package=("google", "bag", "v20p1"), module="bag_v20p1_pb2"), + imp.Import( + package=("google", "bah", "v20p1", "bai"), module="bah_v20p1_bai_pb2" + ), + imp.Import( + package=("google", "bah", "v20p1", "baj", "v3"), + module="bah_v20p1_baj_v3_pb2", + ), imp.Import( package=("google", "bah", "v20p1alpha3p5"), module="bah_v20p1alpha3p5_pb2" ), @@ -940,23 +974,27 @@ def test_python_modules_nested(): "google.baf.v20beta", "google.bag.v20p1", "google.bah.v20p1alpha3p5", + "google.bah.v20p1.bai", + "google.bah.v20p1.baj.v3", ) ) ), ) - assert api_schema.protos["foo.proto"].python_modules == ( imp.Import(package=("google", "baa", "types"), module="baa"), imp.Import(package=("google", "bab_v1", "types"), module="bab_v1"), imp.Import(package=("google", "bac_v10", "types"), module="bac_v10"), - imp.Import(package=("google", "bad_v2beta", - "types"), module="bad_v2beta"), - imp.Import(package=("google", "bae_v2beta20", - "types"), module="bae_v2beta20"), - imp.Import(package=("google", "baf_v20beta", - "types"), module="baf_v20beta"), - imp.Import(package=("google", "bag_v20p1", - "types"), module="bag_v20p1"), + imp.Import(package=("google", "bad_v2beta", "types"), module="bad_v2beta"), + imp.Import(package=("google", "bae_v2beta20", "types"), module="bae_v2beta20"), + imp.Import(package=("google", "baf_v20beta", "types"), module="baf_v20beta"), + imp.Import(package=("google", "bag_v20p1", "types"), module="bag_v20p1"), + imp.Import( + package=("google", "bah", "v20p1", "bai", "types"), module="bah_v20p1_bai" + ), + imp.Import( + package=("google", "bah", "v20p1", "baj_v3", "types"), + module="bah_v20p1_baj_v3", + ), imp.Import( package=("google", "bah_v20p1alpha3p5", "types"), module="bah_v20p1alpha3p5" ), From bd3a4daf2005ed5b755f7ab941e169e124585d85 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 9 Mar 2023 18:00:13 +0000 Subject: [PATCH 16/22] style --- tests/unit/schema/test_api.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/unit/schema/test_api.py b/tests/unit/schema/test_api.py index 3295f6c419..d5d3ca4e65 100644 --- a/tests/unit/schema/test_api.py +++ b/tests/unit/schema/test_api.py @@ -938,12 +938,16 @@ def test_python_modules_nested(): imp.Import(package=("google", "baa"), module="baa_pb2"), imp.Import(package=("google", "bab", "v1"), module="bab_v1_pb2"), imp.Import(package=("google", "bac", "v10"), module="bac_v10_pb2"), - imp.Import(package=("google", "bad", "v2beta"), module="bad_v2beta_pb2"), - imp.Import(package=("google", "bae", "v2beta20"), module="bae_v2beta20_pb2"), - imp.Import(package=("google", "baf", "v20beta"), module="baf_v20beta_pb2"), + imp.Import(package=("google", "bad", "v2beta"), + module="bad_v2beta_pb2"), + imp.Import(package=("google", "bae", "v2beta20"), + module="bae_v2beta20_pb2"), + imp.Import(package=("google", "baf", "v20beta"), + module="baf_v20beta_pb2"), imp.Import(package=("google", "bag", "v20p1"), module="bag_v20p1_pb2"), imp.Import( - package=("google", "bah", "v20p1", "bai"), module="bah_v20p1_bai_pb2" + package=("google", "bah", "v20p1", "bai"), + module="bah_v20p1_bai_pb2" ), imp.Import( package=("google", "bah", "v20p1", "baj", "v3"), @@ -984,10 +988,14 @@ def test_python_modules_nested(): imp.Import(package=("google", "baa", "types"), module="baa"), imp.Import(package=("google", "bab_v1", "types"), module="bab_v1"), imp.Import(package=("google", "bac_v10", "types"), module="bac_v10"), - imp.Import(package=("google", "bad_v2beta", "types"), module="bad_v2beta"), - imp.Import(package=("google", "bae_v2beta20", "types"), module="bae_v2beta20"), - imp.Import(package=("google", "baf_v20beta", "types"), module="baf_v20beta"), - imp.Import(package=("google", "bag_v20p1", "types"), module="bag_v20p1"), + imp.Import(package=("google", "bad_v2beta", "types"), + module="bad_v2beta"), + imp.Import(package=("google", "bae_v2beta20", "types"), + module="bae_v2beta20"), + imp.Import(package=("google", "baf_v20beta", "types"), + module="baf_v20beta"), + imp.Import(package=("google", "bag_v20p1", "types"), + module="bag_v20p1"), imp.Import( package=("google", "bah", "v20p1", "bai", "types"), module="bah_v20p1_bai" ), From 7286e8afa798d9c61d1f3ea70737343364a5ccca Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Tue, 14 Mar 2023 11:47:11 +0000 Subject: [PATCH 17/22] add pypi package google-geo-type --- gapic/schema/metadata.py | 1 + gapic/templates/_pypi_packages.j2 | 1 + 2 files changed, 2 insertions(+) diff --git a/gapic/schema/metadata.py b/gapic/schema/metadata.py index 7820017d0e..a724be9345 100644 --- a/gapic/schema/metadata.py +++ b/gapic/schema/metadata.py @@ -212,6 +212,7 @@ def python_import(self) -> imp.Import: return imp.Import( package=self.package + ('types',), module=self.module, + alias=self.module_alias, ) # Return the standard import. diff --git a/gapic/templates/_pypi_packages.j2 b/gapic/templates/_pypi_packages.j2 index 97239d2578..0c84347a09 100644 --- a/gapic/templates/_pypi_packages.j2 +++ b/gapic/templates/_pypi_packages.j2 @@ -5,6 +5,7 @@ PyPI package name, the minimum allowed version and the maximum allowed version. --> {% set pypi_packages = { ("google", "apps", "script", "type"): {"package_name": "google-apps-script-type", "lower_bound": "0.2.0", "upper_bound": "1.0.0dev"}, + ("google", "geo", "type"): {"package_name": "google-geo-type", "lower_bound": "0.1.0", "upper_bound": "1.0.0dev"}, ("google", "cloud", "documentai", "v1"): {"package_name": "google-cloud-documentai", "lower_bound": "1.2.1", "upper_bound": "3.0.0dev"}, ("google", "cloud", "kms", "v1"): {"package_name": "google-cloud-kms", "lower_bound": "2.3.0", "upper_bound": "3.0.0dev"}, ("google", "iam", "v1"): {"package_name": "grpc-google-iam-v1", "lower_bound": "0.12.4", "upper_bound": "1.0.0dev"} From 2dfda6cfe7af5d8cfd80c4b8271c8e1a67476b66 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Tue, 14 Mar 2023 12:52:38 +0000 Subject: [PATCH 18/22] bump google-cloud-documentai dependency to 2.0.0 --- gapic/templates/_pypi_packages.j2 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gapic/templates/_pypi_packages.j2 b/gapic/templates/_pypi_packages.j2 index 0c84347a09..af25d77b3d 100644 --- a/gapic/templates/_pypi_packages.j2 +++ b/gapic/templates/_pypi_packages.j2 @@ -2,11 +2,13 @@ Dict of PyPI packages available by API in the form , Where the first contains the API and the second tuple contains the PyPI package name, the minimum allowed version and the maximum allowed version. + +Note: Set the minimum version for google-cloud-documentai to 2.0.0 which has support for `barcode` in `google.cloud.documentai.types` --> {% set pypi_packages = { ("google", "apps", "script", "type"): {"package_name": "google-apps-script-type", "lower_bound": "0.2.0", "upper_bound": "1.0.0dev"}, ("google", "geo", "type"): {"package_name": "google-geo-type", "lower_bound": "0.1.0", "upper_bound": "1.0.0dev"}, - ("google", "cloud", "documentai", "v1"): {"package_name": "google-cloud-documentai", "lower_bound": "1.2.1", "upper_bound": "3.0.0dev"}, + ("google", "cloud", "documentai", "v1"): {"package_name": "google-cloud-documentai", "lower_bound": "2.0.0", "upper_bound": "3.0.0dev"}, ("google", "cloud", "kms", "v1"): {"package_name": "google-cloud-kms", "lower_bound": "2.3.0", "upper_bound": "3.0.0dev"}, ("google", "iam", "v1"): {"package_name": "grpc-google-iam-v1", "lower_bound": "0.12.4", "upper_bound": "1.0.0dev"} } From 99a1ac8da58713d758b508f5980f7a940d228773 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Fri, 17 Mar 2023 10:24:02 +0000 Subject: [PATCH 19/22] address review comment --- gapic/schema/metadata.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/gapic/schema/metadata.py b/gapic/schema/metadata.py index a724be9345..5da782ce72 100644 --- a/gapic/schema/metadata.py +++ b/gapic/schema/metadata.py @@ -103,12 +103,10 @@ def __str__(self) -> str: @property def is_proto_plus_type(self): - if self.proto_package.startswith(self.api_naming.proto_package) or ( + return self.proto_package.startswith(self.api_naming.proto_package) or ( hasattr(self.api_naming, "proto_plus_deps") and self.proto_package in self.api_naming.proto_plus_deps - ): - return True - return False + ) @cached_property def __cached_string_repr(self): From 47a2067690019bde6e17e0025ced16325ec72692 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Fri, 17 Mar 2023 10:33:58 +0000 Subject: [PATCH 20/22] add docstring --- gapic/schema/metadata.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/gapic/schema/metadata.py b/gapic/schema/metadata.py index 5da782ce72..1cd1bd5fc7 100644 --- a/gapic/schema/metadata.py +++ b/gapic/schema/metadata.py @@ -102,7 +102,19 @@ def __str__(self) -> str: return '.'.join(self.parent + (self.name,)) @property - def is_proto_plus_type(self): + def is_proto_plus_type(self) -> bool: + """This function is used to determine whether a given package `self.proto_package` + is using proto-plus types or protobuf types. There are 2 scenarios where the package + is expected to use proto-plus types. + 1) When `self.proto_package` starts with `self.api_naming.proto_package`, then + the given package has the same namespace as the one that is being generated. It is assumed + that the gapic generator always generates packages with proto-plus types. + 2) When `self.proto_package` is explicitly in `self.api_naming.proto_plus_deps` which is + populated via the generator option `proto-plus-deps`. + + Returns: + bool: Whether the given package uses proto-plus types or not. + """ return self.proto_package.startswith(self.api_naming.proto_package) or ( hasattr(self.api_naming, "proto_plus_deps") and self.proto_package in self.api_naming.proto_plus_deps From 3f95c0af570cd68db0dc634f79a5760573d3f406 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Fri, 17 Mar 2023 10:38:12 +0000 Subject: [PATCH 21/22] address review comment --- gapic/schema/metadata.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gapic/schema/metadata.py b/gapic/schema/metadata.py index 1cd1bd5fc7..9401924b45 100644 --- a/gapic/schema/metadata.py +++ b/gapic/schema/metadata.py @@ -210,7 +210,7 @@ def python_import(self) -> imp.Import: version_regex = "^v\d[^/]*$" regex_match = re.match(version_regex, self.package[-1]) - if regex_match: + if regex_match and len(self.package) > 1: versioned_module = f"{self.package[-2]}_{regex_match[0]}" return imp.Import( package=self.package[:-2] + From dfe77d76f0ae1185b784836ce7a64c7a31a0380a Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Fri, 17 Mar 2023 10:40:20 +0000 Subject: [PATCH 22/22] fix docs --- gapic/schema/metadata.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gapic/schema/metadata.py b/gapic/schema/metadata.py index 9401924b45..f4ec88e7cb 100644 --- a/gapic/schema/metadata.py +++ b/gapic/schema/metadata.py @@ -105,11 +105,11 @@ def __str__(self) -> str: def is_proto_plus_type(self) -> bool: """This function is used to determine whether a given package `self.proto_package` is using proto-plus types or protobuf types. There are 2 scenarios where the package - is expected to use proto-plus types. - 1) When `self.proto_package` starts with `self.api_naming.proto_package`, then + is expected to use proto-plus types: + 1) When `self.proto_package` starts with `self.api_naming.proto_package`, then the given package has the same namespace as the one that is being generated. It is assumed that the gapic generator always generates packages with proto-plus types. - 2) When `self.proto_package` is explicitly in `self.api_naming.proto_plus_deps` which is + 2) When `self.proto_package` is explicitly in `self.api_naming.proto_plus_deps` which is populated via the generator option `proto-plus-deps`. Returns: