]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Lint and format Tools/build/check-warnings.py (#124382)
authorHugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Mon, 23 Sep 2024 23:47:05 +0000 (16:47 -0700)
committerGitHub <noreply@github.com>
Mon, 23 Sep 2024 23:47:05 +0000 (02:47 +0300)
.pre-commit-config.yaml
Tools/build/.ruff.toml [new file with mode: 0644]
Tools/build/check_warnings.py

index 4cdb7545454589623e21fbbb1b8fbd73186bed96..891934bc70a64fb972e56ce1f42655ff7d1a0c2c 100644 (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/
diff --git a/Tools/build/.ruff.toml b/Tools/build/.ruff.toml
new file mode 100644 (file)
index 0000000..53bfe18
--- /dev/null
@@ -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
+]
index 7210cc8365ead6aa94a45f41e2f604e3b8f42ceb..7a8721087b6c66ff0d87f2baa6f3e7402ba98e5a 100644 (file)
@@ -4,9 +4,9 @@ exist only in files that are expected to have warnings.
 """
 
 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: