Lint and format Tools/build/check-warnings.py (#124382)

This commit is contained in:
Hugo van Kemenade 2024-09-23 16:47:05 -07:00 committed by GitHub
parent 38a887dc3e
commit e256a7590a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 7 deletions

View File

@ -10,6 +10,10 @@ repos:
name: Run Ruff (lint) on Lib/test/
args: [--exit-non-zero-on-fix]
files: ^Lib/test/
- id: ruff
name: Run Ruff (lint) on Tools/build/check_warnings.py
args: [--exit-non-zero-on-fix, --config=Tools/build/.ruff.toml]
files: ^Tools/build/check_warnings.py
- id: ruff
name: Run Ruff (lint) on Argument Clinic
args: [--exit-non-zero-on-fix, --config=Tools/clinic/.ruff.toml]
@ -22,6 +26,11 @@ repos:
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.8.0
hooks:
- id: black
name: Run Black on Tools/build/check_warnings.py
files: ^Tools/build/check_warnings.py
language_version: python3.12
args: [--line-length=79]
- id: black
name: Run Black on Tools/jit/
files: ^Tools/jit/

20
Tools/build/.ruff.toml Normal file
View File

@ -0,0 +1,20 @@
target-version = "py310"
fix = true
line-length = 79
[lint]
select = [
"C4", # flake8-comprehensions
"E", # pycodestyle
"F", # pyflakes
"I", # isort
"ISC", # flake8-implicit-str-concat
"LOG", # flake8-logging
"PGH", # pygrep-hooks
"PT", # flake8-pytest-style
"PYI", # flake8-pyi
"RUF100", # Ban unused `# noqa` comments
"UP", # pyupgrade
"W", # pycodestyle
"YTT", # flake8-2020
]

View File

@ -4,9 +4,9 @@
"""
import argparse
from collections import defaultdict
import re
import sys
from collections import defaultdict
from pathlib import Path
from typing import NamedTuple
@ -38,7 +38,8 @@ def parse_warning_ignore_file(file_path: str) -> set[IgnoreRule]:
# Directories must have a wildcard count
if is_directory and count != "*":
print(
f"Error parsing ignore file: {file_path} at line: {i}"
f"Error parsing ignore file: {file_path} "
f"at line: {i}"
)
print(
f"Directory {file_name} must have count set to *"
@ -93,9 +94,10 @@ def extract_warnings_from_compiler_output(
.rstrip("]"),
}
)
except:
except AttributeError:
print(
f"Error parsing compiler output. Unable to extract warning on line {i}:\n{line}"
f"Error parsing compiler output. "
f"Unable to extract warning on line {i}:\n{line}"
)
sys.exit(1)
@ -125,8 +127,9 @@ def get_warnings_by_file(warnings: list[dict]) -> dict[str, list[dict]]:
def is_file_ignored(
file_path: str, ignore_rules: set[IgnoreRule]
) -> IgnoreRule | None:
"""
Returns the IgnoreRule object for the file path if there is a related rule for it
"""Return the IgnoreRule object for the file path.
Return ``None`` if there is no related rule for that path.
"""
for rule in ignore_rules:
if rule.is_directory:
@ -191,7 +194,10 @@ def get_unexpected_improvements(
"""
unexpected_improvements = []
for rule in ignore_rules:
if not rule.ignore_all and rule.file_path not in files_with_warnings.keys():
if (
not rule.ignore_all
and rule.file_path not in files_with_warnings.keys()
):
if rule.file_path not in files_with_warnings.keys():
unexpected_improvements.append((rule.file_path, rule.count, 0))
elif len(files_with_warnings[rule.file_path]) < rule.count: