File tree Expand file tree Collapse file tree 4 files changed +43
-0
lines changed Expand file tree Collapse file tree 4 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -275,8 +275,21 @@ def input_filenames(self) -> Iterable[str]:
275
275
@property
276
276
def python_module_imports (self ) -> Set [str ]:
277
277
imports = set ()
278
+
279
+ has_deprecated = False
280
+ if any (m .deprecated for m in self .messages ):
281
+ has_deprecated = True
278
282
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 :
279
291
imports .add ("warnings" )
292
+
280
293
if self .builtins_import :
281
294
imports .add ("builtins" )
282
295
return imports
Original file line number Diff line number Diff line change @@ -84,6 +84,10 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub):
84
84
{% if method .comment %}
85
85
{{ method.comment }}
86
86
87
+ {% endif %}
88
+ {% if method .proto_obj .options .deprecated %}
89
+ warnings.warn("{{ service.py_name }}.{{ method.py_name }} is deprecated", DeprecationWarning)
90
+
87
91
{% endif %}
88
92
{% if method .server_streaming %}
89
93
{% if method .client_streaming %}
Original file line number Diff line number Diff line change @@ -12,3 +12,10 @@ message Message {
12
12
option deprecated = true ;
13
13
string value = 1 ;
14
14
}
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
+ }
Original file line number Diff line number Diff line change 2
2
3
3
import pytest
4
4
5
+ from tests .mocks import MockChannel
5
6
from tests .output_betterproto .deprecated import (
7
+ Empty ,
6
8
Message ,
7
9
Test ,
10
+ TestServiceStub ,
8
11
)
9
12
10
13
@@ -43,3 +46,19 @@ def test_message_with_deprecated_field_not_set_default(message):
43
46
_ = Test (value = 10 ).message
44
47
45
48
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
You can’t perform that action at this time.
0 commit comments