Skip to content

Commit 87f9e40

Browse files
[py] Fix D205 ruff warnings for docstrings and add type hints (#16537)
Co-authored-by: Corey Goldberg <[email protected]>
1 parent 89baf16 commit 87f9e40

Some content is hidden

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

53 files changed

+499
-654
lines changed

py/generate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class CdpProperty:
216216
deprecated: bool
217217

218218
@property
219-
def py_name(self):
219+
def py_name(self)->str:
220220
''' Get this property's Python name. '''
221221
return snake_case(self.name)
222222

@@ -590,7 +590,7 @@ class CdpCommand:
590590
domain: str
591591

592592
@property
593-
def py_name(self):
593+
def py_name(self) -> str:
594594
''' Get a Python name for this command. '''
595595
return snake_case(self.name)
596596

@@ -794,7 +794,7 @@ class CdpDomain:
794794
events: List[CdpEvent]
795795

796796
@property
797-
def module(self):
797+
def module(self) -> str:
798798
''' The name of the Python module for this CDP domain. '''
799799
return snake_case(self.domain)
800800

py/generate_api_module_listing.py

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

1818

19-
"""This script recursively scans the `selenium` package directory
20-
to find all modules, then generates the `py/docs/source/api.rst`
21-
file containing a listing of all modules in separate sections.
22-
The `api.rst` file is later used by `sphinx-autogen` to generate
23-
sphinx autodoc stub pages used in the Python API documentation.
24-
See `py/tox.ini` for how it is invoked.
19+
"""This script recursively scans the `selenium` package directory and generates an API listing.
20+
21+
Recursively scans the `selenium` package directory to find all modules,
22+
then generates the `py/docs/source/api.rst` file containing a listing of all
23+
modules in separate sections. The `api.rst` file is later used by
24+
`sphinx-autogen` to generate sphinx autodoc stub pages used in the Python API
25+
documentation. See `py/tox.ini` for how it is invoked.
2526
"""
2627

2728
import os

py/pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ extend-ignore = [
154154
"D104", # Missing docstring in public package
155155
"D105", # Missing docstring in magic method
156156
"D107", # Missing docstring in `__init__`
157-
"D205", # 1 blank line required between summary line and description
158157
"D212", # Multi-line docstring summary should start at the first line
159158
]
160159

py/selenium/common/exceptions.py

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ class NoSuchAttributeException(WebDriverException):
9191

9292

9393
class NoSuchShadowRootException(WebDriverException):
94-
"""Thrown when trying to access the shadow root of an element when it does
95-
not have a shadow root attached.
96-
"""
94+
"""Thrown when trying to access the shadow root of an element when it does not have a shadow root attached."""
9795

9896

9997
class StaleElementReferenceException(WebDriverException):
@@ -121,11 +119,9 @@ def __init__(
121119

122120

123121
class InvalidElementStateException(WebDriverException):
124-
"""Thrown when a command could not be completed because the element is in
125-
an invalid state.
122+
"""Thrown when a command could not be completed because the element is in an invalid state.
126123
127-
This can be caused by attempting to clear an element that isn't both
128-
editable and resettable.
124+
This can be caused by attempting to clear an element that isn't both editable and resettable.
129125
"""
130126

131127

@@ -159,11 +155,9 @@ class NoAlertPresentException(WebDriverException):
159155

160156

161157
class ElementNotVisibleException(InvalidElementStateException):
162-
"""Thrown when an element is present on the DOM, but it is not visible, and
163-
so is not able to be interacted with.
158+
"""Thrown when an element is present on the DOM, but it is not visible, and so is not able to be interacted with.
164159
165-
Most commonly encountered when trying to click or read text of an
166-
element that is hidden from view.
160+
Most commonly encountered when trying to click or read text of an element that is hidden from view.
167161
"""
168162

169163
def __init__(
@@ -175,9 +169,7 @@ def __init__(
175169

176170

177171
class ElementNotInteractableException(InvalidElementStateException):
178-
"""Thrown when an element is present in the DOM but interactions with that
179-
element will hit another element due to paint order.
180-
"""
172+
"""Thrown when element interactions will hit another element due to paint order."""
181173

182174
def __init__(
183175
self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
@@ -195,9 +187,7 @@ class ElementNotSelectableException(InvalidElementStateException):
195187

196188

197189
class InvalidCookieDomainException(WebDriverException):
198-
"""Thrown when attempting to add a cookie under a different domain than the
199-
current URL.
200-
"""
190+
"""Thrown when attempting to add a cookie under a different domain."""
201191

202192

203193
class UnableToSetCookieException(WebDriverException):
@@ -209,23 +199,17 @@ class TimeoutException(WebDriverException):
209199

210200

211201
class MoveTargetOutOfBoundsException(WebDriverException):
212-
"""Thrown when the target provided to the `ActionsChains` move() method is
213-
invalid, i.e. out of document.
214-
"""
202+
"""Thrown when the target provided to the `ActionsChains` move() method is invalid, i.e. out of document."""
215203

216204

217205
class UnexpectedTagNameException(WebDriverException):
218206
"""Thrown when a support class did not get an expected web element."""
219207

220208

221209
class InvalidSelectorException(WebDriverException):
222-
"""Thrown when the selector which is used to find an element does not
223-
return a WebElement.
210+
"""Thrown when the selector used to find an element does not return a WebElement.
224211
225-
Currently this only happens when the selector is an xpath expression
226-
and it is either syntactically invalid (i.e. it is not a xpath
227-
expression) or the expression does not select WebElements (e.g.
228-
"count(//input)").
212+
Currently this only happens when the XPath expression is syntactically invalid or does not select WebElements.
229213
"""
230214

231215
def __init__(
@@ -257,20 +241,15 @@ class JavascriptException(WebDriverException):
257241

258242

259243
class NoSuchCookieException(WebDriverException):
260-
"""No cookie matching the given path name was found amongst the associated
261-
cookies of the current browsing context's active document.
262-
"""
244+
"""Thrown when no cookie matching the given path name was found."""
263245

264246

265247
class ScreenshotException(WebDriverException):
266248
"""A screen capture was made impossible."""
267249

268250

269251
class ElementClickInterceptedException(WebDriverException):
270-
"""The Element Click command could not be completed because the element
271-
receiving the events is obscuring the element that was requested to be
272-
clicked.
273-
"""
252+
"""Thrown when element click fails because another element obscures it."""
274253

275254
def __init__(
276255
self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
@@ -281,19 +260,15 @@ def __init__(
281260

282261

283262
class InsecureCertificateException(WebDriverException):
284-
"""Navigation caused the user agent to hit a certificate warning, which is
285-
usually the result of an expired or invalid TLS certificate.
286-
"""
263+
"""Thrown when the user agent hits a certificate warning (expired or invalid TLS certificate)."""
287264

288265

289266
class InvalidCoordinatesException(WebDriverException):
290267
"""The coordinates provided to an interaction's operation are invalid."""
291268

292269

293270
class InvalidSessionIdException(WebDriverException):
294-
"""Occurs if the given session id is not in the list of active sessions,
295-
meaning the session either does not exist or that it's not active.
296-
"""
271+
"""Thrown when the given session id is not in the list of active sessions."""
297272

298273
def __init__(
299274
self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
@@ -315,9 +290,7 @@ def __init__(
315290

316291

317292
class UnknownMethodException(WebDriverException):
318-
"""The requested command matched a known URL but did not match any methods
319-
for that URL.
320-
"""
293+
"""The requested command matched a known URL but did not match any methods for that URL."""
321294

322295

323296
class NoSuchDriverException(WebDriverException):

py/selenium/webdriver/chrome/service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424

2525

2626
class Service(service.ChromiumService):
27-
"""A Service class that is responsible for the starting and stopping of
28-
`chromedriver`.
27+
"""Service class responsible for starting and stopping the chromedriver executable.
2928
3029
Args:
3130
executable_path: Install path of the chromedriver executable, defaults
@@ -65,6 +64,7 @@ def command_line_args(self) -> list[str]:
6564

6665
@property
6766
def service_args(self) -> Sequence[str]:
67+
"""Returns the sequence of service arguments."""
6868
return self._service_args
6969

7070
@service_args.setter

py/selenium/webdriver/chromium/options.py

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ def __init__(self) -> None:
3838

3939
@property
4040
def binary_location(self) -> str:
41-
"""
42-
Returns:
43-
The location of the binary, otherwise an empty string.
44-
"""
41+
"""Returns the location of the binary, otherwise an empty string."""
4542
return self._binary_location
4643

4744
@binary_location.setter
@@ -57,16 +54,12 @@ def binary_location(self, value: str) -> None:
5754

5855
@property
5956
def debugger_address(self) -> Optional[str]:
60-
"""
61-
Returns:
62-
The address of the remote devtools instance.
63-
"""
57+
"""Returns the address of the remote devtools instance."""
6458
return self._debugger_address
6559

6660
@debugger_address.setter
6761
def debugger_address(self, value: str) -> None:
68-
"""Allows you to set the address of the remote devtools instance that
69-
the ChromeDriver instance will try to connect to during an active wait.
62+
"""Set the address of the remote devtools instance for active wait connection.
7063
7164
Args:
7265
value: Address of remote devtools instance if any (hostname[:port]).
@@ -77,10 +70,7 @@ def debugger_address(self, value: str) -> None:
7770

7871
@property
7972
def extensions(self) -> list[str]:
80-
"""
81-
Returns:
82-
A list of encoded extensions that will be loaded.
83-
"""
73+
"""Returns a list of encoded extensions that will be loaded."""
8474

8575
def _decode(file_data: BinaryIO) -> str:
8676
# Should not use base64.encodestring() which inserts newlines every
@@ -96,8 +86,7 @@ def _decode(file_data: BinaryIO) -> str:
9686
return encoded_extensions + self._extensions
9787

9888
def add_extension(self, extension: str) -> None:
99-
"""Adds the path to the extension to a list that will be used to
100-
extract it to the ChromeDriver.
89+
"""Add the path to an extension to be extracted to ChromeDriver.
10190
10291
Args:
10392
extension: Path to the *.crx file.
@@ -112,8 +101,7 @@ def add_extension(self, extension: str) -> None:
112101
raise ValueError("argument can not be null")
113102

114103
def add_encoded_extension(self, extension: str) -> None:
115-
"""Adds Base64 encoded string with extension data to a list that will
116-
be used to extract it to the ChromeDriver.
104+
"""Add Base64-encoded string with extension data to be extracted to ChromeDriver.
117105
118106
Args:
119107
extension: Base64 encoded string with extension data.
@@ -125,10 +113,7 @@ def add_encoded_extension(self, extension: str) -> None:
125113

126114
@property
127115
def experimental_options(self) -> dict:
128-
"""
129-
Returns:
130-
A dictionary of experimental options for chromium.
131-
"""
116+
"""Returns a dictionary of experimental options for chromium."""
132117
return self._experimental_options
133118

134119
def add_experimental_option(self, name: str, value: Union[str, int, dict, list[str]]) -> None:
@@ -142,11 +127,7 @@ def add_experimental_option(self, name: str, value: Union[str, int, dict, list[s
142127

143128
@property
144129
def enable_webextensions(self) -> bool:
145-
"""
146-
Returns:
147-
Whether webextension support is enabled for Chromium-based browsers.
148-
True if webextension support is enabled, False otherwise.
149-
"""
130+
"""Return whether webextension support is enabled for Chromium-based browsers."""
150131
return self._enable_webextensions
151132

152133
@enable_webextensions.setter

py/selenium/webdriver/chromium/service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424

2525

2626
class ChromiumService(service.Service):
27-
"""A Service class that is responsible for the starting and stopping the
28-
WebDriver instance of the ChromiumDriver.
27+
"""Service class responsible for starting and stopping the ChromiumDriver WebDriver instance.
2928
3029
Args:
3130
executable_path: Install path of the executable.
@@ -76,6 +75,7 @@ def command_line_args(self) -> list[str]:
7675

7776
@property
7877
def service_args(self) -> Sequence[str]:
78+
"""Returns the sequence of service arguments."""
7979
return self._service_args
8080

8181
@service_args.setter

py/selenium/webdriver/chromium/webdriver.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626

2727

2828
class ChromiumDriver(RemoteWebDriver):
29-
"""Controls the WebDriver instance of ChromiumDriver and allows you to
30-
drive the browser.
31-
"""
29+
"""Control the WebDriver instance of ChromiumDriver and drive the browser."""
3230

3331
def __init__(
3432
self,
@@ -38,8 +36,7 @@ def __init__(
3836
service: Optional[ChromiumService] = None,
3937
keep_alive: bool = True,
4038
) -> None:
41-
"""Creates a new WebDriver instance of the ChromiumDriver. Starts the
42-
service and then creates new WebDriver instance of ChromiumDriver.
39+
"""Create a new WebDriver instance, start the service, and create new ChromiumDriver instance.
4340
4441
Args:
4542
browser_name: Browser name used when matching capabilities.
@@ -148,17 +145,11 @@ def execute_cdp_cmd(self, cmd: str, cmd_args: dict):
148145
return super().execute_cdp_cmd(cmd, cmd_args)
149146

150147
def get_sinks(self) -> list:
151-
"""
152-
Returns:
153-
A list of sinks available for Cast.
154-
"""
148+
"""Get a list of sinks available for Cast."""
155149
return self.execute("getSinks")["value"]
156150

157151
def get_issue_message(self):
158-
"""
159-
Returns:
160-
An error message when there is any issue in a Cast session.
161-
"""
152+
"""Returns an error message when there is any issue in a Cast session."""
162153
return self.execute("getIssueMessage")["value"]
163154

164155
@property
@@ -186,8 +177,7 @@ def get_log(self, log_type):
186177
return self.execute(Command.GET_LOG, {"type": log_type})["value"]
187178

188179
def set_sink_to_use(self, sink_name: str) -> dict:
189-
"""Sets a specific sink, using its name, as a Cast session receiver
190-
target.
180+
"""Set a specific sink as a Cast session receiver target.
191181
192182
Args:
193183
sink_name: Name of the sink to use as the target.

0 commit comments

Comments
 (0)