Coverage for metrics/cyclomatic/python.py: 85%

1"""McCabe cyclomatic complexity for Python."""

2

3 from __future__ import annotations

4

5 from metrics.cyclomatic.shared import (

6 create_cyclomatic_computer,

7 create_cyclomatic_wrapper,

8)

9

10

11 _PY_DECISION_TYPES = {

12 "if_statement",

13 "elif_clause",

14 "for_statement",

15 "while_statement",

16 "except_clause",

17 "conditional_expression",

18 "case_clause",

19 "boolean_operator",

20}

21

22

23 def _py_skip_check(child, top_func) -> bool:

24 """Skip nested function/class definitions."""

25 if child.type == "function_definition" and child is not top_func:

26 return True

27 if child.type == "class_definition":

28 return True

29 if child.type == "decorated_definition":

30 return True

31 return False

32

33

34 _count_decisions = create_cyclomatic_computer(_py_skip_check, _PY_DECISION_TYPES)

35 compute_cyclomatic_py = create_cyclomatic_wrapper(_count_decisions)