From 26ccc48760cf174093f189d26c570495bf78a702 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Date: Tue, 10 Oct 2023 10:17:53 +0100 Subject: [PATCH 1/3] [3.11] GH-109408: Move the Python file whitespace check from patchcheck to pre-commit (GH-109891) Co-authored-by: Hugo van Kemenade . (cherry picked from commit 08ec4a1dbf66383303de9ce5cb55b2b437ef92c0) Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> --- .pre-commit-config.yaml | 9 +++++++++ Tools/scripts/patchcheck.py | 26 +++++++------------------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1ae3dd71354e63..96f9635201eff5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -16,6 +16,15 @@ repos: - id: trailing-whitespace types_or: [c, inc, python, rst] + - repo: local + hooks: + - id: python-file-whitespace + name: "Check Python file whitespace" + entry: 'python Tools/patchcheck/reindent.py --nobackup --newline LF' + language: 'system' + types: [python] + exclude: '^(Lib/test/tokenizedata/|Tools/c-analyzer/cpython/_parser).*$' + - repo: https://github.com/sphinx-contrib/sphinx-lint rev: v0.6.8 hooks: diff --git a/Tools/scripts/patchcheck.py b/Tools/scripts/patchcheck.py index c53dd45d8601d3..e5fa40a32fbd89 100755 --- a/Tools/scripts/patchcheck.py +++ b/Tools/scripts/patchcheck.py @@ -7,7 +7,6 @@ import subprocess import sysconfig -import reindent import untabify @@ -177,15 +176,6 @@ def report_modified_files(file_paths): return "\n".join(lines) -@status("Fixing Python file whitespace", info=report_modified_files) -def normalize_whitespace(file_paths): - """Make sure that the whitespace for .py files have been normalized.""" - reindent.makebackup = False # No need to create backups. - fixed = [path for path in file_paths if path.endswith('.py') and - reindent.check(os.path.join(SRCDIR, path))] - return fixed - - @status("Fixing C file whitespace", info=report_modified_files) def normalize_c_whitespace(file_paths): """Report if any C files """ @@ -243,10 +233,8 @@ def ci(pull_request): return base_branch = get_base_branch() file_paths = changed_files(base_branch) - python_files = [fn for fn in file_paths if fn.endswith('.py')] c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))] fixed = [] - fixed.extend(normalize_whitespace(python_files)) fixed.extend(normalize_c_whitespace(c_files)) if not fixed: print('No whitespace issues found') @@ -260,13 +248,10 @@ def ci(pull_request): def main(): base_branch = get_base_branch() file_paths = changed_files(base_branch) - python_files = [fn for fn in file_paths if fn.endswith('.py')] c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))] doc_files = [fn for fn in file_paths if fn.startswith('Doc') and fn.endswith(('.rst', '.inc'))] misc_files = {p for p in file_paths if p.startswith('Misc')} - # PEP 8 whitespace rules enforcement. - normalize_whitespace(python_files) # C rules enforcement. normalize_c_whitespace(c_files) # Docs updated. @@ -281,10 +266,13 @@ def main(): regenerated_pyconfig_h_in(file_paths) # Test suite run and passed. - if python_files or c_files: - end = " and check for refleaks?" if c_files else "?" - print() - print("Did you run the test suite" + end) + has_c_files = any(fn for fn in file_paths if fn.endswith(('.c', '.h'))) + has_python_files = any(fn for fn in file_paths if fn.endswith('.py')) + print() + if has_c_files: + print("Did you run the test suite and check for refleaks?") + elif has_python_files: + print("Did you run the test suite?") if __name__ == '__main__': From 0490c441e974791b78dc7a94f5dcab3b6db0d73c Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Tue, 10 Oct 2023 15:49:07 +0100 Subject: [PATCH 2/3] fix the directory --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 96f9635201eff5..3b90e015601e17 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,7 +20,7 @@ repos: hooks: - id: python-file-whitespace name: "Check Python file whitespace" - entry: 'python Tools/patchcheck/reindent.py --nobackup --newline LF' + entry: 'python Tools/scripts/reindent.py --nobackup --newline LF' language: 'system' types: [python] exclude: '^(Lib/test/tokenizedata/|Tools/c-analyzer/cpython/_parser).*$' From 8a442665a934a81ee7cebe14911471853679ebd2 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Tue, 10 Oct 2023 16:35:56 +0100 Subject: [PATCH 3/3] Fix excludes --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3b90e015601e17..8ade0166ec47a9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -23,7 +23,7 @@ repos: entry: 'python Tools/scripts/reindent.py --nobackup --newline LF' language: 'system' types: [python] - exclude: '^(Lib/test/tokenizedata/|Tools/c-analyzer/cpython/_parser).*$' + exclude: '^(Lib/test/tokenizedata/|Tools/c-analyzer/cpython/(_parser|_capi)).*$' - repo: https://github.com/sphinx-contrib/sphinx-lint rev: v0.6.8