Skip to content

Commit e19680e

Browse files
authored
[py] Improve type hints with union syntax and native types (#16590)
1 parent e535ce2 commit e19680e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+419
-458
lines changed

py/generate.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
# This is a copy of https://github.com/HyperionGray/python-chrome-devtools-protocol/blob/master/generator/generate.py
2424
# The license above is theirs and MUST be preserved.
2525

26-
# flake8: noqa
2726

2827
import builtins
2928
from dataclasses import dataclass
@@ -36,7 +35,9 @@
3635
from pathlib import Path
3736
import re
3837
from textwrap import dedent, indent as tw_indent
39-
from typing import Optional , cast, List, Union, Iterator
38+
from typing import Optional, cast, List, Union
39+
40+
from collections.abc import Iterator
4041

4142
import inflection # type: ignore
4243

@@ -206,11 +207,11 @@ def from_json(cls, type):
206207
class CdpProperty:
207208
''' A property belonging to a non-primitive CDP type. '''
208209
name: str
209-
description: Optional[str]
210-
type: Optional[str]
211-
ref: Optional[str]
212-
enum: List[str]
213-
items: Optional[CdpItems]
210+
description: str | None
211+
type: str | None
212+
ref: str | None
213+
enum: list[str]
214+
items: CdpItems | None
214215
optional: bool
215216
experimental: bool
216217
deprecated: bool
@@ -316,11 +317,11 @@ def generate_from_json(self, dict_):
316317
class CdpType:
317318
''' A top-level CDP type. '''
318319
id: str
319-
description: Optional[str]
320+
description: str | None
320321
type: str
321-
items: Optional[CdpItems]
322-
enum: List[str]
323-
properties: List[CdpProperty]
322+
items: CdpItems | None
323+
enum: list[str]
324+
properties: list[CdpProperty]
324325

325326
@classmethod
326327
def from_json(cls, type_):
@@ -585,8 +586,8 @@ class CdpCommand:
585586
description: str
586587
experimental: bool
587588
deprecated: bool
588-
parameters: List[CdpParameter]
589-
returns: List[CdpReturn]
589+
parameters: list[CdpParameter]
590+
returns: list[CdpReturn]
590591
domain: str
591592

592593
@property
@@ -712,10 +713,10 @@ def get_refs(self):
712713
class CdpEvent:
713714
''' A CDP event object. '''
714715
name: str
715-
description: Optional[str]
716+
description: str | None
716717
deprecated: bool
717718
experimental: bool
718-
parameters: List[CdpParameter]
719+
parameters: list[CdpParameter]
719720
domain: str
720721

721722
@property
@@ -786,12 +787,12 @@ def get_refs(self):
786787
class CdpDomain:
787788
''' A CDP domain contains metadata, types, commands, and events. '''
788789
domain: str
789-
description: Optional[str]
790+
description: str | None
790791
experimental: bool
791-
dependencies: List[str]
792-
types: List[CdpType]
793-
commands: List[CdpCommand]
794-
events: List[CdpEvent]
792+
dependencies: list[str]
793+
types: list[CdpType]
794+
commands: list[CdpCommand]
795+
events: list[CdpEvent]
795796

796797
@property
797798
def module(self) -> str:

py/selenium/common/exceptions.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"""Exceptions that may happen in all the webdriver code."""
1818

1919
from collections.abc import Sequence
20-
from typing import Any, Optional
20+
from typing import Any
2121

2222
SUPPORT_MSG = "For documentation on this error, please visit:"
2323
ERROR_URL = "https://www.selenium.dev/documentation/webdriver/troubleshooting/errors"
@@ -27,7 +27,7 @@ class WebDriverException(Exception):
2727
"""Base webdriver exception."""
2828

2929
def __init__(
30-
self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
30+
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
3131
) -> None:
3232
super().__init__()
3333
self.msg = msg
@@ -73,7 +73,7 @@ class NoSuchElementException(WebDriverException):
7373
"""
7474

7575
def __init__(
76-
self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
76+
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
7777
) -> None:
7878
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#nosuchelementexception"
7979

@@ -111,7 +111,7 @@ class StaleElementReferenceException(WebDriverException):
111111
"""
112112

113113
def __init__(
114-
self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
114+
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
115115
) -> None:
116116
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#staleelementreferenceexception"
117117

@@ -134,10 +134,10 @@ class UnexpectedAlertPresentException(WebDriverException):
134134

135135
def __init__(
136136
self,
137-
msg: Optional[Any] = None,
138-
screen: Optional[str] = None,
139-
stacktrace: Optional[Sequence[str]] = None,
140-
alert_text: Optional[str] = None,
137+
msg: Any | None = None,
138+
screen: str | None = None,
139+
stacktrace: Sequence[str] | None = None,
140+
alert_text: str | None = None,
141141
) -> None:
142142
super().__init__(msg, screen, stacktrace)
143143
self.alert_text = alert_text
@@ -161,7 +161,7 @@ class ElementNotVisibleException(InvalidElementStateException):
161161
"""
162162

163163
def __init__(
164-
self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
164+
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
165165
) -> None:
166166
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#elementnotvisibleexception"
167167

@@ -172,7 +172,7 @@ class ElementNotInteractableException(InvalidElementStateException):
172172
"""Thrown when element interactions will hit another element due to paint order."""
173173

174174
def __init__(
175-
self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
175+
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
176176
) -> None:
177177
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#elementnotinteractableexception"
178178

@@ -213,7 +213,7 @@ class InvalidSelectorException(WebDriverException):
213213
"""
214214

215215
def __init__(
216-
self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
216+
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
217217
) -> None:
218218
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#invalidselectorexception"
219219

@@ -252,7 +252,7 @@ class ElementClickInterceptedException(WebDriverException):
252252
"""Thrown when element click fails because another element obscures it."""
253253

254254
def __init__(
255-
self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
255+
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
256256
) -> None:
257257
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#elementclickinterceptedexception"
258258

@@ -271,7 +271,7 @@ class InvalidSessionIdException(WebDriverException):
271271
"""Thrown when the given session id is not in the list of active sessions."""
272272

273273
def __init__(
274-
self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
274+
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
275275
) -> None:
276276
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#invalidsessionidexception"
277277

@@ -282,7 +282,7 @@ class SessionNotCreatedException(WebDriverException):
282282
"""A new session could not be created."""
283283

284284
def __init__(
285-
self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
285+
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
286286
) -> None:
287287
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#sessionnotcreatedexception"
288288

@@ -297,7 +297,7 @@ class NoSuchDriverException(WebDriverException):
297297
"""Raised when driver is not specified and cannot be located."""
298298

299299
def __init__(
300-
self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
300+
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
301301
) -> None:
302302
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}/driver_location"
303303

py/selenium/webdriver/chrome/options.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from typing import Optional
1918

2019
from selenium.webdriver.chromium.options import ChromiumOptions
2120
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
@@ -28,8 +27,8 @@ def default_capabilities(self) -> dict:
2827

2928
def enable_mobile(
3029
self,
31-
android_package: Optional[str] = "com.android.chrome",
32-
android_activity: Optional[str] = None,
33-
device_serial: Optional[str] = None,
30+
android_package: str | None = "com.android.chrome",
31+
android_activity: str | None = None,
32+
device_serial: str | None = None,
3433
) -> None:
3534
super().enable_mobile(android_package, android_activity, device_serial)

py/selenium/webdriver/chrome/remote_connection.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from typing import Optional
1918

2019
from selenium.webdriver import DesiredCapabilities
2120
from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
@@ -29,8 +28,8 @@ def __init__(
2928
self,
3029
remote_server_addr: str,
3130
keep_alive: bool = True,
32-
ignore_proxy: Optional[bool] = False,
33-
client_config: Optional[ClientConfig] = None,
31+
ignore_proxy: bool | None = False,
32+
client_config: ClientConfig | None = None,
3433
) -> None:
3534
super().__init__(
3635
remote_server_addr=remote_server_addr,

py/selenium/webdriver/chrome/service.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818

1919
from collections.abc import Mapping, Sequence
20-
from typing import Optional
2120

2221
from selenium.types import SubprocessStdAlias
2322
from selenium.webdriver.chromium import service
@@ -41,11 +40,11 @@ class Service(service.ChromiumService):
4140

4241
def __init__(
4342
self,
44-
executable_path: Optional[str] = None,
43+
executable_path: str | None = None,
4544
port: int = 0,
46-
service_args: Optional[Sequence[str]] = None,
47-
log_output: Optional[SubprocessStdAlias] = None,
48-
env: Optional[Mapping[str, str]] = None,
45+
service_args: Sequence[str] | None = None,
46+
log_output: SubprocessStdAlias | None = None,
47+
env: Mapping[str, str] | None = None,
4948
**kwargs,
5049
) -> None:
5150
self._service_args = service_args or []

py/selenium/webdriver/chrome/webdriver.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from typing import Optional
1918

2019
from selenium.webdriver.chrome.options import Options
2120
from selenium.webdriver.chrome.service import Service
@@ -28,8 +27,8 @@ class WebDriver(ChromiumDriver):
2827

2928
def __init__(
3029
self,
31-
options: Optional[Options] = None,
32-
service: Optional[Service] = None,
30+
options: Options | None = None,
31+
service: Service | None = None,
3332
keep_alive: bool = True,
3433
) -> None:
3534
"""Creates a new instance of the chrome driver.

py/selenium/webdriver/chromium/options.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import base64
1919
import os
20-
from typing import BinaryIO, Optional, Union
20+
from typing import BinaryIO
2121

2222
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
2323
from selenium.webdriver.common.options import ArgOptions
@@ -32,8 +32,8 @@ def __init__(self) -> None:
3232
self._binary_location: str = ""
3333
self._extension_files: list[str] = []
3434
self._extensions: list[str] = []
35-
self._experimental_options: dict[str, Union[str, int, dict, list[str]]] = {}
36-
self._debugger_address: Optional[str] = None
35+
self._experimental_options: dict[str, str | int | dict | list[str]] = {}
36+
self._debugger_address: str | None = None
3737
self._enable_webextensions: bool = False
3838

3939
@property
@@ -53,7 +53,7 @@ def binary_location(self, value: str) -> None:
5353
self._binary_location = value
5454

5555
@property
56-
def debugger_address(self) -> Optional[str]:
56+
def debugger_address(self) -> str | None:
5757
"""Returns the address of the remote devtools instance."""
5858
return self._debugger_address
5959

@@ -116,7 +116,7 @@ def experimental_options(self) -> dict:
116116
"""Returns a dictionary of experimental options for chromium."""
117117
return self._experimental_options
118118

119-
def add_experimental_option(self, name: str, value: Union[str, int, dict, list[str]]) -> None:
119+
def add_experimental_option(self, name: str, value: str | int | dict | list[str]) -> None:
120120
"""Adds an experimental option which is passed to chromium.
121121
122122
Args:

py/selenium/webdriver/chromium/remote_connection.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17-
from typing import Optional
1817

1918
from selenium.webdriver.remote.client_config import ClientConfig
2019
from selenium.webdriver.remote.remote_connection import RemoteConnection
@@ -27,8 +26,8 @@ def __init__(
2726
vendor_prefix: str,
2827
browser_name: str,
2928
keep_alive: bool = True,
30-
ignore_proxy: Optional[bool] = False,
31-
client_config: Optional[ClientConfig] = None,
29+
ignore_proxy: bool | None = False,
30+
client_config: ClientConfig | None = None,
3231
) -> None:
3332
client_config = client_config or ClientConfig(
3433
remote_server_addr=remote_server_addr, keep_alive=keep_alive, timeout=120

py/selenium/webdriver/chromium/service.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
from collections.abc import Mapping, Sequence
1919
from io import IOBase
20-
from typing import Optional
2120

2221
from selenium.types import SubprocessStdAlias
2322
from selenium.webdriver.common import service
@@ -42,20 +41,20 @@ class ChromiumService(service.Service):
4241

4342
def __init__(
4443
self,
45-
executable_path: Optional[str] = None,
44+
executable_path: str | None = None,
4645
port: int = 0,
47-
service_args: Optional[Sequence[str]] = None,
48-
log_output: Optional[SubprocessStdAlias] = None,
49-
env: Optional[Mapping[str, str]] = None,
50-
driver_path_env_key: Optional[str] = None,
46+
service_args: Sequence[str] | None = None,
47+
log_output: SubprocessStdAlias | None = None,
48+
env: Mapping[str, str] | None = None,
49+
driver_path_env_key: str | None = None,
5150
**kwargs,
5251
) -> None:
5352
self._service_args = list(service_args or [])
5453
driver_path_env_key = driver_path_env_key or "SE_CHROMEDRIVER"
5554

5655
if isinstance(log_output, str):
5756
self._service_args.append(f"--log-path={log_output}")
58-
self.log_output: Optional[IOBase] = None
57+
self.log_output: IOBase | None = None
5958
elif isinstance(log_output, IOBase):
6059
self.log_output = log_output
6160
else:

0 commit comments

Comments
 (0)