예제 #1
0
def instance_getitem(self, index, context=None):
    # Rewrap index to Const for this case
    new_context = contextmod.bind_context_to_node(context, self)
    if not context:
        context = new_context

    # Create a new callcontext for providing index as an argument.
    new_context.callcontext = contextmod.CallContext(args=[index])

    method = next(self.igetattr("__getitem__", context=context), None)
    if not isinstance(method, bases.BoundMethod):
        raise exceptions.InferenceError(
            "Could not find __getitem__ for {node!r}.",
            node=self,
            context=context)

    try:
        return next(method.infer_call_result(self, new_context))
    except StopIteration as exc:
        raise exceptions.InferenceError(
            message="Inference for {node!r}[{index!s}] failed.",
            node=self,
            index=index,
            context=context,
        ) from exc
예제 #2
0
def _invoke_binop_inference(instance, opnode, op, other, context, method_name):
    """Invoke binary operation inference on the given instance."""
    methods = dunder_lookup.lookup(instance, method_name)
    context = contextmod.bind_context_to_node(context, instance)
    method = methods[0]
    inferred = next(method.infer(context=context))
    if inferred is util.Uninferable:
        raise exceptions.InferenceError
    return instance.infer_binary_op(opnode, op, other, context, inferred)
예제 #3
0
파일: inference.py 프로젝트: gtt116/vimrc
def _invoke_binop_inference(instance, opnode, op, other, context, method_name):
    """Invoke binary operation inference on the given instance."""
    methods = dunder_lookup.lookup(instance, method_name)
    context = contextmod.bind_context_to_node(context, instance)
    method = methods[0]
    inferred = next(method.infer(context=context))
    if inferred is util.Uninferable:
        raise exceptions.InferenceError
    return instance.infer_binary_op(opnode, op, other, context, inferred)
예제 #4
0
    def infer_call_result(self, caller, context=None):
        context = bind_context_to_node(context, self.bound)
        if (self.bound.__class__.__name__ == "ClassDef"
                and self.bound.name == "type" and self.name == "__new__"
                and len(caller.args) == 4):
            # Check if we have a ``type.__new__(mcs, name, bases, attrs)`` call.
            new_cls = self._infer_type_new_call(caller, context)
            if new_cls:
                return iter((new_cls, ))

        return super().infer_call_result(caller, context)
예제 #5
0
 def infer_call_result(self, caller, context=None):
     """infer what a class instance is returning when called"""
     context = bind_context_to_node(context, self)
     inferred = False
     for node in self._proxied.igetattr("__call__", context):
         if node is Uninferable or not node.callable():
             continue
         for res in node.infer_call_result(caller, context):
             inferred = True
             yield res
     if not inferred:
         raise InferenceError(node=self, caller=caller, context=context)
예제 #6
0
    def infer_call_result(self, caller, context=None):
        context = contextmod.bind_context_to_node(context, self.bound)
        if (self.bound.__class__.__name__ == 'ClassDef'
                and self.bound.name == 'type'
                and self.name == '__new__'
                and len(caller.args) == 4):
            # Check if we have an ``type.__new__(mcs, name, bases, attrs)`` call.
            new_cls = self._infer_type_new_call(caller, context)
            if new_cls:
                return iter((new_cls, ))

        return super(BoundMethod, self).infer_call_result(caller, context)
예제 #7
0
 def infer_call_result(self, caller, context=None):
     """infer what a class instance is returning when called"""
     context = contextmod.bind_context_to_node(context, self)
     inferred = False
     for node in self._proxied.igetattr("__call__", context):
         if node is util.Uninferable or not node.callable():
             continue
         for res in node.infer_call_result(caller, context):
             inferred = True
             yield res
     if not inferred:
         raise exceptions.InferenceError(node=self, caller=caller, context=context)
예제 #8
0
파일: bases.py 프로젝트: gtt116/vimrc
    def infer_call_result(self, caller, context=None):
        context = contextmod.bind_context_to_node(context, self.bound)
        if (self.bound.__class__.__name__ == 'ClassDef'
                and self.bound.name == 'type'
                and self.name == '__new__'
                and len(caller.args) == 4):
            # Check if we have an ``type.__new__(mcs, name, bases, attrs)`` call.
            new_cls = self._infer_type_new_call(caller, context)
            if new_cls:
                return iter((new_cls, ))

        return super(BoundMethod, self).infer_call_result(caller, context)
예제 #9
0
def _invoke_binop_inference(instance, opnode, op, other, context, method_name):
    """Invoke binary operation inference on the given instance."""
    methods = dunder_lookup.lookup(instance, method_name)
    context = bind_context_to_node(context, instance)
    method = methods[0]
    context.callcontext.callee = method
    try:
        inferred = next(method.infer(context=context))
    except StopIteration as e:
        raise InferenceError(node=method, context=context) from e
    if inferred is util.Uninferable:
        raise InferenceError
    return instance.infer_binary_op(opnode, op, other, context, inferred)
예제 #10
0
파일: bases.py 프로젝트: DudeNr33/astroid
 def getitem(self, index, context=None):
     # TODO: Rewrap index to Const for this case
     new_context = contextmod.bind_context_to_node(context, self)
     if not context:
         context = new_context
     # Create a new CallContext for providing index as an argument.
     new_context.callcontext = contextmod.CallContext(args=[index])
     method = next(self.igetattr("__getitem__", context=context), None)
     if not isinstance(method, BoundMethod):
         raise exceptions.InferenceError(
             "Could not find __getitem__ for {node!r}.",
             node=self,
             context=context)
     return next(method.infer_call_result(self, new_context))
예제 #11
0
    def infer_call_result(self, caller, context=None):
        context = contextmod.bind_context_to_node(context, self.bound)
        if (self.bound.__class__.__name__ == 'ClassDef'
                and self.bound.name == 'type' and self.name == '__new__'
                and len(caller.args) == 4
                # TODO(cpopa): this check shouldn't be needed.
                and self._proxied.parent.frame().qname()
                == '%s.object' % BUILTINS):

            # Check if we have an ``type.__new__(mcs, name, bases, attrs)`` call.
            new_cls = self._infer_type_new_call(caller, context)
            if new_cls:
                return iter((new_cls, ))

        return super(BoundMethod, self).infer_call_result(caller, context)
예제 #12
0
 def getitem(self, index, context=None):
     new_context = bind_context_to_node(context, self)
     if not context:
         context = new_context
     method = next(self.igetattr("__getitem__", context=context), None)
     # Create a new CallContext for providing index as an argument.
     new_context.callcontext = CallContext(args=[index], callee=method)
     if not isinstance(method, BoundMethod):
         raise InferenceError("Could not find __getitem__ for {node!r}.",
                              node=self,
                              context=context)
     if len(method.args.arguments) != 2:  # (self, index)
         raise AstroidTypeError(
             "__getitem__ for {node!r} does not have correct signature",
             node=self,
             context=context,
         )
     return next(method.infer_call_result(self, new_context), None)
예제 #13
0
def _invoke_binop_inference(instance, opnode, op, other, context, method_name):
    """Invoke binary operation inference on the given instance."""
    methods = dunder_lookup.lookup(instance, method_name)
    context = bind_context_to_node(context, instance)
    method = methods[0]
    context.callcontext.callee = method

    if (isinstance(instance, nodes.Const) and isinstance(instance.value, str)
            and op == "%"):
        return iter(
            _infer_old_style_string_formatting(instance, other, context))

    try:
        inferred = next(method.infer(context=context))
    except StopIteration as e:
        raise InferenceError(node=method, context=context) from e
    if inferred is util.Uninferable:
        raise InferenceError
    return instance.infer_binary_op(opnode, op, other, context, inferred)
예제 #14
0
파일: inference.py 프로젝트: gtt116/vimrc
def instance_getitem(self, index, context=None):
    # Rewrap index to Const for this case
    new_context = contextmod.bind_context_to_node(context, self)
    if not context:
        context = new_context

    # Create a new callcontext for providing index as an argument.
    new_context.callcontext = contextmod.CallContext(args=[index])

    method = next(self.igetattr('__getitem__', context=context), None)
    if not isinstance(method, bases.BoundMethod):
        raise exceptions.InferenceError(
            'Could not find __getitem__ for {node!r}.',
            node=self, context=context)

    try:
        return next(method.infer_call_result(self, new_context))
    except StopIteration as exc:
        raise exceptions.InferenceError(
            message='Inference for {node!r}[{index!s}] failed.',
            node=self,
            index=index,
            context=context) from exc
예제 #15
0
    def infer_call_result(self,
                          caller: nodes.Call | Proxy,
                          context: InferenceContext | None = None):
        """infer what a class instance is returning when called"""
        context = bind_context_to_node(context, self)
        inferred = False

        # If the call is an attribute on the instance, we infer the attribute itself
        if isinstance(caller, nodes.Call) and isinstance(
                caller.func, nodes.Attribute):
            for res in self.igetattr(caller.func.attrname, context):
                inferred = True
                yield res

        # Otherwise we infer the call to the __call__ dunder normally
        for node in self._proxied.igetattr("__call__", context):
            if node is Uninferable or not node.callable():
                continue
            for res in node.infer_call_result(caller, context):
                inferred = True
                yield res
        if not inferred:
            raise InferenceError(node=self, caller=caller, context=context)