Example #1
0
    def _check_name(self, node_type, name, node):
        """check for a name using the type's regexp"""
        if is_inside_except(node):
            clobbering, _ = clobber_in_except(node)
            if clobbering:
                return
        if name in self.config.good_names:
            return
        if name in self.config.bad_names:
            self.stats['badname_' + node_type] += 1
            self.add_message('blacklisted-name', node=node, args=name)
            return
        regexp = getattr(self.config, node_type + '_rgx')
        match = regexp.match(name)

        if self._is_multi_naming_match(match):
            name_group = self._find_name_group(node_type)
            if name_group not in self._name_category:
                self._name_category[name_group] = match.lastgroup
            elif self._name_category[name_group] != match.lastgroup:
                match = None

        if match is None:
            type_label = _NAME_TYPES[node_type][1]
            hint = ''
            if self.config.include_naming_hint:
                hint = ' (hint: %s)' % (getattr(self.config, node_type + '_name_hint'))
            self.add_message('invalid-name', node=node, args=(type_label, name, hint))
            self.stats['badname_' + node_type] += 1
Example #2
0
    def visit_function(self, node):
        """visit function: update consumption analysis variable and check locals
        """
        self._to_consume.append((copy(node.locals), {}, 'function'))
        if not (self.linter.is_message_enabled('redefined-outer-name')
                or self.linter.is_message_enabled('redefined-builtin')):
            return
        globs = node.root().globals
        for name, stmt in node.items():
            if is_inside_except(stmt):
                continue
            if name in globs and not isinstance(stmt, astroid.Global):
                definition = globs[name][0]
                if (isinstance(definition, astroid.From)
                        and definition.modname == FUTURE):
                    # It is a __future__ directive, not a symbol.
                    continue

                line = definition.fromlineno
                dummy_rgx = self.config.dummy_variables_rgx
                if not dummy_rgx.match(name):
                    self.add_message('redefined-outer-name',
                                     args=(name, line),
                                     node=stmt)
            elif is_builtin(name):
                # do not print Redefining builtin for additional builtins
                self.add_message('redefined-builtin', args=name, node=stmt)
Example #3
0
 def visit_module(self, node):
     """visit module : update consumption analysis variable
     checks globals doesn't overrides builtins
     """
     self._to_consume = [(copy(node.locals), {}, 'module')]
     for name, stmts in six.iteritems(node.locals):
         if is_builtin(name) and not is_inside_except(stmts[0]):
             # do not print Redefining builtin for additional builtins
             self.add_message('redefined-builtin', args=name, node=stmts[0])
Example #4
0
 def visit_module(self, node):
     """visit module : update consumption analysis variable
     checks globals doesn't overrides builtins
     """
     self._to_consume = [(copy(node.locals), {}, 'module')]
     for name, stmts in six.iteritems(node.locals):
         if is_builtin(name) and not is_inside_except(stmts[0]):
             # do not print Redefining builtin for additional builtins
             self.add_message('redefined-builtin', args=name, node=stmts[0])
Example #5
0
 def visit_function(self, node):
     """visit function: update consumption analysis variable and check locals
     """
     self._to_consume.append((copy(node.locals), {}, 'function'))
     if not set(('W0621', 'W0622')) & self.active_msgs:
         return
     globs = node.root().globals
     for name, stmt in node.items():
         if is_inside_except(stmt):
             continue
         if name in globs and not isinstance(stmt, astng.Global):
             line = globs[name][0].fromlineno
             self.add_message('W0621', args=(name, line), node=stmt)
         elif is_builtin(name):
             # do not print Redefining builtin for additional builtins
             self.add_message('W0622', args=name, node=stmt)
Example #6
0
 def visit_function(self, node):
     """visit function: update consumption analysis variable and check locals
     """
     self._to_consume.append((copy(node.locals), {}, 'function'))
     if not set(('W0621', 'W0622')) & self.active_msgs:
         return
     globs = node.root().globals
     for name, stmt in node.items():
         if is_inside_except(stmt):
             continue
         if name in globs and not isinstance(stmt, astng.Global):
             line = globs[name][0].fromlineno
             self.add_message('W0621', args=(name, line), node=stmt)
         elif is_builtin(name):
             # do not print Redefining builtin for additional builtins
             self.add_message('W0622', args=name, node=stmt)
Example #7
0
    def _check_name(self, node_type, name, node):
        """check for a name using the type's regexp"""
        if is_inside_except(node):
            clobbering, _ = clobber_in_except(node)
            if clobbering:
                return

        if name in self.config.good_names:
            return
        if name in self.config.bad_names:
            self.stats['badname_' + node_type] += 1
            self.add_message('C0102', node=node, args=name)
            return
        regexp = getattr(self.config, node_type + '_rgx')
        if regexp.match(name) is None:
            self.add_message('C0103', node=node, args=(name, regexp.pattern))
            self.stats['badname_' + node_type] += 1
Example #8
0
 def visit_function(self, node):
     """visit function: update consumption analysis variable and check locals
     """
     self._to_consume.append((copy(node.locals), {}, 'function'))
     if not (self.linter.is_message_enabled('redefined-outer-name') or
             self.linter.is_message_enabled('redefined-builtin')):
         return
     globs = node.root().globals
     for name, stmt in node.items():
         if is_inside_except(stmt):
             continue
         if name in globs and not isinstance(stmt, astroid.Global):
             line = globs[name][0].fromlineno
             dummy_rgx = self.config.dummy_variables_rgx
             if not dummy_rgx.match(name):
                 self.add_message('redefined-outer-name', args=(name, line), node=stmt)
         elif is_builtin(name):
             # do not print Redefining builtin for additional builtins
             self.add_message('redefined-builtin', args=name, node=stmt)
Example #9
0
 def _check_name(self, node_type, name, node):
     """check for a name using the type's regexp"""
     if is_inside_except(node):
         clobbering, _ = clobber_in_except(node)
         if clobbering:
             return
     if name in self.config.good_names:
         return
     if name in self.config.bad_names:
         self.stats['badname_' + node_type] += 1
         self.add_message('C0102', node=node, args=name)
         return
     regexp = getattr(self.config, node_type + '_rgx')
     if regexp.match(name) is None:
         type_label = {'inlinedvar': 'inlined variable',
                       'const': 'constant',
                       'attr': 'attribute',
                       }.get(node_type, node_type)
         self.add_message('C0103', node=node, args=(name, type_label, regexp.pattern))
         self.stats['badname_' + node_type] += 1