Skip to content

Commit 1cf40eb

Browse files
committed
fix it
1 parent 871a3fb commit 1cf40eb

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

scripts/generate_api.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@
1313
# limitations under the License.
1414

1515
import re
16+
import sys
1617
from types import FunctionType
1718
from typing import ( # type: ignore
1819
Any,
20+
Dict,
1921
List,
2022
Match,
23+
Optional,
2124
Union,
2225
cast,
2326
get_args,
2427
get_origin,
25-
get_type_hints,
2628
)
29+
from typing import get_type_hints as typing_get_type_hints
2730

2831
from playwright._impl._accessibility import Accessibility
2932
from playwright._impl._assertions import (
@@ -287,3 +290,34 @@ def return_value(value: Any) -> List[str]:
287290

288291
api_globals = globals()
289292
assert Serializable
293+
294+
# Python 3.11+ does not treat default args with None as Optional anymore, this wrapper will still wrap them.
295+
# https://github.com/python/cpython/issues/90353
296+
def get_type_hints(func: Any, globalns: Any) -> Dict[str, Any]:
297+
original_value = typing_get_type_hints(func, globalns)
298+
if sys.version_info < (3, 11):
299+
return original_value
300+
for key, value in _get_defaults(func).items():
301+
if original_value[key] is not Optional:
302+
original_value[key] = Optional[original_value[key]]
303+
return original_value
304+
305+
306+
def _get_defaults(func: Any) -> Dict[str, Any]:
307+
"""Internal helper to extract the default arguments, by name."""
308+
try:
309+
code = func.__code__
310+
except AttributeError:
311+
# Some built-in functions don't have __code__, __defaults__, etc.
312+
return {}
313+
pos_count = code.co_argcount
314+
arg_names = code.co_varnames
315+
arg_names = arg_names[:pos_count]
316+
defaults = func.__defaults__ or ()
317+
kwdefaults = func.__kwdefaults__
318+
res = dict(kwdefaults) if kwdefaults else {}
319+
pos_offset = pos_count - len(defaults)
320+
for name, value in zip(arg_names[pos_offset:], defaults):
321+
assert name not in res
322+
res[name] = value
323+
return res

scripts/generate_async_api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
import inspect
1717
import re
1818
from types import FunctionType
19-
from typing import Any, get_type_hints
19+
from typing import Any
2020

2121
from scripts.documentation_provider import DocumentationProvider
2222
from scripts.generate_api import (
2323
all_types,
2424
api_globals,
2525
arguments,
26+
get_type_hints,
2627
header,
2728
process_type,
2829
return_type,

scripts/generate_sync_api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
import re
1818
import sys
1919
from types import FunctionType
20-
from typing import Any, get_type_hints
20+
from typing import Any
2121

2222
from scripts.documentation_provider import DocumentationProvider
2323
from scripts.generate_api import (
2424
all_types,
2525
api_globals,
2626
arguments,
27+
get_type_hints,
2728
header,
2829
process_type,
2930
return_type,

0 commit comments

Comments
 (0)