Skip to content

Commit

Permalink
Lint and format Tools/build/check-warnings.py (python#124382)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Sep 23, 2024
1 parent 38a887d commit e256a75
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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/
Expand Down
20 changes: 20 additions & 0 deletions Tools/build/.ruff.toml
Original file line number Diff line number Diff line change
@@ -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
]
20 changes: 13 additions & 7 deletions Tools/build/check_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 *"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit e256a75

Please sign in to comment.