14
14
# limitations under the License.
15
15
16
16
import inspect
17
- import re
18
17
from types import FunctionType
19
- from typing import Any , Optional , get_type_hints
18
+ from typing import Any , Callable , Dict , List , get_type_hints
20
19
21
20
from playwright ._impl ._helper import to_snake_case
22
21
from scripts .documentation_provider import DocumentationProvider
23
22
from scripts .generate_api import (
24
- Overload ,
25
23
all_types ,
26
24
api_globals ,
27
25
arguments ,
28
26
header ,
29
- is_overload ,
30
27
process_type ,
31
28
return_type ,
29
+ return_type_value ,
32
30
return_value ,
33
31
short_name ,
34
32
signature ,
37
35
documentation_provider = DocumentationProvider (True )
38
36
39
37
38
+ def async_return_type_value (func : Callable ) -> str :
39
+ return return_type_value (func ).replace (
40
+ "EventContextManager" , "AsyncEventContextManager"
41
+ )
42
+
43
+
40
44
def generate (t : Any ) -> None :
41
45
print ("" )
42
46
class_name = short_name (t )
@@ -59,6 +63,9 @@ def generate(t: Any) -> None:
59
63
[prefix , suffix ] = return_value (type )
60
64
prefix = " return " + prefix + f"self._impl_obj.{ name } "
61
65
print (f"{ prefix } { suffix } " )
66
+ all_overloads : Dict [str , List [Callable ]] = (
67
+ inspect .getmodule (t ).__dict__ .get ("__overloads__" ) or {}
68
+ )
62
69
for [name , value ] in t .__dict__ .items ():
63
70
if name .startswith ("_" ):
64
71
continue
@@ -78,55 +85,48 @@ def generate(t: Any) -> None:
78
85
prefix = " return " + prefix + f"self._impl_obj.{ name } "
79
86
print (f"{ prefix } { arguments (value , len (prefix ))} { suffix } " )
80
87
for [name , value ] in t .__dict__ .items ():
81
- overload : Optional [Overload ] = None
82
- if is_overload (name ):
83
- overload = Overload (t , name )
84
- overload .assert_has_implementation ()
85
- name = overload .name
88
+ overloads = all_overloads .get (name ) or []
86
89
if (
87
90
not name .startswith ("_" )
88
91
and isinstance (value , FunctionType )
89
92
and "remove_listener" != name
90
93
):
91
94
is_async = inspect .iscoroutinefunction (value )
92
- return_type_value = return_type (value )
93
- return_type_value = re .sub (r"\"([^\"]+)Impl\"" , r"\1" , return_type_value )
94
- return_type_value = return_type_value .replace (
95
- "EventContextManager" , "AsyncEventContextManager"
96
- )
97
- print ("" )
98
95
async_prefix = "async " if is_async else ""
99
- if overload is not None :
96
+ indent = len (name ) + 9
97
+ for overload in overloads :
100
98
print (" @typing.overload" )
99
+ print (
100
+ f" { async_prefix } def { name } ({ signature (overload , indent , True )} ) -> { async_return_type_value (overload )} :"
101
+ )
102
+ print (" pass" )
103
+ print ("" )
101
104
print (
102
- f" { async_prefix } def { name } ({ signature (value , len ( name ) + 9 , overload is not None ) } ) -> { return_type_value } :"
105
+ f" { async_prefix } def { name } ({ signature (value , indent ) } ) -> { async_return_type_value ( value ) } :"
103
106
)
104
- if overload is None :
105
- documentation_provider .print_entry (
106
- class_name , name , get_type_hints (value , api_globals )
107
+ documentation_provider .print_entry (
108
+ class_name , name , get_type_hints (value , api_globals )
109
+ )
110
+ if "expect_" in name :
111
+ print ("" )
112
+ print (
113
+ f" return AsyncEventContextManager(self._impl_obj.{ name } ({ arguments (value , 12 )} ).future)"
107
114
)
108
- if "expect_" in name :
109
- print ("" )
110
- print (
111
- f" return AsyncEventContextManager(self._impl_obj.{ name } ({ arguments (value , 12 )} ).future)"
112
- )
113
- else :
114
- [prefix , suffix ] = return_value (
115
- get_type_hints (value , api_globals )["return" ]
115
+ else :
116
+ [prefix , suffix ] = return_value (
117
+ get_type_hints (value , api_globals )["return" ]
118
+ )
119
+ if is_async :
120
+ prefix += (
121
+ f'await self._async("{ to_snake_case (class_name )} .{ name } ", '
116
122
)
117
- if is_async :
118
- prefix += (
119
- f'await self._async("{ to_snake_case (class_name )} .{ name } ", '
120
- )
121
- suffix += ")"
122
- prefix = prefix + f"self._impl_obj.{ name } ("
123
- suffix = ")" + suffix
124
- print (
125
- f"""
123
+ suffix += ")"
124
+ prefix = prefix + f"self._impl_obj.{ name } ("
125
+ suffix = ")" + suffix
126
+ print (
127
+ f"""
126
128
return { prefix } { arguments (value , len (prefix ))} { suffix } """
127
- )
128
- else :
129
- print (" pass" )
129
+ )
130
130
if class_name == "Playwright" :
131
131
print (
132
132
"""
0 commit comments