Skip to content

Commit c28f4a6

Browse files
committed
Fix typing in remote webdriver
The main change is changing the return type hint for `def get_downloadable_files(self)` from `dict` to `List[str]`, which is actually what is returned. Also some cosmetic improvements have been added.
1 parent 105a496 commit c28f4a6

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

py/selenium/webdriver/remote/webdriver.py

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import pkgutil
2323
import tempfile
2424
import types
25-
import typing
2625
import warnings
2726
import zipfile
2827
from abc import ABCMeta
@@ -31,33 +30,31 @@
3130
from contextlib import asynccontextmanager
3231
from contextlib import contextmanager
3332
from importlib import import_module
34-
from typing import Dict
35-
from typing import List
36-
from typing import Optional
37-
from typing import Union
38-
39-
from selenium.common.exceptions import InvalidArgumentException
40-
from selenium.common.exceptions import JavascriptException
41-
from selenium.common.exceptions import NoSuchCookieException
42-
from selenium.common.exceptions import NoSuchElementException
43-
from selenium.common.exceptions import WebDriverException
33+
from typing import Dict, List, Optional, Union, Type
34+
35+
from selenium.common.exceptions import (
36+
InvalidArgumentException,
37+
JavascriptException,
38+
NoSuchCookieException,
39+
NoSuchElementException,
40+
WebDriverException
41+
)
4442
from selenium.webdriver.common.bidi.script import Script
4543
from selenium.webdriver.common.by import By
4644
from selenium.webdriver.common.options import BaseOptions
4745
from selenium.webdriver.common.print_page_options import PrintOptions
4846
from selenium.webdriver.common.timeouts import Timeouts
49-
from selenium.webdriver.common.virtual_authenticator import Credential
50-
from selenium.webdriver.common.virtual_authenticator import VirtualAuthenticatorOptions
5147
from selenium.webdriver.common.virtual_authenticator import (
48+
Credential,
49+
VirtualAuthenticatorOptions,
5250
required_virtual_authenticator,
5351
)
5452
from selenium.webdriver.support.relative_locator import RelativeBy
5553

5654
from .bidi_connection import BidiConnection
5755
from .command import Command
5856
from .errorhandler import ErrorHandler
59-
from .file_detector import FileDetector
60-
from .file_detector import LocalFileDetector
57+
from .file_detector import FileDetector, LocalFileDetector
6158
from .mobile import Mobile
6259
from .remote_connection import RemoteConnection
6360
from .script_key import ScriptKey
@@ -184,7 +181,6 @@ def __init__(
184181
then default LocalFileDetector() will be used.
185182
- options - instance of a driver options.Options class
186183
"""
187-
188184
if isinstance(options, list):
189185
capabilities = create_matches(options)
190186
_ignore_local_proxy = False
@@ -222,9 +218,9 @@ def __enter__(self):
222218

223219
def __exit__(
224220
self,
225-
exc_type: typing.Optional[typing.Type[BaseException]],
226-
exc: typing.Optional[BaseException],
227-
traceback: typing.Optional[types.TracebackType],
221+
exc_type: Optional[Type[BaseException]],
222+
exc: Optional[BaseException],
223+
traceback: Optional[types.TracebackType],
228224
):
229225
self.quit()
230226

@@ -294,7 +290,6 @@ def start_session(self, capabilities: dict) -> None:
294290
:Args:
295291
- capabilities - a capabilities dict to start the session with.
296292
"""
297-
298293
caps = _create_caps(capabilities)
299294
response = self.execute(Command.NEW_SESSION, caps)["value"]
300295
self.session_id = response.get("sessionId")
@@ -375,7 +370,8 @@ def title(self) -> str:
375370

376371
def pin_script(self, script: str, script_key=None) -> ScriptKey:
377372
"""Store common javascript scripts to be executed later by a unique
378-
hashable ID."""
373+
hashable ID.
374+
"""
379375
script_key_instance = ScriptKey(script_key)
380376
self.pinned_scripts[script_key_instance.id] = script
381377
return script_key_instance
@@ -527,9 +523,7 @@ def print_page(self, print_options: Optional[PrintOptions] = None) -> str:
527523

528524
@property
529525
def switch_to(self) -> SwitchTo:
530-
"""
531-
:Returns:
532-
- SwitchTo: an object containing all options to switch focus into
526+
"""Return the `SwitchTo` object containing all options for switching focus.
533527
534528
:Usage:
535529
::
@@ -588,7 +582,7 @@ def get_cookies(self) -> List[dict]:
588582
"""
589583
return self.execute(Command.GET_ALL_COOKIES)["value"]
590584

591-
def get_cookie(self, name) -> typing.Optional[typing.Dict]:
585+
def get_cookie(self, name) -> Optional[Dict]:
592586
"""Get a single cookie by name. Returns the cookie if found, None if
593587
not.
594588
@@ -872,7 +866,6 @@ def get_window_size(self, windowHandle: str = "current") -> dict:
872866
873867
driver.get_window_size()
874868
"""
875-
876869
self._check_if_window_handle_is_current(windowHandle)
877870
size = self.get_window_rect()
878871

@@ -904,7 +897,6 @@ def get_window_position(self, windowHandle="current") -> dict:
904897
905898
driver.get_window_position()
906899
"""
907-
908900
self._check_if_window_handle_is_current(windowHandle)
909901
position = self.get_window_rect()
910902

@@ -939,7 +931,6 @@ def set_window_rect(self, x=None, y=None, width=None, height=None) -> dict:
939931
driver.set_window_rect(width=100, height=200)
940932
driver.set_window_rect(x=10, y=10, width=100, height=200)
941933
"""
942-
943934
if (x is None and y is None) and (not height and not width):
944935
raise InvalidArgumentException("x and y or height and width need values")
945936

@@ -1158,7 +1149,7 @@ def remove_credential(self, credential_id: Union[str, bytearray]) -> None:
11581149
credential_id = urlsafe_b64encode(credential_id).decode()
11591150

11601151
self.execute(
1161-
Command.REMOVE_CREDENTIAL, {"credentialId": credential_id, "authenticatorId": self._authenticator_id}
1152+
Command.REMOVE_CREDENTIAL, {"credentialId": credential_id, "authenticatorId": self._authenticator_id},
11621153
)
11631154

11641155
@required_virtual_authenticator
@@ -1175,9 +1166,10 @@ def set_user_verified(self, verified: bool) -> None:
11751166
"""
11761167
self.execute(Command.SET_USER_VERIFIED, {"authenticatorId": self._authenticator_id, "isUserVerified": verified})
11771168

1178-
def get_downloadable_files(self) -> dict:
1169+
def get_downloadable_files(self) -> List[str]:
11791170
"""Retrieves the downloadable files as a map of file names and their
1180-
corresponding URLs."""
1171+
corresponding URLs.
1172+
"""
11811173
if "se:downloadsEnabled" not in self.capabilities:
11821174
raise WebDriverException("You must enable downloads in order to work with downloadable files.")
11831175

0 commit comments

Comments
 (0)