Method: pylint.checkers.exceptions.ExceptionsChecker.visit_tryexcept
Calls: 314, Exceptions: 3, Paths: 23Back
Path 1: 217 calls (0.69)
TryExcept (217)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 2: 28 calls (0.09)
TryExcept (28)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 3: 16 calls (0.05)
TryExcept (16)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 4: 11 calls (0.04)
TryExcept (11)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 5: 9 calls (0.03)
TryExcept (9)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 6: 7 calls (0.02)
TryExcept (7)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 7: 3 calls (0.01)
TryExcept (3)
NameInferenceError (2) InferenceError (1)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 8: 3 calls (0.01)
TryExcept (3)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 9: 2 calls (0.01)
TryExcept (2)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 10: 2 calls (0.01)
TryExcept (2)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 11: 2 calls (0.01)
TryExcept (2)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 12: 2 calls (0.01)
TryExcept (2)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 13: 2 calls (0.01)
TryExcept (2)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 14: 1 calls (0.0)
TryExcept (1)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 15: 1 calls (0.0)
TryExcept (1)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 16: 1 calls (0.0)
TryExcept (1)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 17: 1 calls (0.0)
TryExcept (1)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 18: 1 calls (0.0)
TryExcept (1)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 19: 1 calls (0.0)
TryExcept (1)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 20: 1 calls (0.0)
TryExcept (1)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 21: 1 calls (0.0)
TryExcept (1)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 22: 1 calls (0.0)
TryExcept (1)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]
Path 23: 1 calls (0.0)
TryExcept (1)
1@utils.only_required_for_messages(
2 "bare-except",
3 "broad-exception-caught",
4 "try-except-raise",
5 "binary-op-exception",
6 "bad-except-order",
7 "catching-non-exception",
8 "duplicate-except",
9 )
10 def visit_tryexcept(self, node: nodes.TryExcept) -> None:
11 """Check for empty except."""
12 self._check_try_except_raise(node)
13 exceptions_classes: list[Any] = []
14 nb_handlers = len(node.handlers)
15 for index, handler in enumerate(node.handlers):
16 if handler.type is None:
17 if not _is_raising(handler.body):
18 self.add_message("bare-except", node=handler, confidence=HIGH)
19
20 # check if an "except:" is followed by some other
21 # except
22 if index < (nb_handlers - 1):
23 msg = "empty except clause should always appear last"
24 self.add_message(
25 "bad-except-order", node=node, args=msg, confidence=HIGH
26 )
27
28 elif isinstance(handler.type, nodes.BoolOp):
29 self.add_message(
30 "binary-op-exception",
31 node=handler,
32 args=handler.type.op,
33 confidence=HIGH,
34 )
35 else:
36 try:
37 exceptions = list(_annotated_unpack_infer(handler.type))
38 except astroid.InferenceError:
39 continue
40
41 for part, exception in exceptions:
42 if isinstance(
43 exception, astroid.Instance
44 ) and utils.inherit_from_std_ex(exception):
45 exception = exception._proxied
46
47 self._check_catching_non_exception(handler, exception, part)
48
49 if not isinstance(exception, nodes.ClassDef):
50 continue
51
52 exc_ancestors = [
53 anc
54 for anc in exception.ancestors()
55 if isinstance(anc, nodes.ClassDef)
56 ]
57
58 for previous_exc in exceptions_classes:
59 if previous_exc in exc_ancestors:
60 msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
61 self.add_message(
62 "bad-except-order",
63 node=handler.type,
64 args=msg,
65 confidence=INFERENCE,
66 )
67 if self._is_overgeneral_exception(exception) and not _is_raising(
68 handler.body
69 ):
70 self.add_message(
71 "broad-exception-caught",
72 args=exception.name,
73 node=handler.type,
74 confidence=INFERENCE,
75 )
76
77 if exception in exceptions_classes:
78 self.add_message(
79 "duplicate-except",
80 args=exception.name,
81 node=handler.type,
82 confidence=INFERENCE,
83 )
84
85 exceptions_classes += [exc for _, exc in exceptions]