Skip to content

Commit d9f3928

Browse files
author
MomIsBestFriend
committed
CI: Check that private functions are not used across modules
1 parent d3e5485 commit d9f3928

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

ci/code_checks.sh

+8
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,14 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
232232
invgrep -R --include=*.{py,pyx} 'xrange' pandas
233233
RET=$(($RET + $?)) ; echo $MSG "DONE"
234234

235+
MSG='Check for use of private functions across modules' ; echo $MSG
236+
if [[ "$GITHUB_ACTIONS" == "true" ]]; then
237+
$BASE_DIR/scripts/validate_unwanted_patterns.py --validation-type="private_function_across_module" --format="##[error]{source_path}:{line_number}:{msg}" pandas/core/
238+
else
239+
$BASE_DIR/scripts/validate_unwanted_patterns.py --validation-type="private_function_across_module" pandas/core
240+
fi
241+
RET=$(($RET + $?)) ; echo $MSG "DONE"
242+
235243
MSG='Check that no file in the repo contains trailing whitespaces' ; echo $MSG
236244
INVGREP_APPEND=" <- trailing whitespaces found"
237245
invgrep -RI --exclude=\*.{svg,c,cpp,html,js} --exclude-dir=env "\s$" *

scripts/validate_unwanted_patterns.py

+29
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"""
1212

1313
import argparse
14+
import ast
1415
import os
1516
import sys
1617
import token
@@ -102,6 +103,33 @@ def bare_pytest_raises(file_obj: IO[str]) -> Iterable[Tuple[int, str]]:
102103
break
103104

104105

106+
def private_function_across_module(file_obj: IO[str]) -> Iterable[Tuple[int, str]]:
107+
"""
108+
Checking that a private function is not used across modules.
109+
110+
Parameters
111+
----------
112+
file_obj : IO
113+
File-like object containing the Python code to validate.
114+
115+
Yields
116+
------
117+
line_number : int
118+
Line number of import statement, that imports the private function.
119+
msg : str
120+
Explenation of the error.
121+
"""
122+
contents = file_obj.read()
123+
tree = ast.parse(contents)
124+
125+
for node in ast.walk(tree):
126+
if not (isinstance(node, ast.Import) or isinstance(node, ast.ImportFrom)):
127+
continue
128+
129+
if any(mod.name.split(".")[-1].startswith("_") for mod in node.names):
130+
yield (node.lineno, "Use of private function across modules found.")
131+
132+
105133
def strings_to_concatenate(file_obj: IO[str]) -> Iterable[Tuple[int, str]]:
106134
"""
107135
This test case is necessary after 'Black' (https://github.com/psf/black),
@@ -343,6 +371,7 @@ def main(
343371
if __name__ == "__main__":
344372
available_validation_types: List[str] = [
345373
"bare_pytest_raises",
374+
"private_function_across_module",
346375
"strings_to_concatenate",
347376
"strings_with_wrong_placed_whitespace",
348377
]

0 commit comments

Comments
 (0)