Coverage for parsers/python.py: 94%

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

2

3 from __future__ import annotations

4

5 import tree_sitter_python as tspython

6 from tree_sitter import Language

7

8 from parsers.common import parse_with_language

9 from parsers.shared import create_parse, create_collect_functions

10

11 PY_LANGUAGE = Language(tspython.language())

12

13 parse = create_parse(PY_LANGUAGE)

14

15

16 def _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

23 collect_functions = create_collect_functions(_collect_functions_recursive)

24

25

26 def 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>"