summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2020-03-05 10:03:35 +0100
committerClaudiu Popa <pcmanticore@gmail.com>2020-03-05 10:03:35 +0100
commit5f0675c41ff8c463ab0d657fb6756aa9679cffbf (patch)
treed80fb9ef39c57bd03ff752cf70f472d704f069b1
parent0a8a75db30da060a24922e05048bc270230f5bad (diff)
downloadastroid-git-5f0675c41ff8c463ab0d657fb6756aa9679cffbf.tar.gz
``NodeNG.bool_value()`` gained an optional ``context`` parameter
We need to pass an inference context downstream when inferring the boolean value of a node in order to prevent recursion errors and double inference. This fix prevents a recursion error with dask library. Close PyCQA/pylint#2985
-rw-r--r--ChangeLog9
-rw-r--r--astroid/bases.py10
-rw-r--r--astroid/brain/brain_builtin_inference.py2
-rw-r--r--astroid/node_classes.py10
-rw-r--r--astroid/scoped_nodes.py16
5 files changed, 28 insertions, 19 deletions
diff --git a/ChangeLog b/ChangeLog
index cf25e0d7..75a9b99b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -10,6 +10,15 @@ Release Date: TBA
Close PyCQA/pylint#3417
+* ``NodeNG.bool_value()`` gained an optional ``context`` parameter
+
+ We need to pass an inference context downstream when inferring the boolean
+ value of a node in order to prevent recursion errors and double inference.
+
+ This fix prevents a recursion error with dask library.
+
+ Close PyCQA/pylint#2985
+
* Pass a context argument to ``astroid.Arguments`` to prevent recursion errors
Close PyCQA/pylint#3414
diff --git a/astroid/bases.py b/astroid/bases.py
index 0c6cb9b2..ae66c7a3 100644
--- a/astroid/bases.py
+++ b/astroid/bases.py
@@ -288,7 +288,7 @@ class Instance(BaseInstance):
def display_type(self):
return "Instance of"
- def bool_value(self):
+ def bool_value(self, context=None):
"""Infer the truth value for an Instance
The truth value of an instance is determined by these conditions:
@@ -301,7 +301,7 @@ class Instance(BaseInstance):
nonzero. If a class defines neither __len__() nor __bool__(),
all its instances are considered true.
"""
- context = contextmod.InferenceContext()
+ context = context or contextmod.InferenceContext()
context.callcontext = contextmod.CallContext(args=[])
context.boundnode = self
@@ -376,7 +376,7 @@ class UnboundMethod(Proxy):
return (Instance(x) if x is not util.Uninferable else x for x in infer)
return self._proxied.infer_call_result(caller, context)
- def bool_value(self):
+ def bool_value(self, context=None):
return True
@@ -481,7 +481,7 @@ class BoundMethod(UnboundMethod):
return super(BoundMethod, self).infer_call_result(caller, context)
- def bool_value(self):
+ def bool_value(self, context=None):
return True
@@ -507,7 +507,7 @@ class Generator(BaseInstance):
def display_type(self):
return "Generator"
- def bool_value(self):
+ def bool_value(self, context=None):
return True
def __repr__(self):
diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py
index 40566e01..b8c2b0ea 100644
--- a/astroid/brain/brain_builtin_inference.py
+++ b/astroid/brain/brain_builtin_inference.py
@@ -527,7 +527,7 @@ def infer_bool(node, context=None):
if inferred is util.Uninferable:
return util.Uninferable
- bool_value = inferred.bool_value()
+ bool_value = inferred.bool_value(context=context)
if bool_value is util.Uninferable:
return util.Uninferable
return nodes.Const(bool_value)
diff --git a/astroid/node_classes.py b/astroid/node_classes.py
index c9f75225..2e03f4ea 100644
--- a/astroid/node_classes.py
+++ b/astroid/node_classes.py
@@ -897,7 +897,7 @@ class NodeNG:
_repr_tree(self, result, set())
return "".join(result)
- def bool_value(self):
+ def bool_value(self, context=None):
"""Determine the boolean value of this node.
The boolean value of a node can have three
@@ -1021,7 +1021,7 @@ class _BaseContainer(
"""
return self.elts
- def bool_value(self):
+ def bool_value(self, context=None):
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
@@ -2624,7 +2624,7 @@ class Const(mixins.NoChildrenMixin, NodeNG, bases.Instance):
"""
return self._proxied.qname()
- def bool_value(self):
+ def bool_value(self, context=None):
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
@@ -2897,7 +2897,7 @@ class Dict(NodeNG, bases.Instance):
raise exceptions.AstroidIndexError(index)
- def bool_value(self):
+ def bool_value(self, context=None):
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
@@ -2952,7 +2952,7 @@ class Ellipsis(mixins.NoChildrenMixin, NodeNG): # pylint: disable=redefined-bui
<Ellipsis l.1 at 0x7f23b2e35160>
"""
- def bool_value(self):
+ def bool_value(self, context=None):
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py
index c7353f5b..922f4c58 100644
--- a/astroid/scoped_nodes.py
+++ b/astroid/scoped_nodes.py
@@ -737,7 +737,7 @@ class Module(LocalsDictNodeNG):
"""
return [name for name in self.keys() if not name.startswith("_")]
- def bool_value(self):
+ def bool_value(self, context=None):
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
@@ -823,7 +823,7 @@ class GeneratorExp(ComprehensionScope):
else:
self.generators = generators
- def bool_value(self):
+ def bool_value(self, context=None):
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
@@ -903,7 +903,7 @@ class DictComp(ComprehensionScope):
else:
self.generators = generators
- def bool_value(self):
+ def bool_value(self, context=None):
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
@@ -975,7 +975,7 @@ class SetComp(ComprehensionScope):
else:
self.generators = generators
- def bool_value(self):
+ def bool_value(self, context=None):
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
@@ -1022,7 +1022,7 @@ class _ListComp(node_classes.NodeNG):
self.elt = elt
self.generators = generators
- def bool_value(self):
+ def bool_value(self, context=None):
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
@@ -1261,7 +1261,7 @@ class Lambda(mixins.FilterStmtsMixin, LocalsDictNodeNG):
frame = self
return frame._scope_lookup(node, name, offset)
- def bool_value(self):
+ def bool_value(self, context=None):
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
@@ -1707,7 +1707,7 @@ class FunctionDef(mixins.MultiLineBlockMixin, node_classes.Statement, Lambda):
except exceptions.InferenceError:
yield util.Uninferable
- def bool_value(self):
+ def bool_value(self, context=None):
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
@@ -2862,7 +2862,7 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, node_classes.Statement
"""
return self._compute_mro(context=context)
- def bool_value(self):
+ def bool_value(self, context=None):
"""Determine the boolean value of this node.
:returns: The boolean value of this node.