Skip to content

Commit 08ec4a1

Browse files
AA-Turnerhugovk
andauthored
GH-109408: Move the Python file whitespace check from patchcheck to pre-commit (#109891)
Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent 982f1b7 commit 08ec4a1

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

.pre-commit-config.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ repos:
2323
- id: trailing-whitespace
2424
types_or: [c, inc, python, rst]
2525

26+
- repo: local
27+
hooks:
28+
- id: python-file-whitespace
29+
name: "Check Python file whitespace"
30+
entry: 'python Tools/patchcheck/reindent.py --nobackup --newline LF'
31+
language: 'system'
32+
types: [python]
33+
exclude: '^(Lib/test/tokenizedata/|Tools/c-analyzer/cpython/_parser).*$'
34+
2635
- repo: https://github.com/sphinx-contrib/sphinx-lint
2736
rev: v0.6.8
2837
hooks:

Tools/patchcheck/patchcheck.py

+7-25
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import subprocess
88
import sysconfig
99

10-
import reindent
1110
import untabify
1211

1312

@@ -184,21 +183,6 @@ def report_modified_files(file_paths):
184183
})
185184

186185

187-
@status("Fixing Python file whitespace", info=report_modified_files)
188-
def normalize_whitespace(file_paths):
189-
"""Make sure that the whitespace for .py files have been normalized."""
190-
reindent.makebackup = False # No need to create backups.
191-
fixed = [
192-
path for path in file_paths
193-
if (
194-
path.endswith('.py')
195-
and path not in _PYTHON_FILES_WITH_TABS
196-
and reindent.check(os.path.join(SRCDIR, path))
197-
)
198-
]
199-
return fixed
200-
201-
202186
@status("Fixing C file whitespace", info=report_modified_files)
203187
def normalize_c_whitespace(file_paths):
204188
"""Report if any C files """
@@ -256,10 +240,8 @@ def ci(pull_request):
256240
return
257241
base_branch = get_base_branch()
258242
file_paths = changed_files(base_branch)
259-
python_files = [fn for fn in file_paths if fn.endswith('.py')]
260243
c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
261244
fixed = []
262-
fixed.extend(normalize_whitespace(python_files))
263245
fixed.extend(normalize_c_whitespace(c_files))
264246
if not fixed:
265247
print('No whitespace issues found')
@@ -273,13 +255,10 @@ def ci(pull_request):
273255
def main():
274256
base_branch = get_base_branch()
275257
file_paths = changed_files(base_branch)
276-
python_files = [fn for fn in file_paths if fn.endswith('.py')]
277258
c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
278259
doc_files = [fn for fn in file_paths if fn.startswith('Doc') and
279260
fn.endswith(('.rst', '.inc'))]
280261
misc_files = {p for p in file_paths if p.startswith('Misc')}
281-
# PEP 8 whitespace rules enforcement.
282-
normalize_whitespace(python_files)
283262
# C rules enforcement.
284263
normalize_c_whitespace(c_files)
285264
# Docs updated.
@@ -294,10 +273,13 @@ def main():
294273
regenerated_pyconfig_h_in(file_paths)
295274

296275
# Test suite run and passed.
297-
if python_files or c_files:
298-
end = " and check for refleaks?" if c_files else "?"
299-
print()
300-
print("Did you run the test suite" + end)
276+
has_c_files = any(fn for fn in file_paths if fn.endswith(('.c', '.h')))
277+
has_python_files = any(fn for fn in file_paths if fn.endswith('.py'))
278+
print()
279+
if has_c_files:
280+
print("Did you run the test suite and check for refleaks?")
281+
elif has_python_files:
282+
print("Did you run the test suite?")
301283

302284

303285
if __name__ == '__main__':

0 commit comments

Comments
 (0)