Skip to content

Commit 6ab95ab

Browse files
committed
add eq and ne methods to the Boolean column
1 parent e7a36d4 commit 6ab95ab

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

docs/src/piccolo/query_clauses/where.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ Equal / Not Equal
3030
b.name != 'Rustaceans'
3131
).run_sync()
3232
33+
.. hint:: With ``Boolean`` columns, some linters will complain if you write
34+
``SomeTable.some_column == True`` (because it's more Pythonic to do
35+
``is True``). To work around this, you can do
36+
``SomeTable.some_column.eq(True)``. Likewise, with ``!=`` you can use
37+
``SomeTable.some_column.ne(True)``
38+
3339
-------------------------------------------------------------------------------
3440

3541
Greater than / less than

piccolo/columns/column_types.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,46 @@ def __init__(
799799
kwargs.update({"default": default})
800800
super().__init__(**kwargs)
801801

802+
def eq(self, value) -> Where:
803+
"""
804+
When using ``Boolean`` columns in ``where`` clauses, some Python
805+
linters don't like it when you do something like:
806+
807+
.. code-block:: python
808+
809+
await MyTable.select().where(
810+
MyTable.some_boolean_column == True
811+
).run()
812+
813+
It's more Pythonic to use ``is True`` rather than ``== True``, which is
814+
why linters complain. The work around is to do the following instead:
815+
816+
.. code-block:: python
817+
818+
await MyTable.select().where(
819+
MyTable.some_boolean_column.__eq__(True)
820+
).run()
821+
822+
Using the ``__eq__`` magic method is a bit untidy, which is why this
823+
``eq`` method exists.
824+
825+
.. code-block:: python
826+
827+
await MyTable.select().where(
828+
MyTable.some_boolean_column.eq(True)
829+
).run()
830+
831+
The ``ne`` method exists for the same reason, for ``!=``.
832+
833+
"""
834+
return self.__eq__(value)
835+
836+
def ne(self, value) -> Where:
837+
"""
838+
See the ``eq`` method for more details.
839+
"""
840+
return self.__ne__(value)
841+
802842

803843
###############################################################################
804844

0 commit comments

Comments
 (0)