Method: pylint.extensions.overlapping_exceptions.OverlappingExceptionsChecker.visit_tryexcept
Calls: 10, Exceptions: 0, Paths: 3Back
Path 1: 5 calls (0.5)
TryExcept (5)
1@utils.only_required_for_messages("overlapping-except")
2 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
3 """Check for empty except."""
4 for handler in node.handlers:
5 if handler.type is None:
6 continue
7 if isinstance(handler.type, astroid.BoolOp):
8 continue
9 try:
10 excs = list(_annotated_unpack_infer(handler.type))
11 except astroid.InferenceError:
12 continue
13
14 handled_in_clause: list[tuple[Any, Any]] = []
15 for part, exc in excs:
16 if exc is astroid.Uninferable:
17 continue
18 if isinstance(exc, astroid.Instance) and utils.inherit_from_std_ex(exc):
19 exc = exc._proxied
20
21 if not isinstance(exc, astroid.ClassDef):
22 continue
23
24 exc_ancestors = [
25 anc for anc in exc.ancestors() if isinstance(anc, astroid.ClassDef)
26 ]
27
28 for prev_part, prev_exc in handled_in_clause:
29 prev_exc_ancestors = [
30 anc
31 for anc in prev_exc.ancestors()
32 if isinstance(anc, astroid.ClassDef)
33 ]
34 if exc == prev_exc:
35 self.add_message(
36 "overlapping-except",
37 node=handler.type,
38 args=f"{prev_part.as_string()} and {part.as_string()} are the same",
39 )
40 elif prev_exc in exc_ancestors or exc in prev_exc_ancestors:
41 ancestor = part if exc in prev_exc_ancestors else prev_part
42 descendant = part if prev_exc in exc_ancestors else prev_part
43 self.add_message(
44 "overlapping-except",
45 node=handler.type,
46 args=f"{ancestor.as_string()} is an ancestor class of {descendant.as_string()}",
47 )
48 handled_in_clause += [(part, exc)]
Path 2: 4 calls (0.4)
TryExcept (4)
1@utils.only_required_for_messages("overlapping-except")
2 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
3 """Check for empty except."""
4 for handler in node.handlers:
5 if handler.type is None:
6 continue
7 if isinstance(handler.type, astroid.BoolOp):
8 continue
9 try:
10 excs = list(_annotated_unpack_infer(handler.type))
11 except astroid.InferenceError:
12 continue
13
14 handled_in_clause: list[tuple[Any, Any]] = []
15 for part, exc in excs:
16 if exc is astroid.Uninferable:
17 continue
18 if isinstance(exc, astroid.Instance) and utils.inherit_from_std_ex(exc):
19 exc = exc._proxied
20
21 if not isinstance(exc, astroid.ClassDef):
22 continue
23
24 exc_ancestors = [
25 anc for anc in exc.ancestors() if isinstance(anc, astroid.ClassDef)
26 ]
27
28 for prev_part, prev_exc in handled_in_clause:
29 prev_exc_ancestors = [
30 anc
31 for anc in prev_exc.ancestors()
32 if isinstance(anc, astroid.ClassDef)
33 ]
34 if exc == prev_exc:
35 self.add_message(
36 "overlapping-except",
37 node=handler.type,
38 args=f"{prev_part.as_string()} and {part.as_string()} are the same",
39 )
40 elif prev_exc in exc_ancestors or exc in prev_exc_ancestors:
41 ancestor = part if exc in prev_exc_ancestors else prev_part
42 descendant = part if prev_exc in exc_ancestors else prev_part
43 self.add_message(
44 "overlapping-except",
45 node=handler.type,
46 args=f"{ancestor.as_string()} is an ancestor class of {descendant.as_string()}",
47 )
48 handled_in_clause += [(part, exc)]
Path 3: 1 calls (0.1)
TryExcept (1)
1@utils.only_required_for_messages("overlapping-except")
2 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
3 """Check for empty except."""
4 for handler in node.handlers:
5 if handler.type is None:
6 continue
7 if isinstance(handler.type, astroid.BoolOp):
8 continue
9 try:
10 excs = list(_annotated_unpack_infer(handler.type))
11 except astroid.InferenceError:
12 continue
13
14 handled_in_clause: list[tuple[Any, Any]] = []
15 for part, exc in excs:
16 if exc is astroid.Uninferable:
17 continue
18 if isinstance(exc, astroid.Instance) and utils.inherit_from_std_ex(exc):
19 exc = exc._proxied
20
21 if not isinstance(exc, astroid.ClassDef):
22 continue
23
24 exc_ancestors = [
25 anc for anc in exc.ancestors() if isinstance(anc, astroid.ClassDef)
26 ]
27
28 for prev_part, prev_exc in handled_in_clause:
29 prev_exc_ancestors = [
30 anc
31 for anc in prev_exc.ancestors()
32 if isinstance(anc, astroid.ClassDef)
33 ]
34 if exc == prev_exc:
35 self.add_message(
36 "overlapping-except",
37 node=handler.type,
38 args=f"{prev_part.as_string()} and {part.as_string()} are the same",
39 )
40 elif prev_exc in exc_ancestors or exc in prev_exc_ancestors:
41 ancestor = part if exc in prev_exc_ancestors else prev_part
42 descendant = part if prev_exc in exc_ancestors else prev_part
43 self.add_message(
44 "overlapping-except",
45 node=handler.type,
46 args=f"{ancestor.as_string()} is an ancestor class of {descendant.as_string()}",
47 )
48 handled_in_clause += [(part, exc)]