diff --git a/ChangeLog b/ChangeLog index cf25e0d7c9..75a9b99b29 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 0c6cb9b2d1..ae66c7a3e4 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -288,7 +288,7 @@ def pytype(self): 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 @@ def bool_value(self): 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 @@ def infer_call_result(self, caller, context): 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 @@ def infer_call_result(self, caller, context=None): 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 @@ def pytype(self): 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 40566e017a..b8c2b0ea3c 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 c9f752253d..2e03f4ea84 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -897,7 +897,7 @@ def _repr_node(node, result, done, cur_indent="", depth=1): _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 @@ def itered(self): """ 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 @@ def pytype(self): """ 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 @@ def getitem(self, index, context=None): 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 """ - 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 c7353f5b36..922f4c5892 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -737,7 +737,7 @@ def public_names(self): """ 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 @@ def postinit(self, elt=None, generators=None): 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 @@ def postinit(self, key=None, value=None, generators=None): 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 @@ def postinit(self, elt=None, generators=None): 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 @@ def postinit(self, elt=None, generators=None): 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 @@ def scope_lookup(self, node, name, offset=0): 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 @@ def infer_call_result(self, caller=None, context=None): 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 @@ def mro(self, context=None) -> List["ClassDef"]: """ 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.