Skip to content

Commit

Permalink
Better handling of skipped predicates
Browse files Browse the repository at this point in the history
This pins down the semantics of skipping predicates by letting the result propagate up to the initial invocation (that is ``Predicate.test()``)
  • Loading branch information
dfunckt committed Dec 7, 2015
1 parent 817218f commit ba9e4a0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions rules/predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def test(self, obj=NO_VALUE, target=NO_VALUE):
args = tuple(arg for arg in (obj, target) if arg is not NO_VALUE)
_context.stack.append(Context(args))
try:
return self._apply(*args)
return bool(self._apply(*args))
finally:
_context.stack.pop()

Expand Down Expand Up @@ -183,7 +183,7 @@ def INVERT(*args):
def _combine(self, other, op, args):
self_result = self._apply(*args)
if self_result is None:
return bool(other._apply(*args))
return other._apply(*args)

# short-circuit evaluation
if op is operator.and_ and not self_result:
Expand Down
8 changes: 7 additions & 1 deletion tests/testsuite/test_predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def skipped_predicate(self):

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
assert skipped_predicate.test() is None
assert skipped_predicate.test() is False
assert len(w) == 1 and 'deprecated' in str(w[-1].message)


Expand Down Expand Up @@ -365,7 +365,13 @@ def passthrough(a):
assert (passthrough | ~requires_two_args).test(False) is False

# test that when all predicates are skipped, result is False
assert requires_two_args.test(True) is False
assert (requires_two_args | requires_two_args).test(True) is False
assert (requires_two_args & requires_two_args).test(True) is False

# test that a skipped predicate doesn't alter the result at all
assert (requires_two_args | requires_two_args | passthrough).test(True) is True
assert (requires_two_args & requires_two_args & passthrough).test(True) is True


def test_invocation_context():
Expand Down

0 comments on commit ba9e4a0

Please sign in to comment.