Skip to content

Commit 00fb487

Browse files
committed
adding __await__ magic method to Query
1 parent 5b75c6c commit 00fb487

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

piccolo/query/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ def _validate(self):
8787
"""
8888
pass
8989

90+
def __await__(self):
91+
"""
92+
If the user doesn't explicity call .run(), proxy to it as a
93+
convenience.
94+
"""
95+
return self.run().__await__()
96+
9097
async def run(self, in_pool=True):
9198
self._validate()
9299

tests/query/test_await.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import asyncio
2+
3+
from ..base import DBTestCase
4+
from ..example_project.tables import Band
5+
6+
7+
class TestAwait(DBTestCase):
8+
def test_await(self):
9+
"""
10+
Test awaiting a query directly - it should proxy to Query.run().
11+
"""
12+
13+
async def get_all():
14+
return await Band.select()
15+
16+
response = asyncio.run(get_all())
17+
18+
self.assertIsInstance(response, list)

0 commit comments

Comments
 (0)