Ejemplo n.º 1
0
 def igetattr(self, name, context=None):
     """infered getattr, need special treatment in class to handle
     descriptors
     """
     # set lookoup name since this is necessary to infer on import nodes for
     # instance
     context = copy_context(context)
     context.lookupname = name
     try:
         for infered in _infer_stmts(self.getattr(name, context),
                                     context,
                                     frame=self):
             # yield YES object instead of descriptors when necessary
             if not isinstance(infered, Const) and isinstance(
                     infered, Instance):
                 try:
                     infered._proxied.getattr('__get__', context)
                 except NotFoundError:
                     yield infered
                 else:
                     yield YES
             else:
                 yield infered
     except NotFoundError:
         if not name.startswith('__') and self.has_dynamic_getattr(context):
             # class handle some dynamic attributes, return a YES object
             yield YES
         else:
             raise InferenceError(name)
Ejemplo n.º 2
0
 def igetattr(self, name, context=None):
     """infered getattr, need special treatment in class to handle
     descriptors
     """
     # set lookoup name since this is necessary to infer on import nodes for
     # instance
     context = copy_context(context)
     context.lookupname = name
     try:
         for infered in _infer_stmts(self.getattr(name, context), context,
                                     frame=self):
             # yield YES object instead of descriptors when necessary
             if not isinstance(infered, Const) and isinstance(infered, Instance):
                 try:
                     infered._proxied.getattr('__get__', context)
                 except NotFoundError:
                     yield infered
                 else:
                     yield YES
             else:
                 yield infered
     except NotFoundError:
         if not name.startswith('__') and self.has_dynamic_getattr(context):
             # class handle some dynamic attributes, return a YES object
             yield YES
         else:
             raise InferenceError(name)
Ejemplo n.º 3
0
def infer_function(self, context=None):
    """infer on Function nodes must be take with care since it
    may be called to infer one of it's argument (in which case <name>
    should be given)
    """
    name = context.lookupname
    # no name is given, we are infering the function itself
    if name is None:
        yield self
        return
    if context.callcontext:
        # reset call context/name
        callcontext = context.callcontext
        context = copy_context(context)
        context.callcontext = None
        for infered in callcontext.infer_argument(self, name, context):
            yield infered
        return
    # Function.argnames can be None in astng (means that we don't have
    # information on argnames), in which case we can't do anything more
    if self.argnames is None:
        yield YES
        return
    if not name in self.argnames:
        raise InferenceError()
    # first argument of instance/class method
    if name == self.argnames[0]:
        if self.type == 'method':
            yield Instance(self.parent.frame())
            return
        if self.type == 'classmethod':
            yield self.parent.frame()
            return
    mularg = self.mularg_class(name)
    if mularg is not None: # */** argument, no doubt it's a Tuple or Dict
        yield mularg
        return
    # if there is a default value, yield it. And then yield YES to reflect
    # we can't guess given argument value
    try:
        context = copy_context(context)
        for infered in self.default_value(name).infer(context):
            yield infered
        yield YES
    except NoDefault:
        yield YES
Ejemplo n.º 4
0
 def igetattr(self, name, context=None):
     """infered getattr"""
     # set lookup name since this is necessary to infer on import nodes for
     # instance
     context = copy_context(context)
     context.lookupname = name
     try:
         return _infer_stmts(self.getattr(name, context), context, frame=self)
     except NotFoundError:
         raise InferenceError(name)
Ejemplo n.º 5
0
 def igetattr(self, name, context=None):
     """infered getattr"""
     # set lookup name since this is necessary to infer on import nodes for
     # instance
     context = copy_context(context)
     context.lookupname = name
     try:
         return _infer_stmts(self.getattr(name, context), context, frame=self)
     except NotFoundError:
         raise InferenceError(name)
Ejemplo n.º 6
0
def ilookup(self, name, context=None):
    """infered lookup
    
    return an iterator on infered values of the statements returned by
    the lookup method
    """
    frame, stmts = self.lookup(name)
    context = copy_context(context)
    context.lookupname = name
    return _infer_stmts(stmts, context, frame)
Ejemplo n.º 7
0
def ilookup(self, name, context=None):
    """infered lookup
    
    return an iterator on infered values of the statements returned by
    the lookup method
    """
    frame, stmts = self.lookup(name)
    context = copy_context(context)
    context.lookupname = name
    return _infer_stmts(stmts, context, frame)
Ejemplo n.º 8
0
def infer_from(self, context=None, asname=True):
    """self resolve on From / Import nodes return the imported module/object"""
    name = context.lookupname
    if name is None:
        raise InferenceError()
    if asname:
        name = self.real_name(name)
    module = _imported_module_astng(self, self.modname)
    try:
        context = copy_context(context)
        context.lookupname = name
        return _infer_stmts(module.getattr(name), context)
    except NotFoundError:
        raise InferenceError(name)