Skip to content

Commit 5c2959b

Browse files
committed
refactor[py]: Add type hints to webdriver.py and webelement.py
- Added explicit type annotations to selenium.webdriver.remote.webdriver.py and selenium.webdriver.remote.webelement.py - Improved code clarity and static type checking - Ensured compatibility with modern type checkers like Pyright and Mypy
1 parent 05ab017 commit 5c2959b

File tree

5 files changed

+490
-171
lines changed

5 files changed

+490
-171
lines changed

py/selenium/webdriver/common/options.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from abc import ABCMeta
2020
from abc import abstractmethod
2121
from enum import Enum
22+
from typing import Any
2223
from typing import Optional
2324

2425
from selenium.common.exceptions import InvalidArgumentException
@@ -42,7 +43,7 @@ class PageLoadStrategy(str, Enum):
4243

4344

4445
class _BaseOptionsDescriptor:
45-
def __init__(self, name):
46+
def __init__(self, name: str):
4647
self.name = name
4748

4849
def __get__(self, obj, cls):
@@ -54,7 +55,12 @@ def __get__(self, obj, cls):
5455
# Return socket url or None if not created yet
5556
value = obj._caps.get(self.name)
5657
return None if not isinstance(value, str) else value
57-
if self.name in ("acceptInsecureCerts", "strictFileInteractability", "setWindowRect", "se:downloadsEnabled"):
58+
if self.name in (
59+
"acceptInsecureCerts",
60+
"strictFileInteractability",
61+
"setWindowRect",
62+
"se:downloadsEnabled",
63+
):
5864
return obj._caps.get(self.name, False)
5965
return obj._caps.get(self.name)
6066

@@ -82,7 +88,9 @@ def __set__(self, obj, value):
8288
if value in ("normal", "eager", "none"):
8389
obj.set_capability(self.name, value)
8490
else:
85-
raise ValueError("Strategy can only be one of the following: normal, eager, none")
91+
raise ValueError(
92+
"Strategy can only be one of the following: normal, eager, none"
93+
)
8694

8795

8896
class _UnHandledPromptBehaviorDescriptor:
@@ -102,7 +110,13 @@ def __get__(self, obj, cls):
102110
return obj._caps.get(self.name)
103111

104112
def __set__(self, obj, value):
105-
if value in ("dismiss", "accept", "dismiss and notify", "accept and notify", "ignore"):
113+
if value in (
114+
"dismiss",
115+
"accept",
116+
"dismiss and notify",
117+
"accept and notify",
118+
"ignore",
119+
):
106120
obj.set_capability(self.name, value)
107121
else:
108122
raise ValueError(
@@ -130,7 +144,9 @@ def __set__(self, obj, value):
130144
if all(x in ("implicit", "pageLoad", "script") for x in value.keys()):
131145
obj.set_capability(self.name, value)
132146
else:
133-
raise ValueError("Timeout keys can only be one of the following: implicit, pageLoad, script")
147+
raise ValueError(
148+
"Timeout keys can only be one of the following: implicit, pageLoad, script"
149+
)
134150

135151

136152
class _ProxyDescriptor:
@@ -144,7 +160,9 @@ def __get__(self, obj, cls):
144160

145161
def __set__(self, obj, value):
146162
if not isinstance(value, Proxy):
147-
raise InvalidArgumentException("Only Proxy objects can be passed in.")
163+
raise InvalidArgumentException(
164+
"Only Proxy objects can be passed in."
165+
)
148166
obj._proxy = value
149167
obj._caps[self.name] = value.to_capabilities()
150168

@@ -218,7 +236,9 @@ class BaseOptions(metaclass=ABCMeta):
218236
- `None`
219237
"""
220238

221-
strict_file_interactability = _BaseOptionsDescriptor("strictFileInteractability")
239+
strict_file_interactability = _BaseOptionsDescriptor(
240+
"strictFileInteractability"
241+
)
222242
"""Gets and Sets whether session is about file interactability.
223243
224244
Usage
@@ -328,7 +348,9 @@ class BaseOptions(metaclass=ABCMeta):
328348
- `None`
329349
"""
330350

331-
unhandled_prompt_behavior = _UnHandledPromptBehaviorDescriptor("unhandledPromptBehavior")
351+
unhandled_prompt_behavior = _UnHandledPromptBehaviorDescriptor(
352+
"unhandledPromptBehavior"
353+
)
332354
""":Gets and Sets unhandled prompt behavior, the default is "dismiss and
333355
notify".
334356
@@ -452,7 +474,7 @@ def __init__(self) -> None:
452474
def capabilities(self):
453475
return self._caps
454476

455-
def set_capability(self, name, value) -> None:
477+
def set_capability(self, name: Any, value: Any) -> None:
456478
"""Sets a capability."""
457479
self._caps[name] = value
458480

@@ -476,12 +498,12 @@ def enable_mobile(
476498
self.mobile_options["androidDeviceSerial"] = device_serial
477499

478500
@abstractmethod
479-
def to_capabilities(self):
501+
def to_capabilities(self) -> dict[Any, Any]:
480502
"""Convert options into capabilities dictionary."""
481503

482504
@property
483505
@abstractmethod
484-
def default_capabilities(self):
506+
def default_capabilities(self) -> dict[Any, Any]:
485507
"""Return minimal capabilities necessary as a dictionary."""
486508

487509
def ignore_local_proxy_environment_variables(self) -> None:
@@ -497,14 +519,15 @@ class ArgOptions(BaseOptions):
497519

498520
def __init__(self) -> None:
499521
super().__init__()
500-
self._arguments = []
522+
self._arguments: list[Any] = []
523+
self._caps: dict[Any, Any]
501524

502525
@property
503526
def arguments(self):
504527
""":Returns: A list of arguments needed for the browser."""
505528
return self._arguments
506529

507-
def add_argument(self, argument) -> None:
530+
def add_argument(self, argument: Any) -> None:
508531
"""Adds an argument to the list.
509532
510533
:Args:
@@ -529,9 +552,9 @@ def ignore_local_proxy_environment_variables(self) -> None:
529552

530553
super().ignore_local_proxy_environment_variables()
531554

532-
def to_capabilities(self):
555+
def to_capabilities(self) -> dict[Any, Any]:
533556
return self._caps
534557

535558
@property
536-
def default_capabilities(self):
559+
def default_capabilities(self) -> dict[Any, Any]:
537560
return {}

0 commit comments

Comments
 (0)