Skip to content

Commit ed337d9

Browse files
MarcoGorelliKevin D Smith
authored and
Kevin D Smith
committed
CI move validate unwanted patterns over to pre-commit (pandas-dev#37379)
* move validate unwanted patterns over to pre-commit * better names
1 parent 09b0d0f commit ed337d9

File tree

3 files changed

+26
-82
lines changed

3 files changed

+26
-82
lines changed

.pre-commit-config.yaml

+22
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,28 @@ repos:
9999
language: pygrep
100100
entry: (\.\. code-block ::|\.\. ipython ::)
101101
files: \.(py|pyx|rst)$
102+
- id: unwanted-patterns-strings-to-concatenate
103+
name: Check for use of not concatenated strings
104+
language: python
105+
entry: ./scripts/validate_unwanted_patterns.py --validation-type="strings_to_concatenate"
106+
files: \.(py|pyx|pxd|pxi)$
107+
- id: unwanted-patterns-strings-with-wrong-placed-whitespace
108+
name: Check for strings with wrong placed spaces
109+
language: python
110+
entry: ./scripts/validate_unwanted_patterns.py --validation-type="strings_with_wrong_placed_whitespace"
111+
files: \.(py|pyx|pxd|pxi)$
112+
- id: unwanted-patterns-private-import-across-module
113+
name: Check for import of private attributes across modules
114+
language: python
115+
entry: ./scripts/validate_unwanted_patterns.py --validation-type="private_import_across_module"
116+
types: [python]
117+
exclude: ^(asv_bench|pandas/_vendored|pandas/tests|doc)/
118+
- id: unwanted-patterns-private-function-across-module
119+
name: Check for use of private functions across modules
120+
language: python
121+
entry: ./scripts/validate_unwanted_patterns.py --validation-type="private_function_across_module"
122+
types: [python]
123+
exclude: ^(asv_bench|pandas/_vendored|pandas/tests|doc)/
102124
- repo: https://github.com/asottile/yesqa
103125
rev: v1.2.2
104126
hooks:

ci/code_checks.sh

-32
Original file line numberDiff line numberDiff line change
@@ -73,38 +73,6 @@ if [[ -z "$CHECK" || "$CHECK" == "lint" ]]; then
7373
cpplint --quiet --extensions=c,h --headers=h --recursive --filter=-readability/casting,-runtime/int,-build/include_subdir pandas/_libs/src/*.h pandas/_libs/src/parser pandas/_libs/ujson pandas/_libs/tslibs/src/datetime pandas/_libs/*.cpp
7474
RET=$(($RET + $?)) ; echo $MSG "DONE"
7575

76-
MSG='Check for use of not concatenated strings' ; echo $MSG
77-
if [[ "$GITHUB_ACTIONS" == "true" ]]; then
78-
$BASE_DIR/scripts/validate_unwanted_patterns.py --validation-type="strings_to_concatenate" --format="##[error]{source_path}:{line_number}:{msg}" .
79-
else
80-
$BASE_DIR/scripts/validate_unwanted_patterns.py --validation-type="strings_to_concatenate" .
81-
fi
82-
RET=$(($RET + $?)) ; echo $MSG "DONE"
83-
84-
MSG='Check for strings with wrong placed spaces' ; echo $MSG
85-
if [[ "$GITHUB_ACTIONS" == "true" ]]; then
86-
$BASE_DIR/scripts/validate_unwanted_patterns.py --validation-type="strings_with_wrong_placed_whitespace" --format="##[error]{source_path}:{line_number}:{msg}" .
87-
else
88-
$BASE_DIR/scripts/validate_unwanted_patterns.py --validation-type="strings_with_wrong_placed_whitespace" .
89-
fi
90-
RET=$(($RET + $?)) ; echo $MSG "DONE"
91-
92-
MSG='Check for import of private attributes across modules' ; echo $MSG
93-
if [[ "$GITHUB_ACTIONS" == "true" ]]; then
94-
$BASE_DIR/scripts/validate_unwanted_patterns.py --validation-type="private_import_across_module" --included-file-extensions="py" --excluded-file-paths=pandas/tests,asv_bench/,pandas/_vendored --format="##[error]{source_path}:{line_number}:{msg}" pandas/
95-
else
96-
$BASE_DIR/scripts/validate_unwanted_patterns.py --validation-type="private_import_across_module" --included-file-extensions="py" --excluded-file-paths=pandas/tests,asv_bench/,pandas/_vendored pandas/
97-
fi
98-
RET=$(($RET + $?)) ; echo $MSG "DONE"
99-
100-
MSG='Check for use of private functions across modules' ; echo $MSG
101-
if [[ "$GITHUB_ACTIONS" == "true" ]]; then
102-
$BASE_DIR/scripts/validate_unwanted_patterns.py --validation-type="private_function_across_module" --included-file-extensions="py" --excluded-file-paths=pandas/tests,asv_bench/,pandas/_vendored,doc/ --format="##[error]{source_path}:{line_number}:{msg}" pandas/
103-
else
104-
$BASE_DIR/scripts/validate_unwanted_patterns.py --validation-type="private_function_across_module" --included-file-extensions="py" --excluded-file-paths=pandas/tests,asv_bench/,pandas/_vendored,doc/ pandas/
105-
fi
106-
RET=$(($RET + $?)) ; echo $MSG "DONE"
107-
10876
fi
10977

11078
### PATTERNS ###

scripts/validate_unwanted_patterns.py

+4-50
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212

1313
import argparse
1414
import ast
15-
import os
1615
import sys
1716
import token
1817
import tokenize
19-
from typing import IO, Callable, FrozenSet, Iterable, List, Set, Tuple
18+
from typing import IO, Callable, Iterable, List, Set, Tuple
2019

2120
PRIVATE_IMPORTS_TO_IGNORE: Set[str] = {
2221
"_extension_array_shared_docs",
@@ -403,8 +402,6 @@ def main(
403402
function: Callable[[IO[str]], Iterable[Tuple[int, str]]],
404403
source_path: str,
405404
output_format: str,
406-
file_extensions_to_check: str,
407-
excluded_file_paths: str,
408405
) -> bool:
409406
"""
410407
Main entry point of the script.
@@ -432,19 +429,9 @@ def main(
432429
ValueError
433430
If the `source_path` is not pointing to existing file/directory.
434431
"""
435-
if not os.path.exists(source_path):
436-
raise ValueError("Please enter a valid path, pointing to a file/directory.")
437-
438432
is_failed: bool = False
439-
file_path: str = ""
440-
441-
FILE_EXTENSIONS_TO_CHECK: FrozenSet[str] = frozenset(
442-
file_extensions_to_check.split(",")
443-
)
444-
PATHS_TO_IGNORE = frozenset(excluded_file_paths.split(","))
445433

446-
if os.path.isfile(source_path):
447-
file_path = source_path
434+
for file_path in source_path:
448435
with open(file_path) as file_obj:
449436
for line_number, msg in function(file_obj):
450437
is_failed = True
@@ -454,25 +441,6 @@ def main(
454441
)
455442
)
456443

457-
for subdir, _, files in os.walk(source_path):
458-
if any(path in subdir for path in PATHS_TO_IGNORE):
459-
continue
460-
for file_name in files:
461-
if not any(
462-
file_name.endswith(extension) for extension in FILE_EXTENSIONS_TO_CHECK
463-
):
464-
continue
465-
466-
file_path = os.path.join(subdir, file_name)
467-
with open(file_path) as file_obj:
468-
for line_number, msg in function(file_obj):
469-
is_failed = True
470-
print(
471-
output_format.format(
472-
source_path=file_path, line_number=line_number, msg=msg
473-
)
474-
)
475-
476444
return is_failed
477445

478446

@@ -487,9 +455,7 @@ def main(
487455

488456
parser = argparse.ArgumentParser(description="Unwanted patterns checker.")
489457

490-
parser.add_argument(
491-
"path", nargs="?", default=".", help="Source path of file/directory to check."
492-
)
458+
parser.add_argument("paths", nargs="*", help="Source paths of files to check.")
493459
parser.add_argument(
494460
"--format",
495461
"-f",
@@ -503,25 +469,13 @@ def main(
503469
required=True,
504470
help="Validation test case to check.",
505471
)
506-
parser.add_argument(
507-
"--included-file-extensions",
508-
default="py,pyx,pxd,pxi",
509-
help="Comma separated file extensions to check.",
510-
)
511-
parser.add_argument(
512-
"--excluded-file-paths",
513-
default="asv_bench/env",
514-
help="Comma separated file paths to exclude.",
515-
)
516472

517473
args = parser.parse_args()
518474

519475
sys.exit(
520476
main(
521477
function=globals().get(args.validation_type), # type: ignore
522-
source_path=args.path,
478+
source_path=args.paths,
523479
output_format=args.format,
524-
file_extensions_to_check=args.included_file_extensions,
525-
excluded_file_paths=args.excluded_file_paths,
526480
)
527481
)

0 commit comments

Comments
 (0)