Skip to content

Commit

Permalink
feat: support protobuf method deprecation option [gapic-generator-pyt…
Browse files Browse the repository at this point in the history
…hon] (#875)
  • Loading branch information
miraleung authored May 13, 2021
1 parent 35faac9 commit 5a5a839
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
{{ method.client_output.meta.doc|rst(width=72, indent=16, source_format='rst') }}
{% endif %}
"""
{% if method.is_deprecated %}
warnings.warn("{{ method.name|snake_case }} is deprecated", warnings.DeprecationWarning)
{% endif %}
{% if not method.client_streaming %}
# Create or coerce a protobuf request object.
{% if method.flattened_fields %}
Expand Down
6 changes: 6 additions & 0 deletions gapic/schema/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,14 @@ def _client_output(self, enable_asyncio: bool):
# Return the usual output.
return self.output

@property
def is_deprecated(self) -> bool:
"""Returns true if the method is deprecated, false otherwise."""
return descriptor_pb2.MethodOptions.HasField(self.options, 'deprecated')

# TODO(yon-mg): remove or rewrite: don't think it performs as intended
# e.g. doesn't work with basic case of gRPC transcoding

@property
def field_headers(self) -> Sequence[str]:
"""Return the field headers defined for this method."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
{{ method.client_output.meta.doc|rst(width=72, indent=16, source_format="rst") }}
{% endif %}
"""
{% if method.is_deprecated %}
warnings.warn("{{ method.name|snake_case }} is deprecated", warnings.DeprecationWarning)
{% endif %}
{% if not method.client_streaming %}
# Create or coerce a protobuf request object.
{% if method.flattened_fields %}
Expand Down Expand Up @@ -476,9 +479,9 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
metadata: Sequence[Tuple[str, str]] = (),
) -> policy_pb2.Policy:
r"""Sets the IAM access control policy on the specified function.

Replaces any existing policy.

Args:
request (:class:`~.iam_policy_pb2.SetIamPolicyRequest`):
The request object. Request message for `SetIamPolicy`
Expand Down
4 changes: 4 additions & 0 deletions test_utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def make_method(
module: str = 'baz',
http_rule: http_pb2.HttpRule = None,
signatures: typing.Sequence[str] = (),
is_deprecated: bool = False,
**kwargs
) -> wrappers.Method:
# Use default input and output messages if they are not provided.
Expand Down Expand Up @@ -189,6 +190,9 @@ def make_method(
if isinstance(package, str):
package = tuple(package.split('.'))

if is_deprecated:
method_pb.options.deprecated = True

# Instantiate the wrapper class.
return wrappers.Method(
method_pb=method_pb,
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/schema/wrappers/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ def test_method_not_void():
assert not method.void


def test_method_deprecated():
method = make_method('DeprecatedMethod', is_deprecated=True)
assert method.is_deprecated


def test_method_client_output():
output = make_message(name='Input', module='baz')
method = make_method('DoStuff', output_message=output)
Expand Down

0 comments on commit 5a5a839

Please sign in to comment.