Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-109408: Add the docs whitespace check from patchcheck to pre-commit #109854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 10, 2023
2 changes: 1 addition & 1 deletion 2 .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repos:
types: [python]
exclude: Lib/test/tokenizedata/coding20731.py
- id: trailing-whitespace
types_or: [c, python, rst]
types_or: [c, inc, python, rst]

- repo: https://github.com/sphinx-contrib/sphinx-lint
rev: v0.6.8
Expand Down
2 changes: 2 additions & 0 deletions 2 Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -2772,6 +2772,8 @@ funny:
.PHONY: patchcheck
patchcheck: all
$(RUNSHARED) ./$(BUILDPYTHON) $(srcdir)/Tools/patchcheck/patchcheck.py
@$(RUNSHARED) ./$(BUILDPYTHON) -m pre_commit --version >/dev/null 2>&1 || $(RUNSHARED) ./$(BUILDPYTHON) -m pip install pre-commit >/dev/null 2>&1
$(RUNSHARED) ./$(BUILDPYTHON) -m pre_commit run --all-files
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think make patchcheck should run pre commit, especially if the goal is to remove it. Hence I'd say this is a docs point to communicate.

Suggested change
@$(RUNSHARED) ./$(BUILDPYTHON) -m pre_commit --version >/dev/null 2>&1 || $(RUNSHARED) ./$(BUILDPYTHON) -m pip install pre-commit >/dev/null 2>&1
$(RUNSHARED) ./$(BUILDPYTHON) -m pre_commit run --all-files

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking it would be a good idea to add a make lint target like we have for the PEPs repo (and Pillow), which seems to work there.

Perhaps we should add make lint now and have it run pre-commit; and maybe also patchcheck?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd agree. We should also update the devguide (currently only patchcheck is mentioned)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed the Makefile changes from this PR, let's decide what to later after discussion:

https://discuss.python.org/t/do-you-use-make-patchcheck/34743?u=hugovk


.PHONY: check-limited-abi
check-limited-abi: all
Expand Down
46 changes: 12 additions & 34 deletions 46 Tools/patchcheck/patchcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

def n_files_str(count):
"""Return 'N file(s)' with the proper plurality on 'file'."""
return "{} file{}".format(count, "s" if count != 1 else "")
s = "s" if count != 1 else ""
return f"{count} file{s}"


def status(message, modal=False, info=None):
Expand Down Expand Up @@ -77,7 +78,7 @@ def get_git_remote_default_branch(remote_name):

It is typically called 'main', but may differ
"""
cmd = "git remote show {}".format(remote_name).split()
cmd = f"git remote show {remote_name}".split()
AA-Turner marked this conversation as resolved.
Show resolved Hide resolved
env = os.environ.copy()
env['LANG'] = 'C'
try:
Expand Down Expand Up @@ -164,9 +165,9 @@ def report_modified_files(file_paths):
if count == 0:
return n_files_str(count)
else:
lines = ["{}:".format(n_files_str(count))]
lines = [f"{n_files_str(count)}:"]
for path in file_paths:
lines.append(" {}".format(path))
lines.append(f" {path}")
return "\n".join(lines)


Expand Down Expand Up @@ -205,27 +206,6 @@ def normalize_c_whitespace(file_paths):
return fixed


ws_re = re.compile(br'\s+(\r?\n)$')

@status("Fixing docs whitespace", info=report_modified_files)
def normalize_docs_whitespace(file_paths):
fixed = []
for path in file_paths:
abspath = os.path.join(SRCDIR, path)
try:
with open(abspath, 'rb') as f:
lines = f.readlines()
new_lines = [ws_re.sub(br'\1', line) for line in lines]
if new_lines != lines:
shutil.copyfile(abspath, abspath + '.bak')
with open(abspath, 'wb') as f:
f.writelines(new_lines)
fixed.append(path)
except Exception as err:
print('Cannot fix %s: %s' % (path, err))
return fixed


@status("Docs modified", modal=True)
def docs_modified(file_paths):
"""Report if any file in the Doc directory has been changed."""
Expand All @@ -244,6 +224,7 @@ def reported_news(file_paths):
return any(p.startswith(os.path.join('Misc', 'NEWS.d', 'next'))
for p in file_paths)


@status("configure regenerated", modal=True, info=str)
def regenerated_configure(file_paths):
"""Check if configure has been regenerated."""
Expand All @@ -252,6 +233,7 @@ def regenerated_configure(file_paths):
else:
return "not needed"


@status("pyconfig.h.in regenerated", modal=True, info=str)
def regenerated_pyconfig_h_in(file_paths):
"""Check if pyconfig.h.in has been regenerated."""
Expand All @@ -260,6 +242,7 @@ def regenerated_pyconfig_h_in(file_paths):
else:
return "not needed"


def ci(pull_request):
if pull_request == 'false':
print('Not a pull request; skipping')
Expand All @@ -268,33 +251,28 @@ def ci(pull_request):
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'))]
fixed = []
fixed.extend(normalize_whitespace(python_files))
fixed.extend(normalize_c_whitespace(c_files))
fixed.extend(normalize_docs_whitespace(doc_files))
if not fixed:
print('No whitespace issues found')
else:
print(f'Please fix the {len(fixed)} file(s) with whitespace issues')
print('(on UNIX you can run `make patchcheck` to make the fixes)')
count = len(fixed)
print(f'Please fix the {n_files_str(count)} with whitespace issues')
print('(on Unix you can run `make patchcheck` to make the fixes)')
sys.exit(1)


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'))]
hugovk marked this conversation as resolved.
Show resolved Hide resolved
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)
# Doc whitespace enforcement.
normalize_docs_whitespace(doc_files)
# Docs updated.
docs_modified(doc_files)
# Misc/ACKS changed.
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.