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]:
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
Original file line number Diff line number Diff 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 %}
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff line change 22
33import pytest
44
5+ from tests .mocks import MockChannel
56from 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
You can’t perform that action at this time.
0 commit comments