Skip to content

STYLE, CI move validate_rst_title_capitalization check to pre-commit #39779

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ repos:
language: pygrep
types: [python]
files: ^pandas/tests/
- id: title-capitalization
name: Validate correct capitalization among titles in documentation
entry: python scripts/validate_rst_title_capitalization.py
language: python
types: [rst]
files: ^doc/source/(development|reference)/
- repo: https://github.com/asottile/yesqa
rev: v1.2.2
hooks:
Expand Down
4 changes: 0 additions & 4 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
$BASE_DIR/scripts/validate_docstrings.py --format=actions --errors=GL03,GL04,GL05,GL06,GL07,GL09,GL10,SS02,SS04,SS05,PR03,PR04,PR05,PR10,EX04,RT01,RT04,RT05,SA02,SA03
RET=$(($RET + $?)) ; echo $MSG "DONE"

MSG='Validate correct capitalization among titles in documentation' ; echo $MSG
$BASE_DIR/scripts/validate_rst_title_capitalization.py $BASE_DIR/doc/source/development $BASE_DIR/doc/source/reference
Copy link
Member Author

Choose a reason for hiding this comment

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

any reason why this was only run in two directories? I guess it could be expanded now

RET=$(($RET + $?)) ; echo $MSG "DONE"

fi

### TYPING ###
Expand Down
56 changes: 9 additions & 47 deletions scripts/validate_rst_title_capitalization.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/usr/bin/env python3
"""
Validate that the titles in the rst files follow the proper capitalization convention.

Print the titles that do not follow the convention.

Usage::
./scripts/validate_rst_title_capitalization.py doc/source/development/contributing.rst
./scripts/validate_rst_title_capitalization.py doc/source/

As pre-commit hook (recommended):
pre-commit run title-capitalization --all-files

From the command-line:
python scripts/validate_rst_title_capitalization.py <rst file>
"""
import argparse
import glob
import os
import re
import sys
from typing import Iterable, List, Tuple
Expand Down Expand Up @@ -233,45 +233,14 @@ def find_titles(rst_file: str) -> Iterable[Tuple[str, int]]:
previous_line = line


def find_rst_files(source_paths: List[str]) -> Iterable[str]:
"""
Given the command line arguments of directory paths, this method
yields the strings of the .rst file directories that these paths contain.

Parameters
----------
source_paths : str
List of directories to validate, provided through command line arguments.

Yields
-------
str
Directory address of a .rst files found in command line argument directories.
"""

for directory_address in source_paths:
if not os.path.exists(directory_address):
raise ValueError(
"Please enter a valid path, pointing to a valid file/directory."
)
elif directory_address.endswith(".rst"):
yield directory_address
else:
yield from glob.glob(
pathname=f"{directory_address}/**/*.rst", recursive=True
)


def main(source_paths: List[str], output_format: str) -> int:
def main(source_paths: List[str]) -> int:
"""
The main method to print all headings with incorrect capitalization.

Parameters
----------
source_paths : str
List of directories to validate, provided through command line arguments.
output_format : str
Copy link
Member Author

Choose a reason for hiding this comment

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

looks like this was unused anyway

Output format of the script.

Returns
-------
Expand All @@ -281,7 +250,7 @@ def main(source_paths: List[str], output_format: str) -> int:

number_of_errors: int = 0

for filename in find_rst_files(source_paths):
for filename in source_paths:
for title, line_number in find_titles(filename):
if title != correct_title_capitalization(title):
print(
Expand All @@ -297,16 +266,9 @@ def main(source_paths: List[str], output_format: str) -> int:
parser = argparse.ArgumentParser(description="Validate heading capitalization")

parser.add_argument(
"paths", nargs="+", default=".", help="Source paths of file/directory to check."
)

parser.add_argument(
"--format",
"-f",
default="{source_path}:{line_number}:{msg}:{heading}:{correct_heading}",
help="Output format of incorrectly capitalized titles",
"paths", nargs="*", help="Source paths of file/directory to check."
)

args = parser.parse_args()

sys.exit(main(args.paths, args.format))
sys.exit(main(args.paths))