From 7ec98517142b312b9c0d5b80795932cc2ad8131f Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Mon, 23 Mar 2020 20:21:23 +0200 Subject: [PATCH] CI: Check that private functions are not used across modules --- ci/code_checks.sh | 8 ++++++++ scripts/validate_unwanted_patterns.py | 29 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index 15b4128424eb1..0868c7b6a3a12 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -232,6 +232,14 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then invgrep -R --include=*.{py,pyx} 'xrange' pandas RET=$(($RET + $?)) ; echo $MSG "DONE" + MSG='Check for use of private functions across modules' ; echo $MSG + if [[ "$GITHUB_ACTIONS" == "true" ]]; then + $BASE_DIR/scripts/validate_unwanted_patterns.py --validation-type="private_function_across_module" --format="##[error]{source_path}:{line_number}:{msg}" pandas/core/ + else + $BASE_DIR/scripts/validate_unwanted_patterns.py --validation-type="private_function_across_module" pandas/core + fi + RET=$(($RET + $?)) ; echo $MSG "DONE" + MSG='Check that no file in the repo contains trailing whitespaces' ; echo $MSG INVGREP_APPEND=" <- trailing whitespaces found" invgrep -RI --exclude=\*.{svg,c,cpp,html,js} --exclude-dir=env "\s$" * diff --git a/scripts/validate_unwanted_patterns.py b/scripts/validate_unwanted_patterns.py index c4be85ffe7306..f4ea1490081f6 100755 --- a/scripts/validate_unwanted_patterns.py +++ b/scripts/validate_unwanted_patterns.py @@ -11,6 +11,7 @@ """ import argparse +import ast import os import sys import token @@ -102,6 +103,33 @@ def bare_pytest_raises(file_obj: IO[str]) -> Iterable[Tuple[int, str]]: break +def private_function_across_module(file_obj: IO[str]) -> Iterable[Tuple[int, str]]: + """ + Checking that a private function is not used across modules. + + Parameters + ---------- + file_obj : IO + File-like object containing the Python code to validate. + + Yields + ------ + line_number : int + Line number of import statement, that imports the private function. + msg : str + Explenation of the error. + """ + contents = file_obj.read() + tree = ast.parse(contents) + + for node in ast.walk(tree): + if not (isinstance(node, ast.Import) or isinstance(node, ast.ImportFrom)): + continue + + if any(mod.name.split(".")[-1].startswith("_") for mod in node.names): + yield (node.lineno, "Use of private function across modules found.") + + def strings_to_concatenate(file_obj: IO[str]) -> Iterable[Tuple[int, str]]: """ This test case is necessary after 'Black' (https://github.com/psf/black), @@ -343,6 +371,7 @@ def main( if __name__ == "__main__": available_validation_types: List[str] = [ "bare_pytest_raises", + "private_function_across_module", "strings_to_concatenate", "strings_with_wrong_placed_whitespace", ]