Skip to content

Commit 5d8e432

Browse files
picnixzAA-Turner
andauthored
gh-132390: Apply Ruff linting to Tools/build (#132391)
--------- Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
1 parent 246ed23 commit 5d8e432

16 files changed

+109
-99
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ repos:
1111
args: [--exit-non-zero-on-fix]
1212
files: ^Lib/test/
1313
- id: ruff
14-
name: Run Ruff (lint) on Tools/build/check_warnings.py
14+
name: Run Ruff (lint) on Tools/build/
1515
args: [--exit-non-zero-on-fix, --config=Tools/build/.ruff.toml]
16-
files: ^Tools/build/check_warnings.py
16+
files: ^Tools/build/
1717
- id: ruff
1818
name: Run Ruff (lint) on Argument Clinic
1919
args: [--exit-non-zero-on-fix, --config=Tools/clinic/.ruff.toml]

Tools/build/.ruff.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,22 @@ select = [
1818
"W", # pycodestyle
1919
"YTT", # flake8-2020
2020
]
21+
ignore = [
22+
"E501", # Line too long
23+
"F541", # f-string without any placeholders
24+
"PYI024", # Use `typing.NamedTuple` instead of `collections.namedtuple`
25+
"PYI025", # Use `from collections.abc import Set as AbstractSet`
26+
"UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)`
27+
]
28+
29+
[per-file-target-version]
30+
"deepfreeze.py" = "py310"
31+
"stable_abi.py" = "py311" # requires 'tomllib'
32+
33+
[lint.per-file-ignores]
34+
"{check_extension_modules,freeze_modules}.py" = [
35+
"UP031", # Use format specifiers instead of percent format
36+
]
37+
"generate_{re_casefix,sre_constants,token}.py" = [
38+
"UP031", # Use format specifiers instead of percent format
39+
]

Tools/build/check_extension_modules.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
1818
See --help for more information
1919
"""
20+
import _imp
2021
import argparse
2122
import collections
2223
import enum
@@ -27,12 +28,14 @@
2728
import sys
2829
import sysconfig
2930
import warnings
30-
import _imp
31-
31+
from collections.abc import Iterable
3232
from importlib._bootstrap import _load as bootstrap_load
33-
from importlib.machinery import BuiltinImporter, ExtensionFileLoader, ModuleSpec
33+
from importlib.machinery import (
34+
BuiltinImporter,
35+
ExtensionFileLoader,
36+
ModuleSpec,
37+
)
3438
from importlib.util import spec_from_file_location, spec_from_loader
35-
from typing import Iterable
3639

3740
SRC_DIR = pathlib.Path(__file__).parent.parent.parent
3841

@@ -195,7 +198,7 @@ def print_three_column(modinfos: list[ModuleInfo]):
195198
# guarantee zip() doesn't drop anything
196199
while len(names) % 3:
197200
names.append("")
198-
for l, m, r in zip(names[::3], names[1::3], names[2::3]):
201+
for l, m, r in zip(names[::3], names[1::3], names[2::3]): # noqa: E741
199202
print("%-*s %-*s %-*s" % (longest, l, longest, m, longest, r))
200203

201204
if verbose and self.builtin_ok:
@@ -420,7 +423,7 @@ def check_module_import(self, modinfo: ModuleInfo):
420423
except ImportError as e:
421424
logger.error("%s failed to import: %s", modinfo.name, e)
422425
raise
423-
except Exception as e:
426+
except Exception:
424427
if not hasattr(_imp, 'create_dynamic'):
425428
logger.warning("Dynamic extension '%s' ignored", modinfo.name)
426429
return

Tools/build/deepfreeze.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import re
1414
import time
1515
import types
16-
from typing import Dict, FrozenSet, TextIO, Tuple
16+
from typing import TextIO
1717

1818
import umarshal
1919

@@ -57,8 +57,8 @@ def get_localsplus(code: types.CodeType):
5757

5858

5959
def get_localsplus_counts(code: types.CodeType,
60-
names: Tuple[str, ...],
61-
kinds: bytes) -> Tuple[int, int, int, int]:
60+
names: tuple[str, ...],
61+
kinds: bytes) -> tuple[int, int, int]:
6262
nlocals = 0
6363
ncellvars = 0
6464
nfreevars = 0
@@ -84,7 +84,7 @@ def get_localsplus_counts(code: types.CodeType,
8484
PyUnicode_4BYTE_KIND = 4
8585

8686

87-
def analyze_character_width(s: str) -> Tuple[int, bool]:
87+
def analyze_character_width(s: str) -> tuple[int, bool]:
8888
maxchar = ' '
8989
for c in s:
9090
maxchar = max(maxchar, c)
@@ -109,7 +109,7 @@ class Printer:
109109
def __init__(self, file: TextIO) -> None:
110110
self.level = 0
111111
self.file = file
112-
self.cache: Dict[tuple[type, object, str], str] = {}
112+
self.cache: dict[tuple[type, object, str], str] = {}
113113
self.hits, self.misses = 0, 0
114114
self.finis: list[str] = []
115115
self.inits: list[str] = []
@@ -305,7 +305,7 @@ def generate_code(self, name: str, code: types.CodeType) -> str:
305305
self.inits.append(f"_PyStaticCode_Init({name_as_code})")
306306
return f"& {name}.ob_base.ob_base"
307307

308-
def generate_tuple(self, name: str, t: Tuple[object, ...]) -> str:
308+
def generate_tuple(self, name: str, t: tuple[object, ...]) -> str:
309309
if len(t) == 0:
310310
return f"(PyObject *)& _Py_SINGLETON(tuple_empty)"
311311
items = [self.generate(f"{name}_{i}", it) for i, it in enumerate(t)]
@@ -379,7 +379,7 @@ def generate_complex(self, name: str, z: complex) -> str:
379379
self.write(f".cval = {{ {z.real}, {z.imag} }},")
380380
return f"&{name}.ob_base"
381381

382-
def generate_frozenset(self, name: str, fs: FrozenSet[object]) -> str:
382+
def generate_frozenset(self, name: str, fs: frozenset[object]) -> str:
383383
try:
384384
fs = sorted(fs)
385385
except TypeError:
@@ -465,7 +465,7 @@ def generate(args: list[str], output: TextIO) -> None:
465465
printer = Printer(output)
466466
for arg in args:
467467
file, modname = arg.rsplit(':', 1)
468-
with open(file, "r", encoding="utf8") as fd:
468+
with open(file, encoding="utf8") as fd:
469469
source = fd.read()
470470
if is_frozen_header(source):
471471
code = decode_frozen_data(source)
@@ -513,7 +513,7 @@ def main() -> None:
513513
if args.file:
514514
if verbose:
515515
print(f"Reading targets from {args.file}")
516-
with open(args.file, "rt", encoding="utf-8-sig") as fin:
516+
with open(args.file, encoding="utf-8-sig") as fin:
517517
rules = [x.strip() for x in fin]
518518
else:
519519
rules = args.args

Tools/build/freeze_modules.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
See the notes at the top of Python/frozen.c for more info.
44
"""
55

6-
from collections import namedtuple
76
import hashlib
87
import ntpath
98
import os
109
import posixpath
10+
from collections import namedtuple
1111

1212
from update_file import updating_file_with_tmpfile
1313

14-
1514
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
1615
ROOT_DIR = os.path.abspath(ROOT_DIR)
1716
FROZEN_ONLY = os.path.join(ROOT_DIR, 'Tools', 'freeze', 'flag.py')
@@ -482,7 +481,6 @@ def regen_frozen(modules):
482481
header = relpath_for_posix_display(src.frozenfile, parentdir)
483482
headerlines.append(f'#include "{header}"')
484483

485-
externlines = UniqueList()
486484
bootstraplines = []
487485
stdliblines = []
488486
testlines = []
@@ -625,7 +623,6 @@ def regen_makefile(modules):
625623
def regen_pcbuild(modules):
626624
projlines = []
627625
filterlines = []
628-
corelines = []
629626
for src in _iter_sources(modules):
630627
pyfile = relpath_for_windows_display(src.pyfile, ROOT_DIR)
631628
header = relpath_for_windows_display(src.frozenfile, ROOT_DIR)

Tools/build/generate_global_objects.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ def generate_runtime_init(identifiers, strings):
286286
break
287287
else:
288288
raise NotImplementedError
289-
assert nsmallposints and nsmallnegints
289+
assert nsmallposints
290+
assert nsmallnegints
290291

291292
# Then target the runtime initializer.
292293
filename = os.path.join(INTERNAL, 'pycore_runtime_init_generated.h')
@@ -434,7 +435,7 @@ def get_identifiers_and_strings() -> 'tuple[set[str], dict[str, str]]':
434435
# To cover tricky cases (like "\n") we also generate C asserts.
435436
raise ValueError(
436437
'do not use &_Py_ID or &_Py_STR for one-character latin-1 '
437-
+ f'strings, use _Py_LATIN1_CHR instead: {string!r}')
438+
f'strings, use _Py_LATIN1_CHR instead: {string!r}')
438439
if string not in strings:
439440
strings[string] = name
440441
elif name != strings[string]:

Tools/build/generate_levenshtein_examples.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
"""Generate 10,000 unique examples for the Levenshtein short-circuit tests."""
22

33
import argparse
4-
from functools import lru_cache
54
import json
65
import os.path
6+
from functools import lru_cache
77
from random import choices, randrange
88

9-
109
# This should be in sync with Lib/traceback.py. It's not importing those values
1110
# because this script is being executed by PYTHON_FOR_REGEN and not by the in-tree
1211
# build of Python.

Tools/build/generate_re_casefix.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
def update_file(file, content):
1111
try:
12-
with open(file, 'r', encoding='utf-8') as fobj:
12+
with open(file, encoding='utf-8') as fobj:
1313
if fobj.read() == content:
1414
return False
1515
except (OSError, ValueError):
@@ -50,7 +50,7 @@ def main(outfile='Lib/re/_casefix.py'):
5050
# List of codes of lowercased characters which have the same uppercase.
5151
equivalent_lower_codes = [sorted(t)
5252
for s in equivalent_chars
53-
for t in [set(ord(c.lower()) for c in s)]
53+
for t in [{ord(c.lower()) for c in s}]
5454
if len(t) > 1]
5555

5656
bad_codes = []

Tools/build/generate_sbom.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
"""Tool for generating Software Bill of Materials (SBOM) for Python's dependencies"""
2-
import os
3-
import re
2+
3+
import glob
44
import hashlib
55
import json
6-
import glob
7-
from pathlib import Path, PurePosixPath, PureWindowsPath
6+
import os
7+
import re
88
import subprocess
99
import sys
10-
import urllib.request
1110
import typing
11+
import urllib.request
12+
from pathlib import Path, PurePosixPath, PureWindowsPath
1213

1314
CPYTHON_ROOT_DIR = Path(__file__).parent.parent.parent
1415

@@ -250,7 +251,7 @@ def check_sbom_packages(sbom_data: dict[str, typing.Any]) -> None:
250251
license_concluded = package["licenseConcluded"]
251252
error_if(
252253
license_concluded != "NOASSERTION",
253-
f"License identifier must be 'NOASSERTION'"
254+
"License identifier must be 'NOASSERTION'"
254255
)
255256

256257

Tools/build/generate_sre_constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
def update_file(file, content):
88
try:
9-
with open(file, 'r') as fobj:
9+
with open(file) as fobj:
1010
if fobj.read() == content:
1111
return False
1212
except (OSError, ValueError):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy