Skip to content

Commit

Permalink
fix it
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Aug 25, 2022
1 parent 871a3fb commit 1cf40eb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
36 changes: 35 additions & 1 deletion scripts/generate_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@
# limitations under the License.

import re
import sys
from types import FunctionType
from typing import ( # type: ignore
Any,
Dict,
List,
Match,
Optional,
Union,
cast,
get_args,
get_origin,
get_type_hints,
)
from typing import get_type_hints as typing_get_type_hints

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

api_globals = globals()
assert Serializable

# Python 3.11+ does not treat default args with None as Optional anymore, this wrapper will still wrap them.
# https://github.com/python/cpython/issues/90353
def get_type_hints(func: Any, globalns: Any) -> Dict[str, Any]:
original_value = typing_get_type_hints(func, globalns)
if sys.version_info < (3, 11):
return original_value
for key, value in _get_defaults(func).items():
if original_value[key] is not Optional:
original_value[key] = Optional[original_value[key]]
return original_value


def _get_defaults(func: Any) -> Dict[str, Any]:
"""Internal helper to extract the default arguments, by name."""
try:
code = func.__code__
except AttributeError:
# Some built-in functions don't have __code__, __defaults__, etc.
return {}
pos_count = code.co_argcount
arg_names = code.co_varnames
arg_names = arg_names[:pos_count]
defaults = func.__defaults__ or ()
kwdefaults = func.__kwdefaults__
res = dict(kwdefaults) if kwdefaults else {}
pos_offset = pos_count - len(defaults)
for name, value in zip(arg_names[pos_offset:], defaults):
assert name not in res
res[name] = value
return res
3 changes: 2 additions & 1 deletion scripts/generate_async_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
import inspect
import re
from types import FunctionType
from typing import Any, get_type_hints
from typing import Any

from scripts.documentation_provider import DocumentationProvider
from scripts.generate_api import (
all_types,
api_globals,
arguments,
get_type_hints,
header,
process_type,
return_type,
Expand Down
3 changes: 2 additions & 1 deletion scripts/generate_sync_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
import re
import sys
from types import FunctionType
from typing import Any, get_type_hints
from typing import Any

from scripts.documentation_provider import DocumentationProvider
from scripts.generate_api import (
all_types,
api_globals,
arguments,
get_type_hints,
header,
process_type,
return_type,
Expand Down

0 comments on commit 1cf40eb

Please sign in to comment.