Skip to content

Commit 2daacaa

Browse files
authored
Merge pull request piccolo-orm#129 from piccolo-orm/boolean_equality
Boolean equality
2 parents e7a36d4 + 52ec121 commit 2daacaa

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-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

tests/columns/test_boolean.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,21 @@ def test_return_type(self):
3131
.run_sync()["boolean"],
3232
expected,
3333
)
34+
35+
def test_eq_and_ne(self):
36+
"""
37+
Make sure the `eq` and `ne` methods works correctly.
38+
"""
39+
MyTable.insert(
40+
MyTable(boolean=True),
41+
MyTable(boolean=False),
42+
MyTable(boolean=True),
43+
).run_sync()
44+
45+
self.assertEqual(
46+
MyTable.count().where(MyTable.boolean.eq(True)).run_sync(), 2
47+
)
48+
49+
self.assertEqual(
50+
MyTable.count().where(MyTable.boolean.ne(True)).run_sync(), 1
51+
)

0 commit comments

Comments
 (0)