示例#1
0
 def _get_full_name(self, node):
     try:
         func = utils.safe_infer(node.func)
         if func.name.__str__() == 'Uninferable':
             return
     except Exception:
         func = None
     if func is None:
         return
     ret = []
     depth = 0
     while func is not None:
         depth += 1
         if depth > self.max_depth:
             # Prevent endless loop
             return
         try:
             ret.append(func.name)
         except AttributeError:
             return
         func = func.parent
     # ret will contain the levels of the function from last to first (e.g.
     # ['walk', 'os']. Reverse it and join with dots to get the correct
     # full name for the function.
     return '.'.join(ret[::-1])
示例#2
0
    def visit_call(self, node):
        func = utils.safe_infer(node.func)
        if isinstance(func, astroid.BoundMethod) and func.name == 'format':
            # If there's a .format() call, run the code below

            if isinstance(node.func.expr, (astroid.Name, astroid.Const)):
                # This is for:
                #   foo = 'Foo {} bar'
                #   print(foo.format(blah)
                for inferred in node.func.expr.infer():
                    if not hasattr(inferred, 'value'):
                        # If there's no value attribute, it's not worth
                        # checking.
                        continue

                    if not isinstance(inferred.value, six.string_types):
                        # If it's not a string, continue
                        continue

                    if '!r}' in inferred.value:
                        self.add_message('E1322',
                                         node=inferred,
                                         args=inferred.value)

                    if BAD_FORMATTING_SLOT.findall(inferred.value):
                        if self.config.un_indexed_curly_braces_always_error or \
                                sys.version_info[:2] < (2, 7):
                            self.add_message('E1320',
                                             node=inferred,
                                             args=inferred.value)
                        elif six.PY2:
                            self.add_message('W1320',
                                             node=inferred,
                                             args=inferred.value)

                try:
                    # Walk back up until no parents are found and look for a
                    # logging.RootLogger instance in the parent types
                    ptr = node
                    while True:
                        parent = ptr.parent
                        for inferred in parent.func.expr.infer():
                            try:
                                instance_type = inferred.pytype().split('.')[0]
                            except TypeError:
                                continue
                            if instance_type == 'logging':
                                self.add_message(
                                    'E1323',
                                    node=node,
                                    args=node.as_string(),
                                )
                                break
                        ptr = parent
                except (AttributeError, InferenceError, NameInferenceError):
                    pass

            elif not hasattr(node.func.expr, 'value'):
                # If it does not have an value attribute, it's not worth
                # checking
                return
            elif isinstance(node.func.expr.value, astroid.Name):
                # No need to check these either
                return
            elif BAD_FORMATTING_SLOT.findall(node.func.expr.value):
                if self.config.un_indexed_curly_braces_always_error or \
                        sys.version_info[:2] < (2, 7):
                    msgid = 'E1320'
                else:
                    msgid = 'W1320'
                self.add_message('E1320', node=node, args=node.func.expr.value)