Skip to content

Commit be40b5c

Browse files
navin772harsha509
andauthored
fix type errors for service.py, cdp.py, webelement.py and remote_connection.py (#14448)
* fix type errors for `service.py`, `cdp.py`, `webelement.py` and `remote_connection.py` * remove raise error --------- Co-authored-by: Sri Harsha <12621691+harsha509@users.noreply.github.com>
1 parent 8fc4299 commit be40b5c

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

‎py/selenium/webdriver/common/bidi/cdp.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ async def wait_for(self, event_type: typing.Type[T], buffer_size=10) -> typing.A
237237
an async with block. The block will not exit until the indicated
238238
event is received.
239239
"""
240+
sender: trio.MemorySendChannel
241+
receiver: trio.MemoryReceiveChannel
240242
sender, receiver = trio.open_memory_channel(buffer_size)
241243
self.channels[event_type].add(sender)
242244
proxy = CmEventProxy()

‎py/selenium/webdriver/common/service.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from platform import system
2626
from subprocess import PIPE
2727
from time import sleep
28+
from typing import cast
2829
from urllib import request
2930
from urllib.error import URLError
3031

@@ -55,11 +56,11 @@ def __init__(
5556
**kwargs,
5657
) -> None:
5758
if isinstance(log_output, str):
58-
self.log_output = open(log_output, "a+", encoding="utf-8")
59+
self.log_output = cast(IOBase, open(log_output, "a+", encoding="utf-8"))
5960
elif log_output == subprocess.STDOUT:
60-
self.log_output = None
61+
self.log_output = cast(typing.Optional[typing.Union[int, IOBase]], None)
6162
elif log_output is None or log_output == subprocess.DEVNULL:
62-
self.log_output = subprocess.DEVNULL
63+
self.log_output = cast(typing.Optional[typing.Union[int, IOBase]], subprocess.DEVNULL)
6364
else:
6465
self.log_output = log_output
6566

@@ -82,7 +83,7 @@ def command_line_args(self) -> typing.List[str]:
8283

8384
@property
8485
def path(self) -> str:
85-
return self._path
86+
return self._path or ""
8687

8788
@path.setter
8889
def path(self, value: str) -> None:
@@ -95,6 +96,8 @@ def start(self) -> None:
9596
- WebDriverException : Raised either when it can't start the service
9697
or when it can't connect to the service
9798
"""
99+
if self._path is None:
100+
raise WebDriverException("Service path cannot be None.")
98101
self._start_process(self._path)
99102

100103
count = 0
@@ -201,16 +204,16 @@ def _start_process(self, path: str) -> None:
201204
try:
202205
start_info = None
203206
if system() == "Windows":
204-
start_info = subprocess.STARTUPINFO()
205-
start_info.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW
206-
start_info.wShowWindow = subprocess.SW_HIDE
207+
start_info = subprocess.STARTUPINFO() # type: ignore[attr-defined]
208+
start_info.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW # type: ignore[attr-defined]
209+
start_info.wShowWindow = subprocess.SW_HIDE # type: ignore[attr-defined]
207210

208211
self.process = subprocess.Popen(
209212
cmd,
210213
env=self.env,
211214
close_fds=close_file_descriptors,
212-
stdout=self.log_output,
213-
stderr=self.log_output,
215+
stdout=cast(typing.Optional[typing.Union[int, typing.IO[typing.Any]]], self.log_output),
216+
stderr=cast(typing.Optional[typing.Union[int, typing.IO[typing.Any]]], self.log_output),
214217
stdin=PIPE,
215218
creationflags=self.creation_flags,
216219
startupinfo=start_info,
@@ -227,6 +230,8 @@ def _start_process(self, path: str) -> None:
227230
raise
228231
except OSError as err:
229232
if err.errno == errno.EACCES:
233+
if self._path is None:
234+
raise WebDriverException("Service path cannot be None.")
230235
raise WebDriverException(
231236
f"'{os.path.basename(self._path)}' executable may have wrong permissions."
232237
) from err

‎py/selenium/webdriver/remote/remote_connection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ class RemoteConnection:
137137

138138
browser_name = None
139139
_timeout = (
140-
float(os.getenv("GLOBAL_DEFAULT_TIMEOUT"))
141-
if "GLOBAL_DEFAULT_TIMEOUT" in os.environ
142-
else socket._GLOBAL_DEFAULT_TIMEOUT
140+
float(os.getenv("GLOBAL_DEFAULT_TIMEOUT", str(socket.getdefaulttimeout())))
141+
if os.getenv("GLOBAL_DEFAULT_TIMEOUT") is not None
142+
else socket.getdefaulttimeout()
143143
)
144144
_ca_certs = os.getenv("REQUESTS_CA_BUNDLE") if "REQUESTS_CA_BUNDLE" in os.environ else certifi.where()
145145

‎py/selenium/webdriver/remote/webelement.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def send_keys(self, *value: str) -> None:
226226
remote_files = []
227227
for file in local_files:
228228
remote_files.append(self._upload(file))
229-
value = "\n".join(remote_files)
229+
value = tuple("\n".join(remote_files))
230230

231231
self._execute(
232232
Command.SEND_KEYS_TO_ELEMENT, {"text": "".join(keys_to_typing(value)), "value": keys_to_typing(value)}

‎py/selenium/webdriver/webkitgtk/service.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from selenium.webdriver.common import service
2020

21-
DEFAULT_EXECUTABLE_PATH = "WebKitWebDriver"
21+
DEFAULT_EXECUTABLE_PATH: str = "WebKitWebDriver"
2222

2323

2424
class Service(service.Service):
@@ -40,7 +40,7 @@ def __init__(
4040
service_args: typing.Optional[typing.List[str]] = None,
4141
env: typing.Optional[typing.Mapping[str, str]] = None,
4242
**kwargs,
43-
):
43+
) -> None:
4444
self.service_args = service_args or []
4545
log_file = open(log_path, "wb") if log_path else None
4646
super().__init__(
@@ -49,7 +49,7 @@ def __init__(
4949
log_file=log_file,
5050
env=env,
5151
**kwargs,
52-
) # type: ignore
52+
)
5353

5454
def command_line_args(self) -> typing.List[str]:
5555
return ["-p", f"{self.port}"] + self.service_args

0 commit comments

Comments
 (0)