Skip to content

Commit b3fc1b3

Browse files
cgoldbergp0deje
andauthored
[py] Use ruff for linting and code formatting (#15746)
* Replaces the current Python linters (black, isort, docformatter, autoflake, flake8) with ruff * Integrates it into the build system * Formats all Python source files --------- Co-authored-by: Alex Rodionov <[email protected]>
1 parent 1cad0ca commit b3fc1b3

File tree

107 files changed

+793
-889
lines changed

Some content is hidden

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

107 files changed

+793
-889
lines changed

.github/workflows/ci-python.yml

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,6 @@ jobs:
3333
env:
3434
TOXENV: docs
3535

36-
lint:
37-
name: Lint
38-
needs: build
39-
runs-on: ubuntu-latest
40-
steps:
41-
- name: Checkout source tree
42-
uses: actions/checkout@v4
43-
- name: Set up Python 3.9
44-
uses: actions/setup-python@v4
45-
with:
46-
python-version: 3.9
47-
- name: Install dependencies
48-
run: |
49-
python -m pip install --upgrade pip
50-
pip install tox==4.25.0
51-
- name: Test with tox
52-
run: tox -c py/tox.ini
53-
env:
54-
# If this fails, it will exit. Local work should be using `tox -e linting` prior to committing.
55-
# the linting-ci recipe exists solely for CI and will not attempt to rewrite any files in-place etc.
56-
TOXENV: linting-ci
57-
5836
mypy:
5937
name: Mypy
6038
needs: build

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module(name = "selenium")
22

33
bazel_dep(name = "apple_rules_lint", version = "0.4.0")
4+
bazel_dep(name = "aspect_rules_lint", version = "1.4.2")
45
bazel_dep(name = "aspect_bazel_lib", version = "2.13.0")
56
bazel_dep(name = "aspect_rules_esbuild", version = "0.21.0")
67
bazel_dep(name = "aspect_rules_js", version = "2.1.3")

README.md

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,11 @@ To automatically update and pin new dependencies, run:
217217

218218
### Python
219219

220-
#### Linting
220+
#### Linting and Formatting
221221

222222
We follow the [PEP8 Style Guide for Python Code](https://peps.python.org/pep-0008) (except we use a 120 character line length).
223-
This is checked and enforced with several linting tools, including
224-
[black](https://pypi.org/project/black),
225-
[docformatter](https://pypi.org/project/docformatter),
226-
[flake8](https://flake8.pycqa.org),
227-
and [isort](https://pycqa.github.io/isort).
228-
229-
To run all of the linting tools:
230-
```shell
231-
./go py:lint
232-
```
223+
This is checked and enforced with [ruff](https://docs.astral.sh/ruff/), a linting/formatting tool.
224+
There is also an auto-formatting script that can be run: `./scripts/format.sh`
233225

234226
#### Local Installation
235227

Rakefile

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -669,11 +669,6 @@ namespace :py do
669669
@git.add(conf)
670670
end
671671

672-
desc 'Update Python Syntax'
673-
task :lint do
674-
sh 'tox -c py/tox.ini -e linting'
675-
end
676-
677672
namespace :test do
678673
desc 'Python unit tests'
679674
task :unit do
@@ -1165,7 +1160,6 @@ namespace :all do
11651160
task :lint do
11661161
ext = /mswin|msys|mingw|cygwin|bccwin|wince|emc/.match?(RbConfig::CONFIG['host_os']) ? 'ps1' : 'sh'
11671162
sh "./scripts/format.#{ext}", verbose: true
1168-
Rake::Task['py:lint'].invoke
11691163
end
11701164

11711165
# Example: `./go all:prepare 4.31.0 early-stable`

common/devtools/convert_protocol_to_json.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@
1010

1111
import pdl
1212

13+
1314
def main(argv):
14-
parser = argparse.ArgumentParser(description=(
15-
"Converts from .pdl to .json by invoking the pdl Python module."))
16-
parser.add_argument('--map_binary_to_string', type=bool,
17-
help=('If set, binary in the .pdl is mapped to a '
18-
'string in .json. Client code will have to '
19-
'base64 decode the string to get the payload.'))
15+
parser = argparse.ArgumentParser(
16+
description=("Converts from .pdl to .json by invoking the pdl Python module.")
17+
)
18+
parser.add_argument(
19+
"--map_binary_to_string",
20+
type=bool,
21+
help=(
22+
"If set, binary in the .pdl is mapped to a "
23+
"string in .json. Client code will have to "
24+
"base64 decode the string to get the payload."
25+
),
26+
)
2027
parser.add_argument("pdl_file", help="The .pdl input file to parse.")
2128
parser.add_argument("json_file", help="The .json output file write.")
2229
args = parser.parse_args(argv)
@@ -26,10 +33,10 @@ def main(argv):
2633
protocol = pdl.loads(pdl_string, file_name, args.map_binary_to_string)
2734
input_file.close()
2835

29-
output_file = open(os.path.normpath(args.json_file), 'w')
30-
json.dump(protocol, output_file, indent=4, separators=(',', ': '))
36+
output_file = open(os.path.normpath(args.json_file), "w")
37+
json.dump(protocol, output_file, indent=4, separators=(",", ": "))
3138
output_file.close()
3239

3340

34-
if __name__ == '__main__':
41+
if __name__ == "__main__":
3542
sys.exit(main(sys.argv[1:]))

common/devtools/pdl.py

Lines changed: 74 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,72 @@
88
import re
99
import sys
1010

11-
description = ''
11+
description = ""
1212

1313

14-
primitiveTypes = ['integer', 'number', 'boolean', 'string', 'object',
15-
'any', 'array', 'binary']
14+
primitiveTypes = [
15+
"integer",
16+
"number",
17+
"boolean",
18+
"string",
19+
"object",
20+
"any",
21+
"array",
22+
"binary",
23+
]
1624

1725

1826
def assignType(item, type, is_array=False, map_binary_to_string=False):
1927
if is_array:
20-
item['type'] = 'array'
21-
item['items'] = collections.OrderedDict()
22-
assignType(item['items'], type, False, map_binary_to_string)
28+
item["type"] = "array"
29+
item["items"] = collections.OrderedDict()
30+
assignType(item["items"], type, False, map_binary_to_string)
2331
return
2432

25-
if type == 'enum':
26-
type = 'string'
27-
if map_binary_to_string and type == 'binary':
28-
type = 'string'
33+
if type == "enum":
34+
type = "string"
35+
if map_binary_to_string and type == "binary":
36+
type = "string"
2937
if type in primitiveTypes:
30-
item['type'] = type
38+
item["type"] = type
3139
else:
32-
item['$ref'] = type
40+
item["$ref"] = type
3341

3442

3543
def createItem(d, experimental, deprecated, name=None):
3644
result = collections.OrderedDict(d)
3745
if name:
38-
result['name'] = name
46+
result["name"] = name
3947
global description
4048
if description:
41-
result['description'] = description.strip()
49+
result["description"] = description.strip()
4250
if experimental:
43-
result['experimental'] = True
51+
result["experimental"] = True
4452
if deprecated:
45-
result['deprecated'] = True
53+
result["deprecated"] = True
4654
return result
4755

4856

4957
def parse(data, file_name, map_binary_to_string=False):
5058
protocol = collections.OrderedDict()
51-
protocol['version'] = collections.OrderedDict()
52-
protocol['domains'] = []
59+
protocol["version"] = collections.OrderedDict()
60+
protocol["domains"] = []
5361
domain = None
5462
item = None
5563
subitems = None
5664
nukeDescription = False
5765
global description
58-
lines = data.split('\n')
66+
lines = data.split("\n")
5967
for i in range(0, len(lines)):
6068
if nukeDescription:
61-
description = ''
69+
description = ""
6270
nukeDescription = False
6371
line = lines[i]
6472
trimLine = line.strip()
6573

66-
if trimLine.startswith('#'):
74+
if trimLine.startswith("#"):
6775
if len(description):
68-
description += '\n'
76+
description += "\n"
6977
description += trimLine[2:]
7078
continue
7179
else:
@@ -74,99 +82,103 @@ def parse(data, file_name, map_binary_to_string=False):
7482
if len(trimLine) == 0:
7583
continue
7684

77-
match = re.compile(
78-
r'^(experimental )?(deprecated )?domain (.*)').match(line)
85+
match = re.compile(r"^(experimental )?(deprecated )?domain (.*)").match(line)
7986
if match:
80-
domain = createItem({'domain' : match.group(3)}, match.group(1),
81-
match.group(2))
82-
protocol['domains'].append(domain)
87+
domain = createItem(
88+
{"domain": match.group(3)}, match.group(1), match.group(2)
89+
)
90+
protocol["domains"].append(domain)
8391
continue
8492

85-
match = re.compile(r'^ depends on ([^\s]+)').match(line)
93+
match = re.compile(r"^ depends on ([^\s]+)").match(line)
8694
if match:
87-
if 'dependencies' not in domain:
88-
domain['dependencies'] = []
89-
domain['dependencies'].append(match.group(1))
95+
if "dependencies" not in domain:
96+
domain["dependencies"] = []
97+
domain["dependencies"].append(match.group(1))
9098
continue
9199

92-
match = re.compile(r'^ (experimental )?(deprecated )?type (.*) '
93-
r'extends (array of )?([^\s]+)').match(line)
100+
match = re.compile(
101+
r"^ (experimental )?(deprecated )?type (.*) "
102+
r"extends (array of )?([^\s]+)"
103+
).match(line)
94104
if match:
95-
if 'types' not in domain:
96-
domain['types'] = []
97-
item = createItem({'id': match.group(3)}, match.group(1), match.group(2))
105+
if "types" not in domain:
106+
domain["types"] = []
107+
item = createItem({"id": match.group(3)}, match.group(1), match.group(2))
98108
assignType(item, match.group(5), match.group(4), map_binary_to_string)
99-
domain['types'].append(item)
109+
domain["types"].append(item)
100110
continue
101111

102112
match = re.compile(
103-
r'^ (experimental )?(deprecated )?(command|event) (.*)').match(line)
113+
r"^ (experimental )?(deprecated )?(command|event) (.*)"
114+
).match(line)
104115
if match:
105116
list = []
106-
if match.group(3) == 'command':
107-
if 'commands' in domain:
108-
list = domain['commands']
117+
if match.group(3) == "command":
118+
if "commands" in domain:
119+
list = domain["commands"]
109120
else:
110-
list = domain['commands'] = []
121+
list = domain["commands"] = []
111122
else:
112-
if 'events' in domain:
113-
list = domain['events']
123+
if "events" in domain:
124+
list = domain["events"]
114125
else:
115-
list = domain['events'] = []
126+
list = domain["events"] = []
116127

117128
item = createItem({}, match.group(1), match.group(2), match.group(4))
118129
list.append(item)
119130
continue
120131

121132
match = re.compile(
122-
r'^ (experimental )?(deprecated )?(optional )?'
123-
r'(array of )?([^\s]+) ([^\s]+)').match(line)
133+
r"^ (experimental )?(deprecated )?(optional )?"
134+
r"(array of )?([^\s]+) ([^\s]+)"
135+
).match(line)
124136
if match:
125137
param = createItem({}, match.group(1), match.group(2), match.group(6))
126138
if match.group(3):
127-
param['optional'] = True
139+
param["optional"] = True
128140
assignType(param, match.group(5), match.group(4), map_binary_to_string)
129-
if match.group(5) == 'enum':
130-
enumliterals = param['enum'] = []
141+
if match.group(5) == "enum":
142+
enumliterals = param["enum"] = []
131143
subitems.append(param)
132144
continue
133145

134-
match = re.compile(r'^ (parameters|returns|properties)').match(line)
146+
match = re.compile(r"^ (parameters|returns|properties)").match(line)
135147
if match:
136148
subitems = item[match.group(1)] = []
137149
continue
138150

139-
match = re.compile(r'^ enum').match(line)
151+
match = re.compile(r"^ enum").match(line)
140152
if match:
141-
enumliterals = item['enum'] = []
153+
enumliterals = item["enum"] = []
142154
continue
143155

144-
match = re.compile(r'^version').match(line)
156+
match = re.compile(r"^version").match(line)
145157
if match:
146158
continue
147159

148-
match = re.compile(r'^ major (\d+)').match(line)
160+
match = re.compile(r"^ major (\d+)").match(line)
149161
if match:
150-
protocol['version']['major'] = match.group(1)
162+
protocol["version"]["major"] = match.group(1)
151163
continue
152164

153-
match = re.compile(r'^ minor (\d+)').match(line)
165+
match = re.compile(r"^ minor (\d+)").match(line)
154166
if match:
155-
protocol['version']['minor'] = match.group(1)
167+
protocol["version"]["minor"] = match.group(1)
156168
continue
157169

158-
match = re.compile(r'^ redirect ([^\s]+)').match(line)
170+
match = re.compile(r"^ redirect ([^\s]+)").match(line)
159171
if match:
160-
item['redirect'] = match.group(1)
172+
item["redirect"] = match.group(1)
161173
continue
162174

163-
match = re.compile(r'^ ( )?[^\s]+$').match(line)
175+
match = re.compile(r"^ ( )?[^\s]+$").match(line)
164176
if match:
165177
# enum literal
166178
enumliterals.append(trimLine)
167179
continue
168180

169-
print('Error in %s:%s, illegal token: \t%s' % (file_name, i, line))
181+
print("Error in %s:%s, illegal token: \t%s" % (file_name, i, line))
170182
sys.exit(1)
171183
return protocol
172184

javascript/grid-ui/scripts/rmSourcemaps.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
BUILD_DIR = "build"
44

55
for current, dirs, files in os.walk(BUILD_DIR):
6-
for file in files:
7-
if file.endswith('.map'):
8-
# remove the source map
9-
os.remove(os.path.join(current, file))
6+
for file in files:
7+
if file.endswith(".map"):
8+
# remove the source map
9+
os.remove(os.path.join(current, file))

0 commit comments

Comments
 (0)