Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 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
yield from function.infer_call_result(caller=self, context=context)
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
)
def bool_value(self):
"""Determine the bool value of this node
The boolean value of a node can have three
possible values:
* False. For instance, empty data structures,
False, empty strings, instances which return
explicitly False from the __nonzero__ / __bool__
method.
* True. Most of constructs are True by default:
classes, functions, modules etc
* Uninferable: the inference engine is uncertain of the
node's value.
"""
return util.Uninferable
def unpack_infer(stmt, context=None):
"""recursively generate nodes inferred by the given statement.
If the inferred value is a list or a tuple, recurse on the elements
"""
if isinstance(stmt, (treeabc.List, treeabc.Tuple)):
for elt in stmt.elts:
if elt is util.Uninferable:
yield elt
continue
for inferred_elt in unpack_infer(elt, context):
yield inferred_elt
# Explicit StopIteration to return error information, see comment
# in raise_if_nothing_inferred.
raise StopIteration(dict(node=stmt, context=context))
# if inferred is a final node, return it and stop
inferred = next(stmt.infer(context))
if inferred is stmt:
yield inferred
raise StopIteration(dict(node=stmt, context=context))
# else, infer recursivly, except Uninferable object that should be returned as is
for inferred in stmt.infer(context):
if inferred is util.Uninferable:
yield inferred
if self.body and isinstance(self.body[-1], node_classes.Assert):
yield node_classes.Const(None)
return
raise exceptions.InferenceError(
"The function does not have any return statements"
)
for returnnode in itertools.chain((first_return,), returns):
if returnnode.value is None:
yield node_classes.Const(None)
else:
try:
yield from returnnode.value.infer(context)
except exceptions.InferenceError:
yield util.Uninferable
# set lookup name since this is necessary to infer on import nodes for
# instance
context = contextmod.copy_context(context)
context.lookupname = name
try:
attrs = self.getattr(name, context, class_context=class_context)
for inferred in bases._infer_stmts(attrs, context, frame=self):
# yield Uninferable object instead of descriptors when necessary
if (not isinstance(inferred, node_classes.Const)
and isinstance(inferred, bases.Instance)):
try:
inferred._proxied.getattr('__get__', context)
except exceptions.AttributeInferenceError:
yield inferred
else:
yield util.Uninferable
else:
yield function_to_method(inferred, self)
except exceptions.AttributeInferenceError as error:
if not name.startswith('__') and self.has_dynamic_getattr(context):
# class handle some dynamic attributes, return a Uninferable object
yield util.Uninferable
else:
util.reraise(exceptions.InferenceError(
error.message, target=self, attribute=name, context=context))
c.parent = self
class_bases = [next(b.infer(context)) for b in caller.args[1:]]
c.bases = [base for base in class_bases if base != util.Uninferable]
c._metaclass = metaclass
yield c
return
returns = self.nodes_of_class(node_classes.Return, skip_klass=FunctionDef)
for returnnode in returns:
if returnnode.value is None:
yield node_classes.Const(None)
else:
try:
for inferred in returnnode.value.infer(context):
yield inferred
except exceptions.InferenceError:
yield util.Uninferable
if len(node.args) not in (2, 3):
# Not a valid getattr call.
raise UseInferenceDefault
try:
# TODO(cpopa): follow all the values of the first argument?
obj = next(node.args[0].infer(context=context))
attr = next(node.args[1].infer(context=context))
except InferenceError:
raise UseInferenceDefault
if obj is util.Uninferable or attr is util.Uninferable:
# If one of the arguments is something we can't infer,
# then also make the result of the getattr call something
# which is unknown.
return util.Uninferable, util.Uninferable
is_string = (isinstance(attr, nodes.Const) and
isinstance(attr.value, six.string_types))
if not is_string:
raise UseInferenceDefault
return obj, attr.value
def _infer_getattr_args(node, context):
if len(node.args) not in (2, 3):
# Not a valid getattr call.
raise UseInferenceDefault
try:
obj = next(node.args[0].infer(context=context))
attr = next(node.args[1].infer(context=context))
except InferenceError:
raise UseInferenceDefault
if obj is util.Uninferable or attr is util.Uninferable:
# If one of the arguments is something we can't infer,
# then also make the result of the getattr call something
# which is unknown.
return util.Uninferable, util.Uninferable
is_string = isinstance(attr, nodes.Const) and isinstance(
attr.value, six.string_types
)
if not is_string:
raise UseInferenceDefault
return obj, attr.value
itertools.chain(
_filter_uninferable_nodes(self.elts, context),
_filter_uninferable_nodes(other.elts, context),
)
)
yield node
elif isinstance(other, nodes.Const) and operator == "*":
if not isinstance(other.value, int):
yield not_implemented
return
yield _multiply_seq_by_int(self, opnode, other, context)
elif isinstance(other, bases.Instance) and operator == "*":
# Verify if the instance supports __index__.
as_index = helpers.class_instance_as_index(other)
if not as_index:
yield util.Uninferable
else:
yield _multiply_seq_by_int(self, opnode, as_index, context)
else:
yield not_implemented
def _generic_inference(node, context, node_type, transform):
args = node.args
if not args:
return node_type()
if len(node.args) > 1:
raise UseInferenceDefault()
arg, = args
transformed = transform(arg)
if not transformed:
try:
inferred = next(arg.infer(context=context))
except (InferenceError, StopIteration):
raise UseInferenceDefault()
if inferred is util.Uninferable:
raise UseInferenceDefault()
transformed = transform(inferred)
if not transformed or transformed is util.Uninferable:
raise UseInferenceDefault()
return transformed