Skip to content

Commit aa2bdb9

Browse files
authored
[py] Add autoflake linter and update linting dependencies (#15643)
1 parent 4243248 commit aa2bdb9

File tree

4 files changed

+66
-47
lines changed

4 files changed

+66
-47
lines changed

py/conftest.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
import socket
2121
import subprocess
2222
import time
23-
from test.selenium.webdriver.common.network import get_lan_ip
24-
from test.selenium.webdriver.common.webserver import SimpleWebServer
2523
from urllib.request import urlopen
2624

2725
import pytest
2826

2927
from selenium import webdriver
28+
from test.selenium.webdriver.common.network import get_lan_ip
29+
from test.selenium.webdriver.common.webserver import SimpleWebServer
3030

3131
drivers = (
3232
"chrome",
@@ -97,7 +97,7 @@ def pytest_ignore_collect(path, config):
9797

9898

9999
def get_driver_class(driver_option):
100-
"""Generate the driver class name from the lowercase driver option"""
100+
"""Generate the driver class name from the lowercase driver option."""
101101
if driver_option == "webkitgtk":
102102
driver_class = "WebKitGTK"
103103
elif driver_option == "wpewebkit":
@@ -113,10 +113,8 @@ def get_driver_class(driver_option):
113113
@pytest.fixture(scope="function")
114114
def driver(request):
115115
kwargs = {}
116-
117116
# browser can be changed with `--driver=firefox` as an argument or to addopts in pytest.ini
118117
driver_class = get_driver_class(getattr(request, "param", "Chrome"))
119-
120118
# skip tests if not available on the platform
121119
_platform = platform.system()
122120
if driver_class == "Safari" and _platform != "Darwin":
@@ -125,12 +123,10 @@ def driver(request):
125123
pytest.skip("IE and EdgeHTML Tests can only run on Windows")
126124
if "WebKit" in driver_class and _platform == "Windows":
127125
pytest.skip("WebKit tests cannot be run on Windows")
128-
129126
# skip tests for drivers that don't support BiDi when --bidi is enabled
130127
if request.config.option.bidi:
131128
if driver_class in ("Ie", "Safari", "WebKitGTK", "WPEWebKit"):
132129
pytest.skip(f"{driver_class} does not support BiDi")
133-
134130
# conditionally mark tests as expected to fail based on driver
135131
marker = request.node.get_closest_marker(f"xfail_{driver_class.lower()}")
136132

@@ -182,7 +178,6 @@ def fin():
182178

183179
driver_instance = getattr(webdriver, driver_class)(**kwargs)
184180
yield driver_instance
185-
186181
# Close the browser after BiDi tests. Those make event subscriptions
187182
# and doesn't seems to be stable enough, causing the flakiness of the
188183
# subsequent tests.

py/pyproject.toml

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,36 @@ ignore_missing_imports = true
136136
# Automatically keep imports alphabetically sorted, on single lines in
137137
# PEP recommended sections (https://peps.python.org/pep-0008/#imports)
138138
# files or individual lines can be ignored via `# isort:skip|# isort:skip_file`.
139-
profile = "black"
140-
py_version=39
141139
force_single_line = true
140+
profile = "black"
141+
py_version = 39
142+
quiet = true
143+
skip = "selenium/webdriver/common/devtools"
142144

143145
[tool.black]
144-
extend-exclude = 'selenium/webdriver/common/devtools'
146+
extend-exclude = "selenium/webdriver/common/devtools"
145147
line-length = 120
146-
target-version = ['py39']
148+
target-version = ["py39"]
149+
150+
[tool.autoflake]
151+
exclude = "selenium/webdriver/common/devtools"
152+
ignore-pass-after-docstring = true
153+
in-place = true
154+
quiet = true
155+
recursive = true
156+
remove-all-unused-imports = true
157+
remove-duplicate-keys = true
158+
remove-unused-variables = true
159+
160+
[tool.flake8]
161+
exclude = "selenium/webdriver/common/devtools"
162+
# Disable E501 once line length is better handled
163+
extend-ignore = ["E501", "E203"]
164+
# This does nothing for now as E501 is ignored
165+
max-line-length = 120
166+
min-python-version = "3.9"
147167

148168
[tool.docformatter]
149-
exclude = 'selenium/webdriver/common/devtools'
169+
exclude = "selenium/webdriver/common/devtools"
170+
in-place = true
150171
recursive = true

py/selenium/webdriver/support/expected_conditions.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def _predicate(driver: WebDriver):
195195

196196

197197
def visibility_of_element_located(
198-
locator: Tuple[str, str]
198+
locator: Tuple[str, str],
199199
) -> Callable[[WebDriverOrWebElement], Union[Literal[False], WebElement]]:
200200
"""An expectation for checking that an element is present on the DOM of a
201201
page and visible. Visibility means that the element is not only displayed
@@ -337,7 +337,7 @@ def _predicate(driver: WebDriverOrWebElement):
337337

338338

339339
def visibility_of_all_elements_located(
340-
locator: Tuple[str, str]
340+
locator: Tuple[str, str],
341341
) -> Callable[[WebDriverOrWebElement], Union[List[WebElement], Literal[False]]]:
342342
"""An expectation for checking that all elements are present on the DOM of
343343
a page and visible. Visibility means that the elements are not only
@@ -486,7 +486,7 @@ def _predicate(driver: WebDriverOrWebElement):
486486

487487

488488
def frame_to_be_available_and_switch_to_it(
489-
locator: Union[Tuple[str, str], str, WebElement]
489+
locator: Union[Tuple[str, str], str, WebElement],
490490
) -> Callable[[WebDriver], bool]:
491491
"""An expectation for checking whether the given frame is available to
492492
switch to.
@@ -527,7 +527,7 @@ def _predicate(driver: WebDriver):
527527

528528

529529
def invisibility_of_element_located(
530-
locator: Union[WebElement, Tuple[str, str]]
530+
locator: Union[WebElement, Tuple[str, str]],
531531
) -> Callable[[WebDriverOrWebElement], Union[WebElement, bool]]:
532532
"""An Expectation for checking that an element is either invisible or not
533533
present on the DOM.
@@ -576,7 +576,7 @@ def _predicate(driver: WebDriverOrWebElement):
576576

577577

578578
def invisibility_of_element(
579-
element: Union[WebElement, Tuple[str, str]]
579+
element: Union[WebElement, Tuple[str, str]],
580580
) -> Callable[[WebDriverOrWebElement], Union[WebElement, bool]]:
581581
"""An Expectation for checking that an element is either invisible or not
582582
present on the DOM.
@@ -602,7 +602,7 @@ def invisibility_of_element(
602602

603603

604604
def element_to_be_clickable(
605-
mark: Union[WebElement, Tuple[str, str]]
605+
mark: Union[WebElement, Tuple[str, str]],
606606
) -> Callable[[WebDriverOrWebElement], Union[Literal[False], WebElement]]:
607607
"""An Expectation for checking an element is visible and enabled such that
608608
you can click it.

py/tox.ini

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ envlist = docs, flake8, isort, validate-pyproject
44
[testenv:validate-pyproject]
55
skip_install = true
66
deps =
7-
validate-pyproject==0.23
8-
packaging==24.2
7+
validate-pyproject==0.24.1
8+
packaging==25.0
99
commands =
1010
validate-pyproject ./pyproject.toml
1111

@@ -33,42 +33,45 @@ deps =
3333
commands = mypy --install-types {posargs}
3434

3535
[testenv:linting-ci]
36-
; checks linting for CI with stricter exiting when failing.
36+
; checks linting for CI with stricter exiting when failing and no rewriting
3737
skip_install = true
3838
deps =
39-
isort==5.13.2
40-
black==24.1.1
41-
flake8==6.1.0
42-
flake8-typing-imports==1.14.0
39+
isort==6.0.1
40+
black==25.1.0
41+
autoflake==2.3.1
42+
flake8==7.1.2
43+
flake8-pyproject==1.2.3
44+
flake8-typing-imports==1.16.0
4345
docformatter==1.7.5
4446
commands =
45-
; execute isort in check only mode.
4647
isort --check-only --diff selenium/ test/ conftest.py
47-
; execute black in check only mode with diff.
48-
black --check --diff selenium/ test/ conftest.py -l 120
49-
flake8 selenium/ test/ --min-python-version=3.9
50-
docformatter --check -r selenium/
48+
black --check --diff selenium/ test/ conftest.py
49+
autoflake --check-diff selenium/ test/ conftest.py
50+
flake8 selenium/ test/ conftest.py
51+
docformatter --check --diff selenium/ test/ conftest.py
5152

5253
[testenv:linting]
5354
; A consolidated linting based recipe, responsible for executing linting tools across the code base.
54-
; This encompasses isort for imports, black for general formatting and flake8.
55-
; IMPORTANT: black & isort rewrite files, flake8 merely alerts to the failure.
55+
; This encompasses:
56+
; - isort for imports
57+
; - black and flake8 for general formatting
58+
; - autoflake for unused imports and variables
59+
; - docformatter for docstrings
60+
; IMPORTANT:
61+
; - isort, black, autoflake, docformatter: will rewrite files
62+
; - flake8: only alerts to the failures
5663
skip_install = true
5764
deps =
58-
isort==5.13.2
59-
black==24.1.1
60-
flake8==6.1.0
61-
flake8-typing-imports==1.14.0
65+
isort==6.0.1
66+
black==25.1.0
67+
autoflake==2.3.1
68+
flake8==7.1.2
69+
flake8-pyproject==1.2.3
70+
flake8-typing-imports==1.16.0
6271
docformatter==1.7.5
6372
commands =
6473
isort selenium/ test/ conftest.py
65-
black selenium/ test/ conftest.py -l 120
66-
flake8 selenium/ test/ --min-python-version=3.9
67-
docformatter --in-place -r selenium/
68-
69-
[flake8]
70-
exclude = .tox,selenium/webdriver/common/devtools,docs/source/conf.py,*venv
71-
# Disable this once black is applied throughout & line length is better handled.
72-
extend-ignore = E501, E203
73-
# This does nothing for now as E501 is ignored.
74-
max-line-length = 120
74+
black selenium/ test/ conftest.py
75+
autoflake selenium/ test/ conftest.py
76+
flake8 selenium/ test/ conftest.py
77+
docformatter selenium/ test/ conftest.py

0 commit comments

Comments
 (0)