Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _check_using_constant_test(self, node, test):
const_nodes = (
astroid.Module,
astroid.scoped_nodes.GeneratorExp,
astroid.Lambda,
astroid.FunctionDef,
astroid.ClassDef,
astroid.bases.Generator,
astroid.UnboundMethod,
astroid.BoundMethod,
astroid.Module,
)
structs = (astroid.Dict, astroid.Tuple, astroid.Set)
# These nodes are excepted, since they are not constant
# values, requiring a computation to happen.
except_nodes = (
astroid.Call,
astroid.BinOp,
astroid.BoolOp,
astroid.UnaryOp,
astroid.Subscript,
)
inferred = None
def _check_using_constant_test(self, node, test):
const_nodes = (
astroid.Module,
astroid.scoped_nodes.GeneratorExp,
astroid.Lambda, astroid.FunctionDef, astroid.ClassDef,
astroid.bases.Generator, astroid.UnboundMethod,
astroid.BoundMethod, astroid.Module)
structs = (astroid.Dict, astroid.Tuple, astroid.Set)
# These nodes are excepted, since they are not constant
# values, requiring a computation to happen. The only type
# of node in this list which doesn't have this property is
# Getattr, which is excepted because the conditional statement
# can be used to verify that the attribute was set inside a class,
# which is definitely a valid use case.
except_nodes = (astroid.Attribute, astroid.Call,
astroid.BinOp, astroid.BoolOp, astroid.UnaryOp,
astroid.Subscript)
inferred = None
emit = isinstance(test, (astroid.Const, ) + structs + const_nodes)
if not isinstance(test, except_nodes):
inferred = utils.safe_infer(test)
def delayed_assattr(self, node):
"""Visit a AssAttr node
This adds name to locals and handle members definition.
"""
try:
frame = node.frame()
for inferred in node.expr.infer():
if inferred is util.YES:
continue
try:
if inferred.__class__ is bases.Instance:
inferred = inferred._proxied
iattrs = inferred._instance_attrs
elif isinstance(inferred, bases.Instance):
# Const, Tuple, ... we may be wrong, may be not, but
# anyway we don't want to pollute builtin's namespace
continue
elif inferred.is_function:
iattrs = inferred._instance_attrs
else:
iattrs = inferred._locals
except AttributeError:
# XXX log error
continue
values = iattrs.setdefault(node.attrname, [])
if node in values:
continue
# get assign in __init__ first XXX useful ?
if (frame.name == '__init__' and values and
not values[0].frame().name == '__init__'):
kwargs = []
if isinstance(node.func, astroid.Attribute) and node.func.attrname == "sort":
inferred = utils.safe_infer(node.func.expr)
if not inferred:
return
builtins_list = "{}.list".format(bases.BUILTINS)
if isinstance(inferred, astroid.List) or inferred.qname() == builtins_list:
kwargs = node.keywords
elif isinstance(node.func, astroid.Name) and node.func.name == "sorted":
inferred = utils.safe_infer(node.func)
if not inferred:
return
builtins_sorted = "{}.sorted".format(bases.BUILTINS)
if inferred.qname() == builtins_sorted:
kwargs = node.keywords
for kwarg in kwargs or []:
if kwarg.arg == "cmp":
self.add_message("using-cmp-argument", node=node)
return
def _arguments_infer_argname(self, name, context):
# arguments information may be missing, in which case we can't do anything
# more
if not (self.arguments or self.vararg or self.kwarg):
yield util.Uninferable
return
# first argument of instance/class method
if self.arguments and getattr(self.arguments[0], "name", None) == name:
functype = self.parent.type
cls = self.parent.parent.scope()
is_metaclass = isinstance(cls, nodes.ClassDef) and cls.type == "metaclass"
# If this is a metaclass, then the first argument will always
# be the class, not an instance.
if context.boundnode and isinstance(context.boundnode, bases.Instance):
cls = context.boundnode._proxied
if is_metaclass or functype == "classmethod":
yield cls
return
if functype == "method":
yield cls.instantiate_class()
return
if context and context.callcontext:
call_site = arguments.CallSite(context.callcontext, context.extra_context)
yield from call_site.infer_argument(self.parent, name, context)
return
if name == self.vararg:
vararg = nodes.const_factory(())
vararg.parent = self
def attr_mro(self):
if not self._instance.newstyle:
raise exceptions.AttributeInferenceError(
target=self._instance, attribute="mro"
)
# pylint: disable=import-outside-toplevel; circular import
from astroid import bases
other_self = self
# Cls.mro is a method and we need to return one in order to have a proper inference.
# The method we're returning is capable of inferring the underlying MRO though.
class MroBoundMethod(bases.BoundMethod):
def infer_call_result(self, caller, context=None):
yield other_self.attr___mro__
implicit_metaclass = self._instance.implicit_metaclass()
mro_method = implicit_metaclass.locals["mro"][0]
return MroBoundMethod(proxy=mro_method, bound=implicit_metaclass)
def infer_name(self, context=None):
"""infer a Name: use name lookup rules"""
frame, stmts = self.lookup(self.name)
if not stmts:
# Try to see if the name is enclosed in a nested function
# and use the higher (first function) scope for searching.
# TODO: should this be promoted to other nodes as well?
parent_function = _higher_function_scope(self.scope())
if parent_function:
_, stmts = parent_function.lookup(self.name)
if not stmts:
raise exceptions.UnresolvableName(self.name)
context = context.clone()
context.lookupname = self.name
return bases._infer_stmts(stmts, context, frame)
nodes.Name._infer = bases.path_wrapper(infer_name)
def _object_type(node, context=None):
astroid_manager = manager.AstroidManager()
builtins = astroid_manager.builtins_module
context = context or contextmod.InferenceContext()
for inferred in node.infer(context=context):
if isinstance(inferred, scoped_nodes.ClassDef):
if inferred.newstyle:
metaclass = inferred.metaclass(context=context)
if metaclass:
yield metaclass
continue
yield builtins.getattr("type")[0]
elif isinstance(inferred, (scoped_nodes.Lambda, bases.UnboundMethod)):
yield _function_type(inferred, builtins)
elif isinstance(inferred, scoped_nodes.Module):
yield _build_proxy_class("module", builtins)
else:
yield inferred._proxied
def py__get__(self):
from astroid import bases
func = self._instance
class DescriptorBoundMethod(bases.BoundMethod):
"""Bound method which knows how to understand calling descriptor binding."""
def implicit_parameters(self):
# Different than BoundMethod since the signature
# is different.
return 0
def infer_call_result(self, caller, context=None, context_lookup=None):
if len(caller.args) != 2:
raise exceptions.InferenceError(
"Invalid arguments for descriptor binding",
target=self, context=context)
context = contextmod.copy_context(context)
cls = next(caller.args[0].infer(context=context))