forked from reddit/baseplate.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pylint_baseplate_plugin.py
42 lines (33 loc) · 1.08 KB
/
pylint_baseplate_plugin.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
from pylint.checkers import BaseChecker
from pylint.interfaces import IAstroidChecker
class NoRelativeImportsChecker(BaseChecker):
__implements__ = IAstroidChecker
name = "no-relative-imports"
priority = -1
msgs = {
"W5403": (
"Explicit relative import %r should be %r",
"explicit-relative-import",
"All imports should be absolute",
)
}
def __init__(self, linter=None):
super().__init__(linter)
self.module = None
def visit_module(self, node):
self.module = node
def leave_module(self, node):
self.module = None
def visit_importfrom(self, node):
if node.level is not None:
self.add_message(
"explicit-relative-import",
node=node,
args=(
"." * node.level + node.modname,
self.module.relative_to_absolute_name(node.modname, node.level),
),
)
def register(linter):
checker = NoRelativeImportsChecker(linter)
linter.register_checker(checker)