Coverage for metrics/lloc/python.py: 100%

1"""LLOC (Logical Lines of Code) for Python."""

2

3 from __future__ import annotations

4

5 _PY_LLOC_TYPES = {

6 # Simple statements

7 "expression_statement", "return_statement", "pass_statement",

8 "break_statement", "continue_statement", "raise_statement",

9 "assert_statement", "delete_statement", "global_statement",

10 "nonlocal_statement", "import_statement", "import_from_statement",

11 "future_import_statement", "type_alias_statement",

12 # Compound statement headers

13 "if_statement", "elif_clause", "else_clause",

14 "for_statement", "while_statement",

15 "try_statement", "except_clause", "finally_clause",

16 "with_statement",

17 "function_definition", "class_definition",

18 "match_statement", "case_clause",

19 # Decorators

20 "decorator",

21}

22

23

24 def count_lloc(root) -> int:

25 """Count logical lines of code in a Python AST."""

26 count = 0

27 if root.type in _PY_LLOC_TYPES:

28 count += 1

29 for child in root.children:

30 count += count_lloc(child)

31 return count