Coverage for parsers / python.py: 94%

18 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-08 15:04 -0800

1"""Python tree-sitter parser and function collection.""" 

2 

3from __future__ import annotations 

4 

5import tree_sitter_python as tspython 

6from tree_sitter import Language 

7 

8from parsers.common import parse_with_language 

9from parsers.shared import create_parse, create_collect_functions 

10 

11PY_LANGUAGE = Language(tspython.language()) 

12 

13parse = create_parse(PY_LANGUAGE) 

14 

15 

16def _collect_functions_recursive(node, acc: list) -> None: 

17 if node.type == "function_definition": 

18 acc.append(node) 

19 for child in node.children: 

20 _collect_functions_recursive(child, acc) 

21 

22 

23collect_functions = create_collect_functions(_collect_functions_recursive) 

24 

25 

26def func_name(node) -> str: 

27 """Extract function name from a function_definition node.""" 

28 for child in node.children: 

29 if child.type == "identifier": 

30 return child.text.decode() 

31 return "<anonymous>"