flake8抱怨布尔比较"=="在过滤器子句中 [英] flake8 complains on boolean comparison "==" in filter clause
问题描述
我在mysql db表中有一个布尔字段.
I have a boolean field in the mysql db table.
# table model
class TestCase(Base):
__tablename__ = 'test_cases'
...
obsoleted = Column('obsoleted', Boolean)
要获得所有未过时的测试用例的计数,可以像这样简单地完成:
To get the count of all the non-obsoleted test cases, that can be done simply like this:
caseNum = session.query(TestCase).filter(TestCase.obsoleted == False).count()
print(caseNum)
这很好,但是flake8报告以下警告:
That works fine, but the flake8 report the following warning:
E712:与False的比较应为如果cond为False:"或如果不是" cond:"
E712: Comparison to False should be "if cond is False:" or "if not cond:"
好的,我认为这是有道理的.因此,将我的代码更改为此:
Okay, I think that make sense. So change my code to this:
caseNum = session.query(TestCase).filter(TestCase.obsoleted is False).count()
或
caseNum = session.query(TestCase).filter(not TestCase.obsoleted).count()
但是它们都不起作用.结果始终为0. 我认为filter子句不支持运算符是"或不是".有人可以告诉我如何处理这种情况.我不想禁用薄片.
But neither of them can work. The result is always 0. I think the filter clause doesn't support the operator "is" or "is not". Will someone can tell me how to handle this situation. I don't want to disable the flake.
推荐答案
这是因为SQLAlchemy过滤器是== False
实际上有意义的少数几个位置之一. 在其他任何地方,您应该不要使用它.
That's because SQLAlchemy filters are one of the few places where == False
actually makes sense. Everywhere else you should not use it.
在该行中添加# noqa
注释,然后完成它.
Add a # noqa
comment to the line and be done with it.
或者您可以使用 sqlalchemy.sql.expression.false
:
Or you can use sqlalchemy.sql.expression.false
:
from sqlalchemy.sql.expression import false
TestCase.obsoleted == false()
其中,false()
返回您的会话SQL方言的正确值.有一个匹配的 sqlalchemy.expression.true
.
where false()
returns the right value for your session SQL dialect. There is a matching sqlalchemy.expression.true
.
这篇关于flake8抱怨布尔比较"=="在过滤器子句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!