summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2016-02-27 17:45:00 +0000
committerClaudiu Popa <pcmanticore@gmail.com>2016-02-27 17:45:00 +0000
commit8f5ad6282949962b75b6a34edb0fca0957c01050 (patch)
tree1381bf1611e4e7bd9cfb3eaa12e600537db47a51
parent53aca0ec839637e63d0a0c537a62597d066bce47 (diff)
downloadastroid-git-8f5ad6282949962b75b6a34edb0fca0957c01050.tar.gz
Separate the UnboundMethod from BoundMethod
They were previously tied because UnboundMethod had logic for type.__new__. Now this logic was moved into a brain tip, simplifying the implementations of the methods.
-rw-r--r--astroid/brain/brain_builtin_inference.py35
-rw-r--r--astroid/builder.py2
-rw-r--r--astroid/interpreter/objects.py24
-rw-r--r--astroid/interpreter/runtimeabc.py8
-rw-r--r--astroid/interpreter/util.py2
5 files changed, 38 insertions, 33 deletions
diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py
index a3c95370..52466974 100644
--- a/astroid/brain/brain_builtin_inference.py
+++ b/astroid/brain/brain_builtin_inference.py
@@ -514,45 +514,55 @@ def infer_slice(node, context=None):
def infer_type_dunder_new(caller, context=None):
- """Try to infer what type.__new__(mcs, name, bases, attrs) returns.
+ """Try to infer what __new__ returns when called
+ It also looks for type.__new__(mcs, name, bases, attrs).
In order for such call to be valid, the metaclass needs to be
a subtype of ``type``, the name needs to be a string, the bases
needs to be a tuple of classes and the attributes a dictionary
of strings to values.
"""
- if len(caller.args) != 4:
+ if not caller.args:
raise UseInferenceDefault
+ inferred = next(caller.args[0].infer())
+ if inferred is util.Uninferable:
+ raise UseInferenceDefault
+
+ default = iter((objects.Instance(inferred), ))
+
+ if len(caller.args) != 4:
+ return default
+
# Verify the metaclass
mcs = next(caller.args[0].infer(context=context))
if not isinstance(mcs, nodes.ClassDef):
# Not a valid first argument.
- raise UseInferenceDefault
+ return default
if not mcs.is_subtype_of("%s.type" % BUILTINS):
# Not a valid metaclass.
- raise UseInferenceDefault
+ return default
# Verify the name
name = next(caller.args[1].infer(context=context))
if not isinstance(name, nodes.Const):
# Not a valid name, needs to be a const.
- raise UseInferenceDefault
+ return default
if not isinstance(name.value, str):
# Needs to be a string.
- raise UseInferenceDefault
+ return default
# Verify the bases
bases = next(caller.args[2].infer(context=context))
if not isinstance(bases, nodes.Tuple):
# Needs to be a tuple.
- raise UseInferenceDefault
+ return default
inferred_bases = [next(elt.infer(context=context))
for elt in bases.elts]
if not all(isinstance(base, nodes.ClassDef)
for base in inferred_bases):
# All the bases needs to be Classes
- raise UseInferenceDefault
+ return default
cls = nodes.ClassDef(name=name.value, lineno=caller.lineno,
col_offset=caller.col_offset, parent=caller)
@@ -561,17 +571,17 @@ def infer_type_dunder_new(caller, context=None):
attrs = next(caller.args[3].infer(context=context))
if not isinstance(attrs, nodes.Dict):
# Needs to be a dictionary.
- raise UseInferenceDefault
+ return default
body = []
for key, value in attrs.items:
key = next(key.infer(context=context))
value = next(value.infer(context=context))
if not isinstance(key, nodes.Const):
# Something invalid as an attribute.
- raise UseInferenceDefault
+ return default
if not isinstance(key.value, str):
# Not a proper attribute.
- raise UseInferenceDefault
+ return default
assign = nodes.Assign(parent=cls)
assign.postinit(targets=nodes.AssignName(key.value, parent=assign),
value=value)
@@ -584,11 +594,8 @@ def infer_type_dunder_new(caller, context=None):
def _looks_like_type_dunder_new(node):
return (isinstance(node.func, nodes.Attribute)
- and isinstance(node.func.expr, nodes.Name)
- and node.func.expr.name == 'type'
and node.func.attrname == '__new__')
-
# Builtins inference
register_builtin_transform(infer_bool, 'bool')
register_builtin_transform(infer_super, 'super')
diff --git a/astroid/builder.py b/astroid/builder.py
index b3a5d0f0..b5494024 100644
--- a/astroid/builder.py
+++ b/astroid/builder.py
@@ -154,11 +154,11 @@ class AstroidBuilder(object):
"""Handles encoding and delayed nodes after a module has been built"""
module.file_encoding = encoding
self._manager.cache_module(module)
- delayed_assignments(module)
# Visit the transforms
if self._apply_transforms:
module = self._manager.visit_transforms(module)
+ delayed_assignments(module)
return module
def _data_build(self, data, modname, path):
diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py
index 383ea8ac..ad585445 100644
--- a/astroid/interpreter/objects.py
+++ b/astroid/interpreter/objects.py
@@ -170,7 +170,7 @@ class BaseInstance(Proxy):
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 isinstance(attr, Method):
if is_property(attr):
for inferred in attr.infer_call_result(self, context):
yield inferred
@@ -296,9 +296,15 @@ class Method(Proxy):
return iter((self.special_attributes.lookup(name), ))
return self._proxied.igetattr(name, context)
+ def infer_call_result(self, caller, context):
+ return self._proxied.infer_call_result(caller, context)
+
def bool_value(self):
return True
+ def is_bound(self):
+ return False
+
@util.register_implementation(runtimeabc.UnboundMethod)
class UnboundMethod(Method):
@@ -306,27 +312,15 @@ class UnboundMethod(Method):
special_attributes = util.lazy_descriptor(lambda: objectmodel.UnboundMethodModel())
- def infer_call_result(self, caller, context):
- # If we're unbound method __new__ of builtin object, the result is an
- # instance of the class given as first argument.
- if (self._proxied.name == '__new__' and
- self._proxied.parent.frame().qname() == '%s.object' % BUILTINS):
- infer = caller.args[0].infer() if caller.args else []
- return ((x is util.Uninferable and x or Instance(x)) for x in infer)
- return self._proxied.infer_call_result(caller, context)
-
- def is_bound(self):
- return False
-
@util.register_implementation(runtimeabc.BoundMethod)
-class BoundMethod(UnboundMethod):
+class BoundMethod(Method):
"""a special node representing a method bound to an instance"""
special_attributes = util.lazy_descriptor(lambda: objectmodel.BoundMethodModel())
def __init__(self, proxy, bound):
- UnboundMethod.__init__(self, proxy)
+ super(BoundMethod, self).__init__(proxy)
self.bound = bound
def is_bound(self):
diff --git a/astroid/interpreter/runtimeabc.py b/astroid/interpreter/runtimeabc.py
index 4fc1a15d..9f140cde 100644
--- a/astroid/interpreter/runtimeabc.py
+++ b/astroid/interpreter/runtimeabc.py
@@ -40,11 +40,15 @@ class BuiltinInstance(RuntimeObject):
"""Represents an instance of a builtin."""
-class UnboundMethod(RuntimeObject):
+class Method(RuntimeObject):
+ """Base class for methods."""
+
+
+class UnboundMethod(Method):
"""Class representing an unbound method."""
-class BoundMethod(UnboundMethod):
+class BoundMethod(Method):
"""Class representing a bound method."""
diff --git a/astroid/interpreter/util.py b/astroid/interpreter/util.py
index 18e199b8..207cd143 100644
--- a/astroid/interpreter/util.py
+++ b/astroid/interpreter/util.py
@@ -241,7 +241,7 @@ def _object_type(node, context=None):
yield metaclass
continue
yield builtins_ast.getattr('type')[0]
- elif isinstance(inferred, (treeabc.Lambda, runtimeabc.UnboundMethod)):
+ elif isinstance(inferred, (treeabc.Lambda, runtimeabc.Method)):
if isinstance(inferred, treeabc.Lambda):
if inferred.root() is builtins_ast:
yield builtins_ast[types.BuiltinFunctionType.__name__]