Skip to content

Commit 399f24f

Browse files
authored
chore: add support for Python 3.11 (#1533)
1 parent baa6ec8 commit 399f24f

File tree

6 files changed

+51
-5
lines changed

6 files changed

+51
-5
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ jobs:
8383
- os: macos-11.0
8484
python-version: '3.10'
8585
browser: chromium
86+
- os: windows-latest
87+
python-version: '3.11.0-rc.1'
88+
browser: chromium
89+
- os: macos-latest
90+
python-version: '3.11.0-rc.1'
91+
browser: chromium
92+
- os: ubuntu-latest
93+
python-version: '3.11.0-rc.1'
94+
browser: chromium
8695
runs-on: ${{ matrix.os }}
8796
steps:
8897
- uses: actions/checkout@v2

meta.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ requirements:
2323
- setuptools_scm
2424
run:
2525
- python
26-
- greenlet ==1.1.2
26+
- greenlet ==1.1.3
2727
- pyee ==8.1.0
2828
- websockets ==10.1
2929
- typing_extensions # [py<39]

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 value is None and 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,

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def _download_and_extract_local_driver(
209209
include_package_data=True,
210210
install_requires=[
211211
"websockets==10.1",
212-
"greenlet==1.1.2",
212+
"greenlet==1.1.3",
213213
"pyee==8.1.0",
214214
"typing-extensions;python_version<='3.8'",
215215
],
@@ -222,6 +222,7 @@ def _download_and_extract_local_driver(
222222
"Programming Language :: Python :: 3.8",
223223
"Programming Language :: Python :: 3.9",
224224
"Programming Language :: Python :: 3.10",
225+
"Programming Language :: Python :: 3.11",
225226
"License :: OSI Approved :: Apache Software License",
226227
"Operating System :: OS Independent",
227228
],

0 commit comments

Comments
 (0)