Skip to content

Commit 636420b

Browse files
authored
Mark pool-wrapped connection coroutine methods as coroutines (#1134)
Use `markcoroutinefunction` (available in Python 3.12+) to make `inspect.iscoroutinefunction()` return the correct answer for wrapped connection methods. Fixes: #1133
1 parent 7f00484 commit 636420b

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

asyncpg/compat.py

+7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ async def wait_closed(stream: asyncio.StreamWriter) -> None:
5252
pass
5353

5454

55+
if sys.version_info < (3, 12):
56+
def markcoroutinefunction(c): # type: ignore
57+
pass
58+
else:
59+
from inspect import markcoroutinefunction # noqa: F401
60+
61+
5562
if sys.version_info < (3, 12):
5663
from ._asyncio_compat import wait_for as wait_for # noqa: F401
5764
else:

asyncpg/pool.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def __new__(mcls, name, bases, dct, *, wrap=False):
3333
if not inspect.isfunction(meth):
3434
continue
3535

36-
wrapper = mcls._wrap_connection_method(attrname)
36+
iscoroutine = inspect.iscoroutinefunction(meth)
37+
wrapper = mcls._wrap_connection_method(attrname, iscoroutine)
3738
wrapper = functools.update_wrapper(wrapper, meth)
3839
dct[attrname] = wrapper
3940

@@ -43,7 +44,7 @@ def __new__(mcls, name, bases, dct, *, wrap=False):
4344
return super().__new__(mcls, name, bases, dct)
4445

4546
@staticmethod
46-
def _wrap_connection_method(meth_name):
47+
def _wrap_connection_method(meth_name, iscoroutine):
4748
def call_con_method(self, *args, **kwargs):
4849
# This method will be owned by PoolConnectionProxy class.
4950
if self._con is None:
@@ -55,6 +56,9 @@ def call_con_method(self, *args, **kwargs):
5556
meth = getattr(self._con.__class__, meth_name)
5657
return meth(self._con, *args, **kwargs)
5758

59+
if iscoroutine:
60+
compat.markcoroutinefunction(call_con_method)
61+
5862
return call_con_method
5963

6064

0 commit comments

Comments
 (0)