Skip to content

Commit e256a75

Browse files
authored
Lint and format Tools/build/check-warnings.py (#124382)
1 parent 38a887d commit e256a75

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

.pre-commit-config.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ repos:
1010
name: Run Ruff (lint) on Lib/test/
1111
args: [--exit-non-zero-on-fix]
1212
files: ^Lib/test/
13+
- id: ruff
14+
name: Run Ruff (lint) on Tools/build/check_warnings.py
15+
args: [--exit-non-zero-on-fix, --config=Tools/build/.ruff.toml]
16+
files: ^Tools/build/check_warnings.py
1317
- id: ruff
1418
name: Run Ruff (lint) on Argument Clinic
1519
args: [--exit-non-zero-on-fix, --config=Tools/clinic/.ruff.toml]
@@ -22,6 +26,11 @@ repos:
2226
- repo: https://github.com/psf/black-pre-commit-mirror
2327
rev: 24.8.0
2428
hooks:
29+
- id: black
30+
name: Run Black on Tools/build/check_warnings.py
31+
files: ^Tools/build/check_warnings.py
32+
language_version: python3.12
33+
args: [--line-length=79]
2534
- id: black
2635
name: Run Black on Tools/jit/
2736
files: ^Tools/jit/

Tools/build/.ruff.toml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
target-version = "py310"
2+
fix = true
3+
line-length = 79
4+
5+
[lint]
6+
select = [
7+
"C4", # flake8-comprehensions
8+
"E", # pycodestyle
9+
"F", # pyflakes
10+
"I", # isort
11+
"ISC", # flake8-implicit-str-concat
12+
"LOG", # flake8-logging
13+
"PGH", # pygrep-hooks
14+
"PT", # flake8-pytest-style
15+
"PYI", # flake8-pyi
16+
"RUF100", # Ban unused `# noqa` comments
17+
"UP", # pyupgrade
18+
"W", # pycodestyle
19+
"YTT", # flake8-2020
20+
]

Tools/build/check_warnings.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"""
55

66
import argparse
7-
from collections import defaultdict
87
import re
98
import sys
9+
from collections import defaultdict
1010
from pathlib import Path
1111
from typing import NamedTuple
1212

@@ -38,7 +38,8 @@ def parse_warning_ignore_file(file_path: str) -> set[IgnoreRule]:
3838
# Directories must have a wildcard count
3939
if is_directory and count != "*":
4040
print(
41-
f"Error parsing ignore file: {file_path} at line: {i}"
41+
f"Error parsing ignore file: {file_path} "
42+
f"at line: {i}"
4243
)
4344
print(
4445
f"Directory {file_name} must have count set to *"
@@ -93,9 +94,10 @@ def extract_warnings_from_compiler_output(
9394
.rstrip("]"),
9495
}
9596
)
96-
except:
97+
except AttributeError:
9798
print(
98-
f"Error parsing compiler output. Unable to extract warning on line {i}:\n{line}"
99+
f"Error parsing compiler output. "
100+
f"Unable to extract warning on line {i}:\n{line}"
99101
)
100102
sys.exit(1)
101103

@@ -125,8 +127,9 @@ def get_warnings_by_file(warnings: list[dict]) -> dict[str, list[dict]]:
125127
def is_file_ignored(
126128
file_path: str, ignore_rules: set[IgnoreRule]
127129
) -> IgnoreRule | None:
128-
"""
129-
Returns the IgnoreRule object for the file path if there is a related rule for it
130+
"""Return the IgnoreRule object for the file path.
131+
132+
Return ``None`` if there is no related rule for that path.
130133
"""
131134
for rule in ignore_rules:
132135
if rule.is_directory:
@@ -191,7 +194,10 @@ def get_unexpected_improvements(
191194
"""
192195
unexpected_improvements = []
193196
for rule in ignore_rules:
194-
if not rule.ignore_all and rule.file_path not in files_with_warnings.keys():
197+
if (
198+
not rule.ignore_all
199+
and rule.file_path not in files_with_warnings.keys()
200+
):
195201
if rule.file_path not in files_with_warnings.keys():
196202
unexpected_improvements.append((rule.file_path, rule.count, 0))
197203
elif len(files_with_warnings[rule.file_path]) < rule.count:

0 commit comments

Comments
 (0)