|
11 | 11 | """
|
12 | 12 |
|
13 | 13 | import argparse
|
| 14 | +import ast |
14 | 15 | import os
|
15 | 16 | import sys
|
16 | 17 | import token
|
@@ -102,6 +103,33 @@ def bare_pytest_raises(file_obj: IO[str]) -> Iterable[Tuple[int, str]]:
|
102 | 103 | break
|
103 | 104 |
|
104 | 105 |
|
| 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 | + |
105 | 133 | def strings_to_concatenate(file_obj: IO[str]) -> Iterable[Tuple[int, str]]:
|
106 | 134 | """
|
107 | 135 | This test case is necessary after 'Black' (https://github.com/psf/black),
|
@@ -343,6 +371,7 @@ def main(
|
343 | 371 | if __name__ == "__main__":
|
344 | 372 | available_validation_types: List[str] = [
|
345 | 373 | "bare_pytest_raises",
|
| 374 | + "private_function_across_module", |
346 | 375 | "strings_to_concatenate",
|
347 | 376 | "strings_with_wrong_placed_whitespace",
|
348 | 377 | ]
|
|
0 commit comments