flake8 抱怨布尔比较“==";在过滤器子句中

flake8 complains on boolean comparison quot;==quot; in filter clause(flake8 抱怨布尔比较“==;在过滤器子句中)
本文介绍了flake8 抱怨布尔比较“==";在过滤器子句中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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 的比较应该是if cond is False:"或if not条件:"

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.我认为过滤器子句不支持运算符是"或不是".请问有人可以告诉我如何处理这种情况.我不想禁用薄片.

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()

where false() 返回会话 SQL 方言的正确值.有一个匹配的 sqlalchemy.expression.true.

where false() returns the right value for your session SQL dialect. There is a matching sqlalchemy.expression.true.

这篇关于flake8 抱怨布尔比较“==";在过滤器子句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)