Skip to content

Add warnings when calling deprecated method #596

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/betterproto/plugin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/betterproto/templates/template.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
7 changes: 7 additions & 0 deletions tests/inputs/deprecated/deprecated.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
}
19 changes: 19 additions & 0 deletions tests/test_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import pytest

from tests.mocks import MockChannel
from tests.output_betterproto.deprecated import (
Empty,
Message,
Test,
TestServiceStub,
)


Expand Down Expand Up @@ -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
Loading