diff --git a/src/betterproto/plugin/models.py b/src/betterproto/plugin/models.py index cf14cadb4..03834a7ee 100644 --- a/src/betterproto/plugin/models.py +++ b/src/betterproto/plugin/models.py @@ -275,8 +275,21 @@ def input_filenames(self) -> Iterable[str]: @property def python_module_imports(self) -> Set[str]: imports = set() + + has_deprecated = False + if any(m.deprecated for m in self.messages): + has_deprecated = True if any(x for x in self.messages if any(x.deprecated_fields)): + has_deprecated = True + if any( + any(m.proto_obj.options.deprecated for m in s.methods) + for s in self.services + ): + has_deprecated = True + + if has_deprecated: imports.add("warnings") + if self.builtins_import: imports.add("builtins") return imports diff --git a/src/betterproto/templates/template.py.j2 b/src/betterproto/templates/template.py.j2 index 24c927cfc..c10793de2 100644 --- a/src/betterproto/templates/template.py.j2 +++ b/src/betterproto/templates/template.py.j2 @@ -84,6 +84,10 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub): {% if method.comment %} {{ method.comment }} + {% endif %} + {% if method.proto_obj.options.deprecated %} + warnings.warn("{{ service.py_name }}.{{ method.py_name }} is deprecated", DeprecationWarning) + {% endif %} {% if method.server_streaming %} {% if method.client_streaming %} diff --git a/tests/inputs/deprecated/deprecated.proto b/tests/inputs/deprecated/deprecated.proto index 81d69c0a9..f504d03af 100644 --- a/tests/inputs/deprecated/deprecated.proto +++ b/tests/inputs/deprecated/deprecated.proto @@ -12,3 +12,10 @@ message Message { option deprecated = true; string value = 1; } + +message Empty {} + +service TestService { + rpc func(Empty) returns (Empty); + rpc deprecated_func(Empty) returns (Empty) { option deprecated = true; }; +} diff --git a/tests/test_deprecated.py b/tests/test_deprecated.py index 84e08bd4c..c1caead3a 100644 --- a/tests/test_deprecated.py +++ b/tests/test_deprecated.py @@ -2,9 +2,12 @@ import pytest +from tests.mocks import MockChannel from tests.output_betterproto.deprecated import ( + Empty, Message, Test, + TestServiceStub, ) @@ -43,3 +46,19 @@ def test_message_with_deprecated_field_not_set_default(message): _ = Test(value=10).message assert not record + + +@pytest.mark.asyncio +async def test_service_with_deprecated_method(): + stub = TestServiceStub(MockChannel([Empty(), Empty()])) + + with pytest.warns(DeprecationWarning) as record: + await stub.deprecated_func(Empty()) + + assert len(record) == 1 + assert str(record[0].message) == f"TestService.deprecated_func is deprecated" + + with pytest.warns(None) as record: + await stub.func(Empty()) + + assert not record