Skip to content

CI: Check private function not used across modules #33393

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

Closed
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
13 changes: 13 additions & 0 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ if [[ -z "$CHECK" || "$CHECK" == "lint" ]]; then
fi
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/
RET=$(($RET + $?)) ; echo $MSG "DONE"
$BASE_DIR/scripts/validate_unwanted_patterns.py --validation-type="private_function_across_module" --format="##[error]{source_path}:{line_number}:{msg}" pandas/tseries/
RET=$(($RET + $?)) ; echo $MSG "DONE"
else
$BASE_DIR/scripts/validate_unwanted_patterns.py --validation-type="private_function_across_module" pandas/core/
RET=$(($RET + $?)) ; echo $MSG "DONE"
$BASE_DIR/scripts/validate_unwanted_patterns.py --validation-type="private_function_across_module" pandas/tseries/
RET=$(($RET + $?)) ; echo $MSG "DONE"
fi

echo "isort --version-number"
isort --version-number

Expand Down
52 changes: 51 additions & 1 deletion scripts/validate_unwanted_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import sys
import token
import tokenize
from typing import IO, Callable, Iterable, List, Tuple
from typing import IO, Callable, Iterable, List, Set, Tuple

FILE_EXTENSIONS_TO_CHECK: Tuple[str, ...] = (".py", ".pyx", ".pxi.ini", ".pxd")
PATHS_TO_IGNORE: Tuple[str, ...] = ("asv_bench/env",)
Expand Down Expand Up @@ -115,6 +115,55 @@ def bare_pytest_raises(file_obj: IO[str]) -> Iterable[Tuple[int, str]]:
)


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 the private function that is used across modules.
msg : str
Explenation of the error.
"""
contents = file_obj.read()
tree = ast.parse(contents)

imported_modules: Set[str] = set()

for node in ast.walk(tree):
if isinstance(node, (ast.Import, ast.ImportFrom)):
for module in node.names:
module_fqdn = module.name if module.asname is None else module.asname
imported_modules.add(module_fqdn)

if not isinstance(node, ast.Call):
continue

try:
module_name = node.func.value.id
function_name = node.func.attr
except AttributeError:
continue

### Exception section ###

# (Debatable) Class case
if module_name[0].isupper():
continue
# (Debatable) Dunder methods case
elif function_name.startswith("__") and function_name.endswith("__"):
continue

if module_name in imported_modules and function_name.startswith("_"):
yield (node.lineno, f"Private function '{module_name}.{function_name}'")


def strings_to_concatenate(file_obj: IO[str]) -> Iterable[Tuple[int, str]]:
"""
This test case is necessary after 'Black' (https://github.com/psf/black),
Expand Down Expand Up @@ -358,6 +407,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",
]
Expand Down