Skip to content

Commit

Permalink
NodeNG.bool_value() gained an optional context parameter
Browse files Browse the repository at this point in the history
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 pylint-dev/pylint#2985
  • Loading branch information
PCManticore committed Mar 5, 2020
1 parent 0a8a75d commit 5f0675c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
9 changes: 9 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions astroid/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion astroid/brain/brain_builtin_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions astroid/node_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 8 additions & 8 deletions astroid/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 5f0675c

Please sign in to comment.