forked from openvinotoolkit/open_model_zoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpylint_checkers.py
144 lines (111 loc) · 4 KB
/
pylint_checkers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
Copyright (c) 2019 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import astroid
from pylint.checkers import BaseChecker
from pylint.interfaces import IAstroidChecker, IRawChecker
class BackslashChecker(BaseChecker):
"""
Checks for line continuations with '\' instead of using triple quoted string or parenthesis.
"""
__implements__ = IRawChecker
name = 'backslash'
msgs = {
'W9901': (
'use of \\ for line continuation', 'backslash-line-continuation',
'Used when a \\ is used for a line continuation instead of using triple quoted string or parenthesis.'
),
}
options = ()
def process_module(self, node):
with node.stream() as stream:
for (line_number, line) in enumerate(stream):
if not line.decode().rstrip().endswith('\\'):
continue
self.add_message('backslash-line-continuation', line=line_number)
class AbsoluteImportsChecker(BaseChecker):
"""
Check for absolute import from the same package.
"""
__implements__ = IAstroidChecker
name = 'absolute-imports'
priority = -1
msgs = {
'W9902': (
'absolute import from same package', 'package-absolute-imports',
'Used when module of same package imported using absolute import'
)
}
def visit_importfrom(self, node):
node_package = self._node_package(node)
import_name = node.modname
if import_name.startswith(node_package):
self.add_message('package-absolute-imports', node=node)
@staticmethod
def _node_package(node):
return node.scope().name.split('.')[0]
class StringFormatChecker(BaseChecker):
"""
Check for absolute import from the same package.
"""
__implements__ = IAstroidChecker
name = 'string-format'
priority = -1
msgs = {
'W9903': (
'use of "%" for string formatting', 'deprecated-string-format',
'"%" operator is used for string formatting instead of str.format method'
)
}
def visit_binop(self, node):
if node.op != '%':
return
left = node.left
if not (isinstance(left, astroid.Const) and isinstance(left.value, str)):
return
self.add_message('deprecated-string-format', node=node)
class BadFunctionChecker(BaseChecker):
"""
Check for absolute import from the same package.
"""
__implements__ = IAstroidChecker
name = 'bad-function'
priority = -1
msgs = {'W9904': ('using prohibited function', 'bad-function-call', '')}
options = (
(
'bad-functions',
{
'default': '',
'help': 'List of prohibited functions',
},
),
)
def visit_call(self, node):
bad_functions = set(f.strip() for f in self.config.bad_functions.split(','))
if self._function_name(node) in bad_functions:
self.add_message('bad-function-call', node=node)
@staticmethod
def _function_name(node):
func = node.func
if hasattr(func, 'attrname'):
return func.attrname
elif hasattr(func, 'name'):
return func.name
def register(linter):
"""
Required method to auto register this checker.
"""
linter.register_checker(BackslashChecker(linter))
linter.register_checker(AbsoluteImportsChecker(linter))
linter.register_checker(StringFormatChecker(linter))
linter.register_checker(BadFunctionChecker(linter))