Debian Codegraph @

saltpylint/virt.py

raw symbols SCIP
1 from typing import ClassVar
2
3 import astroid
4 from pylint.checkers import BaseChecker
5
6 VIRT_LOG = "log-in-virtual"
7
8
9 class VirtChecker(BaseChecker):
10 """checks for compliance inside __virtual__."""
11
12 name = "virt-checker"
13
14 msgs: ClassVar = {
15 "E1401": (
16 "Log statement detected inside __virtual__ function. Remove it.",
17 VIRT_LOG,
18 "Loader processes __virtual__ so logging not in scope",
19 ),
20 }
21 options = ()
22
23 priority = -1
24
25 def visit_functiondef(self, node):
26 """Verifies no logger statements inside __virtual__."""
27 if (
28 not isinstance(node, astroid.FunctionDef)
29 or node.is_method()
30 or node.type != "function"
31 or not node.body
32 ):
33 # only process functions
34 return
35
36 try:
37 if node.name != "__virtual__":
38 # only need to process the __virtual__ function
39 return
40 except AttributeError:
41 return
42
43 # walk contents of __virtual__ function
44 for child in node.get_children():
45 for functions in child.get_children():
46 if isinstance(functions, astroid.Call) and isinstance(
47 functions.func,
48 astroid.Attribute,
49 ):
50 try:
51 # Inspect each statement for an instance of 'logging'
52 for inferred in functions.func.expr.infer():
53 try:
54 instance_type = inferred.pytype().split(".")[0]
55 except TypeError:
56 continue
57 if instance_type == "logging":
58 self.add_message(VIRT_LOG, node=functions)
59 # Found logger, don't need to keep processing this line
60 break
61 except AttributeError:
62 # Not a log function
63 return
64
65
66 def register(linter):
67 """Required method to auto register this checker."""
68 linter.register_checker(VirtChecker(linter))