Method: pylint.checkers.exceptions.ExceptionsChecker._check_catching_non_exception
Calls: 362, Exceptions: 0, Paths: 4Back
Path 1: 357 calls (0.99)
ExceptHandler (357)
ClassDef (357)
Name (351) Attribute (5) Call (1)
1def _check_catching_non_exception(
2 self,
3 handler: nodes.ExceptHandler,
4 exc: SuccessfulInferenceResult,
5 part: nodes.NodeNG,
6 ) -> None:
7 if isinstance(exc, nodes.Tuple):
8 # Check if it is a tuple of exceptions.
9 inferred = [utils.safe_infer(elt) for elt in exc.elts]
10 if any(node is astroid.Uninferable for node in inferred):
11 # Don't emit if we don't know every component.
12 return
13 if all(
14 node
15 and (utils.inherit_from_std_ex(node) or not utils.has_known_bases(node))
16 for node in inferred
17 ):
18 return
19
20 if not isinstance(exc, nodes.ClassDef):
21 # Don't emit the warning if the inferred stmt
22 # is None, but the exception handler is something else,
23 # maybe it was redefined.
24 if isinstance(exc, nodes.Const) and exc.value is None:
25 if (
26 isinstance(handler.type, nodes.Const) and handler.type.value is None
27 ) or handler.type.parent_of(exc):
28 # If the exception handler catches None or
29 # the exception component, which is None, is
30 # defined by the entire exception handler, then
31 # emit a warning.
32 self.add_message(
33 "catching-non-exception",
34 node=handler.type,
35 args=(part.as_string(),),
36 )
37 else:
38 self.add_message(
39 "catching-non-exception",
40 node=handler.type,
41 args=(part.as_string(),),
42 )
43 return
44
45 if (
46 not utils.inherit_from_std_ex(exc)
47 and exc.name not in self._builtin_exceptions
48 ):
49 if utils.has_known_bases(exc):
50 self.add_message(
51 "catching-non-exception", node=handler.type, args=(exc.name,)
52 )
Path 2: 2 calls (0.01)
ExceptHandler (2)
FunctionDef (1) UnionType (1)
Name (1) BinOp (1)
None (2)
1def _check_catching_non_exception(
2 self,
3 handler: nodes.ExceptHandler,
4 exc: SuccessfulInferenceResult,
5 part: nodes.NodeNG,
6 ) -> None:
7 if isinstance(exc, nodes.Tuple):
8 # Check if it is a tuple of exceptions.
9 inferred = [utils.safe_infer(elt) for elt in exc.elts]
10 if any(node is astroid.Uninferable for node in inferred):
11 # Don't emit if we don't know every component.
12 return
13 if all(
14 node
15 and (utils.inherit_from_std_ex(node) or not utils.has_known_bases(node))
16 for node in inferred
17 ):
18 return
19
20 if not isinstance(exc, nodes.ClassDef):
21 # Don't emit the warning if the inferred stmt
22 # is None, but the exception handler is something else,
23 # maybe it was redefined.
24 if isinstance(exc, nodes.Const) and exc.value is None:
25 if (
26 isinstance(handler.type, nodes.Const) and handler.type.value is None
27 ) or handler.type.parent_of(exc):
28 # If the exception handler catches None or
29 # the exception component, which is None, is
30 # defined by the entire exception handler, then
31 # emit a warning.
32 self.add_message(
33 "catching-non-exception",
34 node=handler.type,
35 args=(part.as_string(),),
36 )
37 else:
38 self.add_message(
39 "catching-non-exception",
40 node=handler.type,
41 args=(part.as_string(),),
42 )
43 return
44
45 if (
46 not utils.inherit_from_std_ex(exc)
47 and exc.name not in self._builtin_exceptions
48 ):
49 if utils.has_known_bases(exc):
50 self.add_message(
51 "catching-non-exception", node=handler.type, args=(exc.name,)
52 )
Path 3: 2 calls (0.01)
ExceptHandler (2)
Tuple (2)
Name (1) BinOp (1)
None (2)
1def _check_catching_non_exception(
2 self,
3 handler: nodes.ExceptHandler,
4 exc: SuccessfulInferenceResult,
5 part: nodes.NodeNG,
6 ) -> None:
7 if isinstance(exc, nodes.Tuple):
8 # Check if it is a tuple of exceptions.
9 inferred = [utils.safe_infer(elt) for elt in exc.elts]
10 if any(node is astroid.Uninferable for node in inferred):
11 # Don't emit if we don't know every component.
12 return
13 if all(
14 node
15 and (utils.inherit_from_std_ex(node) or not utils.has_known_bases(node))
16 for node in inferred
17 ):
18 return
19
20 if not isinstance(exc, nodes.ClassDef):
21 # Don't emit the warning if the inferred stmt
22 # is None, but the exception handler is something else,
23 # maybe it was redefined.
24 if isinstance(exc, nodes.Const) and exc.value is None:
25 if (
26 isinstance(handler.type, nodes.Const) and handler.type.value is None
27 ) or handler.type.parent_of(exc):
28 # If the exception handler catches None or
29 # the exception component, which is None, is
30 # defined by the entire exception handler, then
31 # emit a warning.
32 self.add_message(
33 "catching-non-exception",
34 node=handler.type,
35 args=(part.as_string(),),
36 )
37 else:
38 self.add_message(
39 "catching-non-exception",
40 node=handler.type,
41 args=(part.as_string(),),
42 )
43 return
44
45 if (
46 not utils.inherit_from_std_ex(exc)
47 and exc.name not in self._builtin_exceptions
48 ):
49 if utils.has_known_bases(exc):
50 self.add_message(
51 "catching-non-exception", node=handler.type, args=(exc.name,)
52 )
Path 4: 1 calls (0.0)
ExceptHandler (1)
ClassDef (1)
Name (1)
1def _check_catching_non_exception(
2 self,
3 handler: nodes.ExceptHandler,
4 exc: SuccessfulInferenceResult,
5 part: nodes.NodeNG,
6 ) -> None:
7 if isinstance(exc, nodes.Tuple):
8 # Check if it is a tuple of exceptions.
9 inferred = [utils.safe_infer(elt) for elt in exc.elts]
10 if any(node is astroid.Uninferable for node in inferred):
11 # Don't emit if we don't know every component.
12 return
13 if all(
14 node
15 and (utils.inherit_from_std_ex(node) or not utils.has_known_bases(node))
16 for node in inferred
17 ):
18 return
19
20 if not isinstance(exc, nodes.ClassDef):
21 # Don't emit the warning if the inferred stmt
22 # is None, but the exception handler is something else,
23 # maybe it was redefined.
24 if isinstance(exc, nodes.Const) and exc.value is None:
25 if (
26 isinstance(handler.type, nodes.Const) and handler.type.value is None
27 ) or handler.type.parent_of(exc):
28 # If the exception handler catches None or
29 # the exception component, which is None, is
30 # defined by the entire exception handler, then
31 # emit a warning.
32 self.add_message(
33 "catching-non-exception",
34 node=handler.type,
35 args=(part.as_string(),),
36 )
37 else:
38 self.add_message(
39 "catching-non-exception",
40 node=handler.type,
41 args=(part.as_string(),),
42 )
43 return
44
45 if (
46 not utils.inherit_from_std_ex(exc)
47 and exc.name not in self._builtin_exceptions
48 ):
49 if utils.has_known_bases(exc):
50 self.add_message(
51 "catching-non-exception", node=handler.type, args=(exc.name,)
52 )