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.

BooleanField is_true and is_false methods

See original GitHub issue

Proposal

Add methods to BooleanField so that we can create expressions like MyModel.my_field.is_true() and MyModel.my_field.is_false() instead of MyModel.my_field == True and MyModel.my_field == False.

Rationale

I’m using flake8, with a selected list of pycodestyle error codes, and using MyModel.my_field == True triggers the E712 error, comparison to True should be ‘if cond is True:’ or ‘if cond:’.

That would be consistent with the is_null method, which is used instead of a “MyModel.my_field == None”.

Reference implementation

peewee.BooleanField.is_true = lambda self: peewee.Expression(self, peewee.OP.EQ, True)
peewee.BooleanField.is_false = lambda self: peewee.Expression(self, peewee.OP.EQ, False)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
coleifercommented, Oct 26, 2016

I appreciate you taking the time to submit this issue. Unfortunately, peewee does quite a few things that many linters find egregious. I don’t use linters myself, so it doesn’t bother me and personally I’m not interested in adding code simply to satisfy flake8. Thanks for providing a reference implementation, anyone who is interested can use your code or subclass BooleanField and add the suggested methods.

6reactions
coleifercommented, Oct 26, 2016

Is there any example with some different approach to avoid Something.my_field == True while doing the same?

You’ve shared a nice one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

BooleanField - Django Models - GeeksforGeeks
BooleanField is a true/false field. It is like a bool field in C/C+++. The default form widget for this field is CheckboxInput, ...
Read more >
Unique BooleanField value in Django? - Stack Overflow
Whenever I've needed to accomplish this task, what I've done is override the save method for the model and have it check if...
Read more >
Boolean field in FLOW sends EMPTY STRING when using ...
Boolean field is HIDDEN, value is FALSE; Flow sends EMPTY STRING. ... Scenario 1: Boolean field on record is TRUE.
Read more >
Boolean (Java Platform SE 7 ) - Oracle Help Center
Returns a Boolean instance representing the specified boolean value. If the specified boolean value is true , this method returns Boolean.TRUE ; if...
Read more >
Boolean data type - Wikipedia
In computer science, the Boolean (sometimes shortened to Bool) is a data type that has one of two possible values which is intended...
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