Extract function definitions and their line ranges from a Python file.
(self, file_path: Path)
| 279 | return sorted(gaps, key=lambda x: (x.priority == 'HIGH', x.complexity_score), reverse=True) |
| 280 | |
| 281 | def _extract_functions_from_file(self, file_path: Path) -> Dict[str, List[int]]: |
| 282 | """Extract function definitions and their line ranges from a Python file.""" |
| 283 | functions = {} |
| 284 | |
| 285 | try: |
| 286 | with open(file_path, 'r', encoding='utf-8') as f: |
| 287 | source = f.read() |
| 288 | |
| 289 | tree = ast.parse(source) |
| 290 | |
| 291 | for node in ast.walk(tree): |
| 292 | if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): |
| 293 | # Calculate line range for function |
| 294 | start_line = node.lineno |
| 295 | end_line = start_line |
| 296 | |
| 297 | # Find the end line by looking at the last statement |
| 298 | if node.body: |
| 299 | last_stmt = node.body[-1] |
| 300 | end_line = getattr(last_stmt, 'end_lineno', last_stmt.lineno) |
| 301 | |
| 302 | functions[node.name] = list(range(start_line, end_line + 1)) |
| 303 | |
| 304 | except (SyntaxError, UnicodeDecodeError) as e: |
| 305 | print(f"Warning: Could not parse {file_path}: {e}") |
| 306 | |
| 307 | return functions |
| 308 | |
| 309 | def _calculate_complexity_score(self, func_lines: List[int], missing_lines: List[int]) -> float: |
| 310 | """Calculate complexity score based on function size and missing coverage.""" |
no outgoing calls
no test coverage detected