Skip to content

Commit c187279

Browse files
authored
[py] Type hint cleanup (#15917)
Moves some types from typing to collections.abc, and replaces some type hints with native data types.
1 parent d3ad2e6 commit c187279

File tree

10 files changed

+36
-35
lines changed

10 files changed

+36
-35
lines changed

py/selenium/webdriver/chrome/service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
# under the License.
1717

1818

19-
from collections.abc import Mapping
20-
from typing import Optional, Sequence
19+
from collections.abc import Mapping, Sequence
20+
from typing import Optional
2121

2222
from selenium.types import SubprocessStdAlias
2323
from selenium.webdriver.chromium import service

py/selenium/webdriver/chromium/service.py

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

18-
from collections.abc import Mapping
18+
from collections.abc import Mapping, Sequence
1919
from io import IOBase
20-
from typing import Optional, Sequence
20+
from typing import Optional
2121

2222
from selenium.types import SubprocessStdAlias
2323
from selenium.webdriver.common import service

py/selenium/webdriver/common/bidi/script.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# under the License.
1717

1818
from dataclasses import dataclass
19-
from typing import Any, Dict, List, Optional
19+
from typing import Any, Optional
2020

2121
from selenium.webdriver.common.bidi.common import command_builder
2222

@@ -55,7 +55,7 @@ class RealmInfo:
5555
sandbox: Optional[str] = None
5656

5757
@classmethod
58-
def from_json(cls, json: Dict[str, Any]) -> "RealmInfo":
58+
def from_json(cls, json: dict[str, Any]) -> "RealmInfo":
5959
"""Creates a RealmInfo instance from a dictionary.
6060
6161
Parameters:
@@ -90,7 +90,7 @@ class Source:
9090
context: Optional[str] = None
9191

9292
@classmethod
93-
def from_json(cls, json: Dict[str, Any]) -> "Source":
93+
def from_json(cls, json: dict[str, Any]) -> "Source":
9494
"""Creates a Source instance from a dictionary.
9595
9696
Parameters:
@@ -120,7 +120,7 @@ class EvaluateResult:
120120
exception_details: Optional[dict] = None
121121

122122
@classmethod
123-
def from_json(cls, json: Dict[str, Any]) -> "EvaluateResult":
123+
def from_json(cls, json: dict[str, Any]) -> "EvaluateResult":
124124
"""Creates an EvaluateResult instance from a dictionary.
125125
126126
Parameters:
@@ -155,7 +155,7 @@ def __init__(self, channel: str, data: dict, source: Source):
155155
self.source = source
156156

157157
@classmethod
158-
def from_json(cls, json: Dict[str, Any]) -> "ScriptMessage":
158+
def from_json(cls, json: dict[str, Any]) -> "ScriptMessage":
159159
"""Creates a ScriptMessage instance from a dictionary.
160160
161161
Parameters:
@@ -189,7 +189,7 @@ def __init__(self, realm_info: RealmInfo):
189189
self.realm_info = realm_info
190190

191191
@classmethod
192-
def from_json(cls, json: Dict[str, Any]) -> "RealmCreated":
192+
def from_json(cls, json: dict[str, Any]) -> "RealmCreated":
193193
"""Creates a RealmCreated instance from a dictionary.
194194
195195
Parameters:
@@ -212,7 +212,7 @@ def __init__(self, realm: str):
212212
self.realm = realm
213213

214214
@classmethod
215-
def from_json(cls, json: Dict[str, Any]) -> "RealmDestroyed":
215+
def from_json(cls, json: dict[str, Any]) -> "RealmDestroyed":
216216
"""Creates a RealmDestroyed instance from a dictionary.
217217
218218
Parameters:
@@ -262,9 +262,9 @@ def remove_console_message_handler(self, id):
262262
def _add_preload_script(
263263
self,
264264
function_declaration: str,
265-
arguments: Optional[List[Dict[str, Any]]] = None,
266-
contexts: Optional[List[str]] = None,
267-
user_contexts: Optional[List[str]] = None,
265+
arguments: Optional[list[dict[str, Any]]] = None,
266+
contexts: Optional[list[str]] = None,
267+
user_contexts: Optional[list[str]] = None,
268268
sandbox: Optional[str] = None,
269269
) -> str:
270270
"""Adds a preload script.
@@ -288,7 +288,7 @@ def _add_preload_script(
288288
if contexts is not None and user_contexts is not None:
289289
raise ValueError("Cannot specify both contexts and user_contexts")
290290

291-
params: Dict[str, Any] = {"functionDeclaration": function_declaration}
291+
params: dict[str, Any] = {"functionDeclaration": function_declaration}
292292

293293
if arguments is not None:
294294
params["arguments"] = arguments
@@ -312,7 +312,7 @@ def _remove_preload_script(self, script_id: str) -> None:
312312
params = {"script": script_id}
313313
self.conn.execute(command_builder("script.removePreloadScript", params))
314314

315-
def _disown(self, handles: List[str], target: dict) -> None:
315+
def _disown(self, handles: list[str], target: dict) -> None:
316316
"""Disowns the given handles.
317317
318318
Parameters:
@@ -331,7 +331,7 @@ def _call_function(
331331
function_declaration: str,
332332
await_promise: bool,
333333
target: dict,
334-
arguments: Optional[List[dict]] = None,
334+
arguments: Optional[list[dict]] = None,
335335
result_ownership: Optional[str] = None,
336336
serialization_options: Optional[dict] = None,
337337
this: Optional[dict] = None,
@@ -416,7 +416,7 @@ def _get_realms(
416416
self,
417417
context: Optional[str] = None,
418418
type: Optional[str] = None,
419-
) -> List[RealmInfo]:
419+
) -> list[RealmInfo]:
420420
"""Returns a list of all realms, optionally filtered.
421421
422422
Parameters:

py/selenium/webdriver/edge/service.py

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

18-
from collections.abc import Mapping
19-
from typing import Optional, Sequence
18+
from collections.abc import Mapping, Sequence
19+
from typing import Optional
2020

2121
from selenium.types import SubprocessStdAlias
2222
from selenium.webdriver.chromium import service

py/selenium/webdriver/firefox/service.py

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

18-
from collections.abc import Mapping
19-
from typing import Optional, Sequence
18+
from collections.abc import Mapping, Sequence
19+
from typing import Optional
2020

2121
from selenium.types import SubprocessStdAlias
2222
from selenium.webdriver.common import service, utils

py/selenium/webdriver/ie/service.py

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

18-
from typing import Optional, Sequence
18+
from collections.abc import Sequence
19+
from typing import Optional
1920

2021
from selenium.types import SubprocessStdAlias
2122
from selenium.webdriver.common import service

py/selenium/webdriver/remote/webdriver.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from base64 import b64decode, urlsafe_b64encode
3030
from contextlib import asynccontextmanager, contextmanager
3131
from importlib import import_module
32-
from typing import Any, Dict, Optional, Type, Union, cast
32+
from typing import Any, Optional, Union, cast
3333

3434
from selenium.common.exceptions import (
3535
InvalidArgumentException,
@@ -136,7 +136,7 @@ def get_remote_connection(
136136

137137

138138
def create_matches(options: list[BaseOptions]) -> dict:
139-
capabilities: Dict[str, Any] = {"capabilities": {}}
139+
capabilities: dict[str, Any] = {"capabilities": {}}
140140
opts = []
141141
for opt in options:
142142
opts.append(opt.to_capabilities())
@@ -201,7 +201,7 @@ def __init__(
201201
file_detector: Optional[FileDetector] = None,
202202
options: Optional[Union[BaseOptions, list[BaseOptions]]] = None,
203203
locator_converter: Optional[LocatorConverter] = None,
204-
web_element_cls: Optional[Type[WebElement]] = None,
204+
web_element_cls: Optional[type[WebElement]] = None,
205205
client_config: Optional[ClientConfig] = None,
206206
) -> None:
207207
"""Create a new driver that will issue commands using the wire
@@ -248,8 +248,8 @@ def __init__(
248248
)
249249
self._is_remote = True
250250
self.session_id: Optional[str] = None
251-
self.caps: Dict[str, Any] = {}
252-
self.pinned_scripts: Dict[str, Any] = {}
251+
self.caps: dict[str, Any] = {}
252+
self.pinned_scripts: dict[str, Any] = {}
253253
self.error_handler = ErrorHandler()
254254
self._switch_to = SwitchTo(self)
255255
self._mobile = Mobile(self)
@@ -668,7 +668,7 @@ def print_page(self, print_options: Optional[PrintOptions] = None) -> str:
668668
--------
669669
>>> driver.print_page()
670670
"""
671-
options: Union[Dict[str, Any], Any] = {}
671+
options: Union[dict[str, Any], Any] = {}
672672
if print_options:
673673
options = print_options.to_dict()
674674

py/selenium/webdriver/safari/service.py

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

18-
from collections.abc import Mapping
19-
from typing import Optional, Sequence
18+
from collections.abc import Mapping, Sequence
19+
from typing import Optional
2020

2121
from selenium.webdriver.common import service
2222

py/selenium/webdriver/webkitgtk/service.py

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

1818
import shutil
1919
import warnings
20-
from collections.abc import Mapping
21-
from typing import Optional, Sequence
20+
from collections.abc import Mapping, Sequence
21+
from typing import Optional
2222

2323
from selenium.webdriver.common import service
2424

py/selenium/webdriver/wpewebkit/service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
# under the License.
1717

1818
import shutil
19-
from collections.abc import Mapping
20-
from typing import Optional, Sequence
19+
from collections.abc import Mapping, Sequence
20+
from typing import Optional
2121

2222
from selenium.webdriver.common import service
2323

0 commit comments

Comments
 (0)