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.
(file_obj: IO[str])
| 102 | |
| 103 | |
| 104 | def private_function_across_module(file_obj: IO[str]) -> Iterable[tuple[int, str]]: |
| 105 | """ |
| 106 | Checking that a private function is not used across modules. |
| 107 | Parameters |
| 108 | ---------- |
| 109 | file_obj : IO |
| 110 | File-like object containing the Python code to validate. |
| 111 | Yields |
| 112 | ------ |
| 113 | line_number : int |
| 114 | Line number of the private function that is used across modules. |
| 115 | msg : str |
| 116 | Explanation of the error. |
| 117 | """ |
| 118 | contents = file_obj.read() |
| 119 | tree = ast.parse(contents) |
| 120 | |
| 121 | imported_modules: set[str] = set() |
| 122 | |
| 123 | for node in ast.walk(tree): |
| 124 | if isinstance(node, (ast.Import, ast.ImportFrom)): |
| 125 | for module in node.names: |
| 126 | module_fqdn = module.name if module.asname is None else module.asname |
| 127 | imported_modules.add(module_fqdn) |
| 128 | |
| 129 | if not isinstance(node, ast.Call): |
| 130 | continue |
| 131 | |
| 132 | try: |
| 133 | module_name = node.func.value.id |
| 134 | function_name = node.func.attr |
| 135 | except AttributeError: |
| 136 | continue |
| 137 | |
| 138 | # Exception section # |
| 139 | |
| 140 | # (Debatable) Class case |
| 141 | if module_name[0].isupper(): |
| 142 | continue |
| 143 | # (Debatable) Dunder methods case |
| 144 | elif function_name.startswith("__") and function_name.endswith("__"): |
| 145 | continue |
| 146 | elif module_name + "." + function_name in PRIVATE_FUNCTIONS_ALLOWED: |
| 147 | continue |
| 148 | |
| 149 | if module_name in imported_modules and function_name.startswith("_"): |
| 150 | yield (node.lineno, f"Private function '{module_name}.{function_name}'") |
| 151 | |
| 152 | |
| 153 | def private_import_across_module(file_obj: IO[str]) -> Iterable[tuple[int, str]]: |