Passed
Push — master ( b69fb2...088870 )
by Alexander
02:33
created

NestedDefinitionChecker.visit_classdef()   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nop 2
1
# Copyright (c) 2019 Alexander Todorov <[email protected]>
2
3
# Licensed under the GPL 2.0: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
4
5
import astroid
6
7
from pylint import interfaces
8
from pylint import checkers
9
from pylint.checkers import utils
10
11
12
class NestedDefinitionChecker(checkers.BaseChecker):
13
    __implements__ = (interfaces.IAstroidChecker,)
14
15
    name = 'nested-definition-checker'
16
17
    msgs = {'E4491': ("Nested class definition found!",
18
                      'nested-class-found',
19
                      "Do not define classes inside other classes or functions!"),
20
            'E4492': ("Nested function definition found!",
21
                      'nested-function-found',
22
                      "Do not define functions inside other functions!")}
23
24
    @utils.check_messages('nested-function-found')
25
    def visit_functiondef(self, node):
26
        if not isinstance(node.parent, (astroid.Module, astroid.ClassDef)):
27
            self.add_message('nested-function-found', node=node)
28
29
    @utils.check_messages('nested-class-found')
30
    def visit_classdef(self, node):
31
        if not isinstance(node.parent, astroid.Module):
32
            self.add_message('nested-class-found', node=node)
33