1
1
import asyncio
2
-
2
+ import inspect
3
3
from pywa import WhatsApp as WhatsAppSync
4
4
from pywa_async import WhatsApp as WhatsAppAsync
5
+ from pywa .api import WhatsAppCloudApi as WhatsAppCloudApiSync
6
+ from pywa_async .api import WhatsAppCloudApiAsync
7
+ from pywa .types import (
8
+ Message as MessageSync ,
9
+ CallbackButton as CallbackButtonSync ,
10
+ CallbackSelection as CallbackSelectionSync ,
11
+ MessageStatus as MessageStatusSync ,
12
+ ChatOpened as ChatOpenedSync ,
13
+ FlowCompletion as FlowCompletionSync ,
14
+ TemplateStatus as TemplateStatusSync ,
15
+ FlowRequest as FlowRequestSync ,
16
+ MediaUrlResponse as MediaUrlResponseSync ,
17
+ )
18
+ from pywa_async .types import (
19
+ Message as MessageAsync ,
20
+ CallbackButton as CallbackButtonAsync ,
21
+ CallbackSelection as CallbackSelectionAsync ,
22
+ MessageStatus as MessageStatusAsync ,
23
+ ChatOpened as ChatOpenedAsync ,
24
+ FlowCompletion as FlowCompletionAsync ,
25
+ TemplateStatus as TemplateStatusAsync ,
26
+ FlowRequest as FlowRequestAsync ,
27
+ MediaUrlResponse as MediaUrlResponseAsync ,
28
+ )
29
+ from pywa .types .flows import FlowDetails as FlowDetailsSync
30
+ from pywa_async .types .flows import FlowDetails as FlowDetailsAsync
31
+ from pywa .types .media import BaseMedia as BaseMediaSync
32
+ from pywa_async .types .media import BaseMediaAsync
33
+ from pywa .types .sent_message import SentMessage as SentMessageSync
34
+ from pywa_async .types .sent_message import SentMessage as SentMessageAsync
5
35
6
36
7
- def test_all_methods_overwritten_in_async ():
37
+ def test_all_methods_are_overwritten_in_async ():
38
+ objs : list [tuple ] = [
39
+ (WhatsAppSync , WhatsAppAsync ),
40
+ (MessageSync , MessageAsync ),
41
+ (CallbackButtonSync , CallbackButtonAsync ),
42
+ (CallbackSelectionSync , CallbackSelectionAsync ),
43
+ (MessageStatusSync , MessageStatusAsync ),
44
+ (ChatOpenedSync , ChatOpenedAsync ),
45
+ (FlowCompletionSync , FlowCompletionAsync ),
46
+ (TemplateStatusSync , TemplateStatusAsync ),
47
+ (FlowRequestSync , FlowRequestAsync ),
48
+ (FlowDetailsSync , FlowDetailsAsync ),
49
+ (MediaUrlResponseSync , MediaUrlResponseAsync ),
50
+ (BaseMediaSync , BaseMediaAsync ),
51
+ (SentMessageSync , SentMessageAsync ),
52
+ (WhatsAppCloudApiSync , WhatsAppCloudApiAsync ),
53
+ ]
8
54
skip_methods = {
9
- "api" ,
10
- "token" ,
11
55
"add_handlers" ,
12
56
"remove_handlers" ,
13
57
"remove_callbacks" ,
@@ -22,14 +66,12 @@ def test_all_methods_overwritten_in_async():
22
66
"on_raw_update" ,
23
67
"on_flow_request" ,
24
68
"stop_listening" ,
25
- "_async_allowed" ,
26
69
"_check_for_async_callback" ,
27
70
"_after_handling_update" ,
28
71
"_check_for_async_filters" ,
29
72
"_check_and_prepare_update" ,
30
73
"_check_for_async_func" ,
31
74
"_get_handler" ,
32
- "_handlers_to_update_constractor" ,
33
75
"_api_cls" ,
34
76
"_flow_req_cls" ,
35
77
"_httpx_client" ,
@@ -42,9 +84,52 @@ def test_all_methods_overwritten_in_async():
42
84
"_remove_listener" ,
43
85
"get_flow_request_handler" ,
44
86
"load_handlers_modules" ,
87
+ "continue_handling" ,
88
+ "stop_handling" ,
89
+ "from_update" ,
90
+ "from_dict" ,
91
+ "from_flow_completion" ,
92
+ "from_sent_update" ,
93
+ "get_media" ,
94
+ "respond" ,
95
+ "TemplateEvent" ,
96
+ "TemplateRejectionReason" ,
97
+ }
98
+ skip_signature_check = {
99
+ "upload_media" ,
100
+ "webhook_challenge_handler" ,
101
+ "webhook_update_handler" ,
102
+ "decrypt_media" ,
45
103
}
46
- for method in dir (WhatsAppSync ):
47
- if method .startswith ("__" ) or method in skip_methods :
48
- continue
49
- if not asyncio .iscoroutinefunction (getattr (WhatsAppAsync , method )):
50
- raise AssertionError (f"Method { method } is not overwritten in WhatsAppAsync" )
104
+ for sync_obj , async_obj in objs :
105
+ for method_name in filter (
106
+ lambda a : callable (getattr (sync_obj , a ))
107
+ and not a .startswith ("__" )
108
+ and a not in skip_methods ,
109
+ dir (sync_obj ),
110
+ ):
111
+ sync_method , async_method = (
112
+ getattr (sync_obj , method_name ),
113
+ getattr (async_obj , method_name ),
114
+ )
115
+ if not asyncio .iscoroutinefunction (async_method ):
116
+ raise AssertionError (
117
+ f"Method { method_name } is not overwritten in { async_obj .__name__ } "
118
+ )
119
+ if method_name not in skip_signature_check :
120
+ sync_sig , async_sig = (
121
+ inspect .signature (sync_method ),
122
+ inspect .signature (async_method ),
123
+ )
124
+ if sync_sig .parameters != async_sig .parameters :
125
+ raise AssertionError (
126
+ f"Method { method_name } has different signatures in { async_obj .__name__ } \n "
127
+ f"S: { sync_sig .parameters } \n "
128
+ f"A: { async_sig .parameters } "
129
+ )
130
+ if sync_sig .return_annotation != async_sig .return_annotation :
131
+ raise AssertionError (
132
+ f"Method { method_name } has different return annotations in { async_obj .__name__ } \n "
133
+ f"S: { sync_sig .return_annotation } \n "
134
+ f"A: { async_sig .return_annotation } "
135
+ )
0 commit comments