Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def function_to_method(n, klass):
if isinstance(n, FunctionDef):
if n.type == 'classmethod':
return bases.BoundMethod(n, klass)
if n.type != 'staticmethod':
return bases.UnboundMethod(n)
return n
def class_instance_as_index(node):
"""Get the value as an index for the given instance.
If an instance provides an __index__ method, then it can
be used in some scenarios where an integer is expected,
for instance when multiplying or subscripting a list.
"""
context = contextmod.InferenceContext()
context.callcontext = contextmod.CallContext(args=[node])
try:
for inferred in node.igetattr("__index__", context=context):
if not isinstance(inferred, bases.BoundMethod):
continue
for result in inferred.infer_call_result(node, context=context):
if isinstance(result, nodes.Const) and isinstance(result.value, int):
return result
except exceptions.InferenceError:
pass
return None
def _wrap_attr(self, attrs, context=contextmod.global_context):
"""wrap bound methods of attrs in a InstanceMethod proxies"""
for attr in attrs:
if isinstance(attr, UnboundMethod):
if _is_property(attr):
yield from attr.infer_call_result(self, context)
else:
yield BoundMethod(attr, self)
elif hasattr(attr, "name") and attr.name == "":
# This is a lambda function defined at class level,
# since its scope is the underlying _proxied class.
# Unfortunately, we can't do an isinstance check here,
# because of the circular dependency between astroid.bases
# and astroid.scoped_nodes.
if attr.statement().scope() == self._proxied:
if attr.args.args and attr.args.args[0].name == "self":
yield BoundMethod(attr, self)
continue
yield attr
else:
yield attr
def function_to_method(n, klass):
if isinstance(n, FunctionDef):
if n.type == "classmethod":
return bases.BoundMethod(n, klass)
if n.type == "property":
return n
if n.type != "staticmethod":
return bases.UnboundMethod(n)
return n
def _wrap_attr(self, attrs, context=None):
"""wrap bound methods of attrs in a InstanceMethod proxies"""
for attr in attrs:
if isinstance(attr, UnboundMethod):
if _is_property(attr):
yield from attr.infer_call_result(self, context)
else:
yield BoundMethod(attr, self)
elif hasattr(attr, "name") and attr.name == "":
if attr.args.arguments and attr.args.arguments[0].name == "self":
yield BoundMethod(attr, self)
continue
yield attr
else:
yield attr
if isinstance(attr, objects.Property):
yield attr
continue
if attr.type == "classmethod":
# If the method is a classmethod, then it will
# be bound to the metaclass, not to the class
# from where the attribute is retrieved.
# get_wrapping_class could return None, so just
# default to the current class.
frame = get_wrapping_class(attr) or self
yield bases.BoundMethod(attr, frame)
elif attr.type == "staticmethod":
yield attr
else:
yield bases.BoundMethod(attr, self)
yield attr
continue
if bases._is_property(attr):
# TODO(cpopa): don't use a private API.
for inferred in attr.infer_call_result(self, context):
yield inferred
continue
if attr.type == 'classmethod':
# If the method is a classmethod, then it will
# be bound to the metaclass, not to the class
# from where the attribute is retrieved.
# get_wrapping_class could return None, so just
# default to the current class.
frame = get_wrapping_class(attr) or self
yield bases.BoundMethod(attr, frame)
elif attr.type == 'staticmethod':
yield attr
else:
yield bases.BoundMethod(attr, self)
for attr in bases._infer_stmts(attrs, context, frame=cls):
if not isinstance(attr, FunctionDef):
yield attr
continue
if isinstance(attr, objects.Property):
yield attr
continue
if attr.type == "classmethod":
# If the method is a classmethod, then it will
# be bound to the metaclass, not to the class
# from where the attribute is retrieved.
# get_wrapping_class could return None, so just
# default to the current class.
frame = get_wrapping_class(attr) or self
yield bases.BoundMethod(attr, frame)
elif attr.type == "staticmethod":
yield attr
else:
yield bases.BoundMethod(attr, self)
def function_to_method(n, klass):
if isinstance(n, FunctionDef):
if n.type == 'classmethod':
return bases.BoundMethod(n, klass)
if n.type != 'staticmethod':
return bases.UnboundMethod(n)
return n