8000 Add warnings when calling deprecated method (#596) · AdrienVannson/python-betterproto@4e9a17c · GitHub
[go: up one dir, main page]

Skip to content

Commit 4e9a17c

Browse files
AdrienVannsonAdrien Vannson
andauthored
Add warnings when calling deprecated method (danielgtaylor#596)
* Add test * To run the workflow * Fix import * Format * Add warning * Fix indentation * Test deprecated method * More test * Format * Add import if needed --------- Co-authored-by: Adrien Vannson <adrien.vannson@gardacp.com>
1 parent f96f516 commit 4e9a17c

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

src/betterproto/plugin/models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,21 @@ def input_filenames(self) -> Iterable[str]:
275275
@property
276276
def python_module_imports(self) -> Set[str]:
277277
imports = set()
278+
279+
has_deprecated = False
280+
if any(m.deprecated for m in self.messages):
281+
has_deprecated = True
278282
if any(x for x in self.messages if any(x.deprecated_fields)):
283+
has_deprecated = True
284+
if any(
285+
any(m.proto_obj.options.deprecated for m in s.methods)
286+
for s in self.services
287+
):
288+
has_deprecated = True
289+
290+
if has_deprecated:
279291
imports.add("warnings")
292+
280293
if self.builtins_import:
281294
imports.add("builtins")
282295
return imports

src/betterproto/templates/template.py.j2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub):
8484
{% if method.comment %}
8585
{{ method.comment }}
8686

87+
{% endif %}
88+
{% if method.proto_obj.options.deprecated %}
89+
warnings.warn("{{ service.py_name }}.{{ method.py_name }} is deprecated", DeprecationWarning)
90+
8791
{% endif %}
8892
{% if method.server_streaming %}
8993
{% if method.client_streaming %}

tests/inputs/deprecated/deprecated.proto

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ message Message {
1212
option deprecated = true;
1313
string value = 1;
1414
}
15+
16+
message Empty {}
17+
18+
service TestService {
19+
rpc func(Empty) returns (Empty);
20+
rpc deprecated_func(Empty) returns (Empty) { option deprecated = true; };
21+
}

tests/test_deprecated.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import pytest
44

5+
from tests.mocks import MockChannel
56
from tests.output_betterproto.deprecated import (
7+
Empty,
68
Message,
79
Test,
10+
TestServiceStub,
811
)
912

1013

@@ -43,3 +46,19 @@ def test_message_with_deprecated_field_not_set_default(message):
4346
_ = Test(value=10).message
4447

4548
assert not record
49+
50+
51+
@pytest.mark.asyncio
52+
async def test_service_with_deprecated_method():
53+
stub = TestServiceStub(MockChannel([Empty(), Empty()]))
54+
55+
with pytest.warns(DeprecationWarning) as record:
56+
await stub.deprecated_func(Empty())
57+
58+
assert len(record) == 1
59+
assert str(record[0].message) == f"TestService.deprecated_func is deprecated"
60+
61+
with pytest.warns(None) as record:
62+
await stub.func(Empty())
63+
64+
assert not record

0 commit comments

Comments
 (0)
0