Skip to content

singleton comparison - ==/is true message improvement #8796

Open
@orSolocate

Description

@orSolocate

Current problem

I was wondering about the singleton-comparison checker I worked on a bit a while ago.
it suggests you to compare singleton using is instead of == which is fine, for example for none.
But why should we use it with True and False? since comparison returns a Boolean anyway.

Desired solution

For the following code:

if expr == True or if expr is True

The checker should suggest to modify it to if expr.

for false, both if expr == False and if expr is False should be modified to if not expr.

Additional context

There is some controversy about this matter as can be seen in this thread:
https://stackoverflow.com/questions/27276610/boolean-identity-true-vs-is-true

Since it assumes an object has its __bool__ implemented although I believe that this is most probably the case in any class, since it comes with a default method that returns True.
See this thread for some (maybe engineered) corner cases:
https://stackoverflow.com/questions/64913140/is-there-a-way-to-remove-bool-method-from-a-class

Metadata

Metadata

Assignees

No one assigned

    Labels

    Enhancement ✨Improvement to a componentNeeds decision 🔒Needs a decision before implemention or rejection

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions