예제 #1
0
def _builtin_lookup(node, name):
    values = node.locals.get(name, [])
    if not values:
        raise exceptions.AttributeInferenceError(attribute=name, target=node)

    return values
예제 #2
0
def _class_lookup(node, name):
    metaclass = node.metaclass()
    if metaclass is None:
        raise exceptions.AttributeInferenceError(attribute=name, target=node)

    return _lookup_in_mro(metaclass, name)
예제 #3
0
파일: objects.py 프로젝트: mgorny/astroid
    def igetattr(self, name, context=None):
        """Retrieve the inferred values of the given attribute name."""

        if name in self.special_attributes:
            yield self.special_attributes.lookup(name)
            return

        try:
            mro = self.super_mro()
        # Don't let invalid MROs or invalid super calls
        # leak out as is from this function.
        except exceptions.SuperError as exc:
            raise exceptions.AttributeInferenceError(
                ("Lookup for {name} on {target!r} because super call {super!r} "
                 "is invalid."),
                target=self,
                attribute=name,
                context=context,
                super_=exc.super_,
            ) from exc
        except exceptions.MroError as exc:
            raise exceptions.AttributeInferenceError(
                ("Lookup for {name} on {target!r} failed because {cls!r} has an "
                 "invalid MRO."),
                target=self,
                attribute=name,
                context=context,
                mros=exc.mros,
                cls=exc.cls,
            ) from exc
        found = False
        for cls in mro:
            if name not in cls.locals:
                continue

            found = True
            for inferred in bases._infer_stmts([cls[name]],
                                               context,
                                               frame=self):
                if not isinstance(inferred, scoped_nodes.FunctionDef):
                    yield inferred
                    continue

                # We can obtain different descriptors from a super depending
                # on what we are accessing and where the super call is.
                if inferred.type == "classmethod":
                    yield bases.BoundMethod(inferred, cls)
                elif self._scope.type == "classmethod" and inferred.type == "method":
                    yield inferred
                elif self._class_based or inferred.type == "staticmethod":
                    yield inferred
                elif isinstance(inferred, Property):
                    function = inferred.function
                    try:
                        yield from function.infer_call_result(caller=self,
                                                              context=context)
                    except exceptions.InferenceError:
                        yield util.Uninferable
                elif bases._is_property(inferred):
                    # TODO: support other descriptors as well.
                    try:
                        yield from inferred.infer_call_result(self, context)
                    except exceptions.InferenceError:
                        yield util.Uninferable
                else:
                    yield bases.BoundMethod(inferred, cls)

        if not found:
            raise exceptions.AttributeInferenceError(target=self,
                                                     attribute=name,
                                                     context=context)
예제 #4
0
    def igetattr(self, name, context=None):
        """Retrieve the inferred values of the given attribute name."""

        local_name = self._model.get(name)
        if local_name:
            yield local_name
            return

        try:
            mro = self.super_mro()
        # Don't let invalid MROs or invalid super calls
        # leak out as is from this function.
        except exceptions.SuperError as exc:
            util.reraise(
                exceptions.AttributeInferenceError((
                    'Lookup for {name} on {target!r} because super call {super!r} '
                    'is invalid.'),
                                                   target=self,
                                                   attribute=name,
                                                   context=context,
                                                   super_=exc.super_))
        except exceptions.MroError as exc:
            util.reraise(
                exceptions.AttributeInferenceError((
                    'Lookup for {name} on {target!r} failed because {cls!r} has an '
                    'invalid MRO.'),
                                                   target=self,
                                                   attribute=name,
                                                   context=context,
                                                   mros=exc.mros,
                                                   cls=exc.cls))
        found = False
        for cls in mro:
            if name not in cls.locals:
                continue

            found = True
            for inferred in bases._infer_stmts([cls[name]],
                                               context,
                                               frame=self):
                if not isinstance(inferred, scoped_nodes.FunctionDef):
                    yield inferred
                    continue

                # We can obtain different descriptors from a super depending
                # on what we are accessing and where the super call is.
                if inferred.type == 'classmethod':
                    yield bases.BoundMethod(inferred, cls)
                elif self._scope.type == 'classmethod' and inferred.type == 'method':
                    yield inferred
                elif self._class_based or inferred.type == 'staticmethod':
                    yield inferred
                elif bases._is_property(inferred):
                    # TODO: support other descriptors as well.
                    for value in inferred.infer_call_result(self, context):
                        yield value
                else:
                    yield bases.BoundMethod(inferred, cls)

        if not found:
            raise exceptions.AttributeInferenceError(target=self,
                                                     attribute=name,
                                                     context=context)
예제 #5
0
 def attr_mro(self):
     if not self._instance.newstyle:
         raise exceptions.AttributeInferenceError(
             target=self._instance, attribute="mro"
         )
예제 #6
0
 def attr___path__(self):
     if not self._instance.package:
         raise exceptions.AttributeInferenceError(
             target=self._instance, attribute="__path__"
         )