question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using `AND` (`&`) with a predicate that returns `None` incorrectly returns `True`

See original GitHub issue

When using & to combine predicates, if one of the predicates returns None (a falsey value), the overall predicate will mistakenly/confusingly return True. This is not consistent with running the predicate by itself or ORed with other predicates.

This behavior can be demonstrated with the following test added to test_predicates.py—the final assertion fails:

    def test_returns_none(self):
        @predicate
        def returns_none(arg1, arg2):
            return None

        # Just the predicate (works)
        assert not returns_none(2, 3)

        # OR the predicate with itself (works)
        p_OR = returns_none | returns_none
        assert not p_OR()

        # AND the predicate with a truthy-predicate (FAILS)
        p_AND = always_true & returns_none
        assert not p_AND()

I noticed this in production in my app, where my logic in one predicate was doing return obj and obj.boolean_field, expecting that if obj were None, it’d still be treated as False in all contexts. It took me a while to figure out what was going on. I didn’t feel sure about where/how to fix this in django-rules, but hope the above example will help to resolve quickly if possible. Thanks in advance (and thanks for building this great library)!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dfuncktcommented, Mar 23, 2022

I agree it’s more “Pythonic” to raise an exception and I have unfortunately no recollection why I made the switch to None. I don’t think it’s sensible to revert back to the old behaviour as it is a breaking change however I’m willing to be convinced otherwise if others feel the change makes sense.

0reactions
sjdemartinicommented, Mar 23, 2022

Yeah, that could be useful. I also noticed in the “Upgrading from 1.x” section of the docs, it mentions that skipping used to be done with raising a SkipPredicate exception. That more explicit approach seems significantly more preferable/safer to me, particularly for a security-oriented library, where a mistake in allowing access can be rather severe. (And then all truthy/falsey conditions can behave more in line with what’s typical in python.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Find first non-None returned value from predicate over a ...
I have a predicate function (function of one argument) that does some processing on the argument and returns a non-None value when the ......
Read more >
Java 8 Predicate with Examples - GeeksforGeeks
isEqual(Object targetRef) : Returns a predicate that tests if two arguments are equal according to Objects. · and(Predicate other) : Returns a ...
Read more >
When to use Kotlin's takeIf() and takeUnless() functions
In short, takeIf() can be called on any non-null object (the subject) ... If the predicate is satisfied (is true), the subject is...
Read more >
The Python return Statement: Usage and Best Practices
In this step-by-step tutorial, you'll learn how to use the Python return statement when writing ... An example of a function that returns...
Read more >
Introducing Computer Science ch 6: True and False
Using Predicates. Here's a procedure that returns the absolute value of a number: (define (abs num) (if (< num 0) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found