flake8 complains on boolean comparison “==” in filter clause

前端 未结 4 1424
[愿得一人]
[愿得一人] 2020-11-30 12:29

I have a boolean field in the mysql db table.

# table model
class TestCase(Base):
    __tablename__ = \'test_cases\'
    ...
    obsoleted = Column(\'obsole         


        
4条回答
  •  有刺的猬
    2020-11-30 12:57

    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:

      1. field == False is converted to field = false
      2. field == True is converted to field = true
      3. field == None is converted to field IS NULL
    • for is_() we get:

      1. field.is_(False) is converted to field IS false
      2. field.is_(True) is converted to field IS true
      3. field.is_(None) is converted to field 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

提交回复
热议问题