def leave_function(self, node):
     """leave function: check function's locals are consumed
     """
     not_consumed = self._to_consume.pop()[0]
     self._vars.pop(0)
     is_method = node.is_method()
     klass = node.parent.frame()
     # don't check arguments of abstract methods or within an interface
     if is_method and (klass.type == 'interface' or node.is_abstract()):
         return
     if is_error(node):
         return
     authorized_rgx = self.config.dummy_variables_rgx
     for name, stmts in not_consumed.items():
         # ignore some special names specified by user configuration
         if authorized_rgx.match(name):
             continue
         # ignore names imported by the global statement
         # FIXME: should only ignore them if it's assigned latter
         stmt = stmts[0]
         if isinstance(stmt, astng.Global):
             continue
         # care about functions with unknown argument (builtins)
         if node.argnames is not None and name in node.argnames:
             # don't warn if the first argument of a method is not used
             if is_method and node.argnames and name == node.argnames[0]:
                 continue
             # don't check callback arguments
             if node.name.startswith('cb_') or \
                    node.name.endswith('_cb'):
                 continue
             self.add_message('W0613', args=name, node=node)
         else:
             self.add_message('W0612', args=name, node=stmt)
Beispiel #2
0
 def leave_function(self, node):
     """leave function: check function's locals are consumed"""
     not_consumed = self._to_consume.pop()[0]
     self._vars.pop(0)
     if not set(('W0612', 'W0613')) & self.active_msgs:
         return
     # don't check arguments of function which are only raising an exception
     if is_error(node):
         return
     # don't check arguments of abstract methods or within an interface
     is_method = node.is_method()
     klass = node.parent.frame()
     if is_method and (klass.type == 'interface' or node.is_abstract()):
         return
     authorized_rgx = self.config.dummy_variables_rgx
     called_overridden = False
     argnames = node.argnames()
     for name, stmts in not_consumed.items():
         # ignore some special names specified by user configuration
         if authorized_rgx.match(name):
             continue
         # ignore names imported by the global statement
         # FIXME: should only ignore them if it's assigned latter
         stmt = stmts[0]
         if isinstance(stmt, astng.Global):
             continue
         # care about functions with unknown argument (builtins)
         if name in argnames:
             if is_method:
                 # don't warn for the first argument of a (non static) method
                 if node.type != 'staticmethod' and name == argnames[0]:
                     continue
                 # don't warn for argument of an overridden method
                 if not called_overridden:
                     overridden = overridden_method(klass, node.name)
                     called_overridden = True
                 if overridden is not None and name in overridden.argnames(
                 ):
                     continue
                 if node.name in PYMETHODS and node.name not in ('__init__',
                                                                 '__new__'):
                     continue
             # don't check callback arguments XXX should be configurable
             if node.name.startswith('cb_') or node.name.endswith('_cb'):
                 continue
             self.add_message('W0613', args=name, node=stmt)
         else:
             self.add_message('W0612', args=name, node=stmt)
Beispiel #3
0
 def leave_function(self, node):
     """leave function: check function's locals are consumed"""
     not_consumed = self._to_consume.pop()[0]
     if not set(('W0612', 'W0613')) & self.active_msgs:
         return
     # don't check arguments of function which are only raising an exception
     if is_error(node):
         return
     # don't check arguments of abstract methods or within an interface
     is_method = node.is_method()
     klass = node.parent.frame()
     if is_method and (klass.type == 'interface' or node.is_abstract()):
         return
     authorized_rgx = self.config.dummy_variables_rgx
     called_overridden = False
     argnames = node.argnames()
     for name, stmts in not_consumed.iteritems():
         # ignore some special names specified by user configuration
         if authorized_rgx.match(name):
             continue
         # ignore names imported by the global statement
         # FIXME: should only ignore them if it's assigned latter
         stmt = stmts[0]
         if isinstance(stmt, astroid.Global):
             continue
         # care about functions with unknown argument (builtins)
         if name in argnames:
             if is_method:
                 # don't warn for the first argument of a (non static) method
                 if node.type != 'staticmethod' and name == argnames[0]:
                     continue
                 # don't warn for argument of an overridden method
                 if not called_overridden:
                     overridden = overridden_method(klass, node.name)
                     called_overridden = True
                 if overridden is not None and name in overridden.argnames(
                 ):
                     continue
                 if node.name in PYMETHODS and node.name not in ('__init__',
                                                                 '__new__'):
                     continue
             # don't check callback arguments XXX should be configurable
             if node.name.startswith('cb_') or node.name.endswith('_cb'):
                 continue
             self.add_message('W0613', args=name, node=stmt)
         else:
             self.add_message('W0612', args=name, node=stmt)
Beispiel #4
0
    def leave_function(self, node):
        """leave function: check function's locals are consumed"""
        not_consumed = self._to_consume.pop()[0]
        if not (self.linter.is_message_enabled('unused-variable') or
                self.linter.is_message_enabled('unused-argument')):
            return
        # don't check arguments of function which are only raising an exception
        if is_error(node):
            return
        # don't check arguments of abstract methods or within an interface
        is_method = node.is_method()
        klass = node.parent.frame()
        if is_method and (klass.type == 'interface' or node.is_abstract()):
            return
        if is_method and isinstance(klass, astroid.Class):
            confidence = INFERENCE if has_known_bases(klass) else INFERENCE_FAILURE
        else:
            confidence = HIGH
        authorized_rgx = self.config.dummy_variables_rgx
        called_overridden = False
        argnames = node.argnames()
        global_names = set()
        nonlocal_names = set()
        for global_stmt in node.nodes_of_class(astroid.Global):
            global_names.update(set(global_stmt.names))
        for nonlocal_stmt in node.nodes_of_class(astroid.Nonlocal):
            nonlocal_names.update(set(nonlocal_stmt.names))

        for name, stmts in six.iteritems(not_consumed):
            # ignore some special names specified by user configuration
            if authorized_rgx.match(name):
                continue
            # ignore names imported by the global statement
            # FIXME: should only ignore them if it's assigned latter
            stmt = stmts[0]
            if isinstance(stmt, astroid.Global):
                continue
            if isinstance(stmt, (astroid.Import, astroid.From)):
                # Detect imports, assigned to global statements.
                if global_names:
                    skip = False
                    for import_name, import_alias in stmt.names:
                        # If the import uses an alias, check only that.
                        # Otherwise, check only the import name.
                        if import_alias:
                            if import_alias in global_names:
                                skip = True
                                break
                        elif import_name in global_names:
                            skip = True
                            break
                    if skip:
                        continue

            # care about functions with unknown argument (builtins)
            if name in argnames:
                if is_method:
                    # don't warn for the first argument of a (non static) method
                    if node.type != 'staticmethod' and name == argnames[0]:
                        continue
                    # don't warn for argument of an overridden method
                    if not called_overridden:
                        overridden = overridden_method(klass, node.name)
                        called_overridden = True
                    if overridden is not None and name in overridden.argnames():
                        continue
                    if node.name in PYMETHODS and node.name not in ('__init__', '__new__'):
                        continue
                # don't check callback arguments
                if any(node.name.startswith(cb) or node.name.endswith(cb)
                       for cb in self.config.callbacks):
                    continue
                self.add_message('unused-argument', args=name, node=stmt,
                                 confidence=confidence)
            else:
                if stmt.parent and isinstance(stmt.parent, astroid.Assign):
                    if name in nonlocal_names:
                        continue
                self.add_message('unused-variable', args=name, node=stmt)
Beispiel #5
0
    def leave_function(self, node):
        """leave function: check function's locals are consumed"""
        not_consumed = self._to_consume.pop()[0]
        if not (self.linter.is_message_enabled('unused-variable')
                or self.linter.is_message_enabled('unused-argument')):
            return
        # don't check arguments of function which are only raising an exception
        if is_error(node):
            return
        # don't check arguments of abstract methods or within an interface
        is_method = node.is_method()
        klass = node.parent.frame()
        if is_method and (klass.type == 'interface' or node.is_abstract()):
            return
        if is_method and isinstance(klass, astroid.Class):
            confidence = INFERENCE if has_known_bases(
                klass) else INFERENCE_FAILURE
        else:
            confidence = HIGH
        authorized_rgx = self.config.dummy_variables_rgx
        called_overridden = False
        argnames = node.argnames()
        global_names = set()
        nonlocal_names = set()
        for global_stmt in node.nodes_of_class(astroid.Global):
            global_names.update(set(global_stmt.names))
        for nonlocal_stmt in node.nodes_of_class(astroid.Nonlocal):
            nonlocal_names.update(set(nonlocal_stmt.names))

        for name, stmts in six.iteritems(not_consumed):
            # ignore some special names specified by user configuration
            if authorized_rgx.match(name):
                continue
            # ignore names imported by the global statement
            # FIXME: should only ignore them if it's assigned latter
            stmt = stmts[0]
            if isinstance(stmt, astroid.Global):
                continue
            if isinstance(stmt, (astroid.Import, astroid.From)):
                # Detect imports, assigned to global statements.
                if global_names:
                    skip = False
                    for import_name, import_alias in stmt.names:
                        # If the import uses an alias, check only that.
                        # Otherwise, check only the import name.
                        if import_alias:
                            if import_alias in global_names:
                                skip = True
                                break
                        elif import_name in global_names:
                            skip = True
                            break
                    if skip:
                        continue

            # care about functions with unknown argument (builtins)
            if name in argnames:
                if is_method:
                    # don't warn for the first argument of a (non static) method
                    if node.type != 'staticmethod' and name == argnames[0]:
                        continue
                    # don't warn for argument of an overridden method
                    if not called_overridden:
                        overridden = overridden_method(klass, node.name)
                        called_overridden = True
                    if overridden is not None and name in overridden.argnames(
                    ):
                        continue
                    if node.name in PYMETHODS and node.name not in ('__init__',
                                                                    '__new__'):
                        continue
                # don't check callback arguments XXX should be configurable
                if node.name.startswith('cb_') or node.name.endswith('_cb'):
                    continue
                self.add_message('unused-argument',
                                 args=name,
                                 node=stmt,
                                 confidence=confidence)
            else:
                if stmt.parent and isinstance(stmt.parent, astroid.Assign):
                    if name in nonlocal_names:
                        continue
                self.add_message('unused-variable', args=name, node=stmt)