flake8 complains on boolean comparison "==" in filter clause
That's because SQLAlchemy filters are one of the few places where == False
actually makes sense. Everywhere else you should not use it.
Add a # noqa
comment to the line and be done with it.
Or you can use sqlalchemy.sql.expression.false
:
from sqlalchemy.sql.expression import false
TestCase.obsoleted == false()
where false()
returns the right value for your session SQL dialect. There is a matching sqlalchemy.expression.true
.
I have a look what exact query is generated for using SQLAlchemy
when ==
and is_
when the database dialect is Postgresql
for boolean field:
for
==
we get:field == False
is converted tofield = false
field == True
is converted tofield = true
field == None
is converted tofield IS NULL
for
is_()
we get:field.is_(False)
is converted tofield IS false
field.is_(True)
is converted tofield IS true
field.is_(None)
is converted tofield IS NULL
NOTE: is_(not None)
will be evaluated to is_(bool(not None)
what gives is_(True)
giving field = true
so you rather go for isnot(None)
producing field IS NOT NULL
SQL Alchemy also has is_
and isnot
functions you can use. An example would be
Model.filter(Model.deleted.is_(False))
More on those here
Why don't you use .filter_by(field=True)
/ .filter_by(field=False)
?