Skip to content

Commit bc9b9ab

Browse files
committed
Add typing to acquire
1 parent e9bb695 commit bc9b9ab

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

asyncpg/pool.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from __future__ import annotations
88

99
import asyncio
10-
from collections.abc import Awaitable, Callable
10+
from collections.abc import Awaitable, Callable, Iterator
1111
import functools
1212
import inspect
1313
import logging
@@ -405,7 +405,7 @@ def __init__(self, *connect_args,
405405
self._holders = []
406406
self._initialized = False
407407
self._initializing = False
408-
self._queue = None
408+
self._queue: Optional[asyncio.LifoQueue[PoolConnectionHolder]] = None
409409

410410
self._connection_class = connection_class
411411
self._record_class = record_class
@@ -838,7 +838,11 @@ async def copy_records_to_table(
838838
where=where
839839
)
840840

841-
def acquire(self, *, timeout=None):
841+
def acquire(
842+
self,
843+
*,
844+
timeout: Optional[float] = None,
845+
) -> PoolAcquireContext:
842846
"""Acquire a database connection from the pool.
843847
844848
:param float timeout: A timeout for acquiring a Connection.
@@ -863,11 +867,12 @@ def acquire(self, *, timeout=None):
863867
"""
864868
return PoolAcquireContext(self, timeout)
865869

866-
async def _acquire(self, timeout):
867-
async def _acquire_impl():
868-
ch = await self._queue.get() # type: PoolConnectionHolder
870+
async def _acquire(self, timeout: Optional[float]) -> PoolConnectionProxy:
871+
async def _acquire_impl() -> PoolConnectionProxy:
872+
assert self._queue is not None
873+
ch = await self._queue.get()
869874
try:
870-
proxy = await ch.acquire() # type: PoolConnectionProxy
875+
proxy = await ch.acquire()
871876
except (Exception, asyncio.CancelledError):
872877
self._queue.put_nowait(ch)
873878
raise
@@ -1039,7 +1044,7 @@ def __init__(self, pool: Pool, timeout: Optional[float]) -> None:
10391044
self.connection = None
10401045
self.done = False
10411046

1042-
async def __aenter__(self):
1047+
async def __aenter__(self) -> PoolConnectionProxy:
10431048
if self.connection is not None or self.done:
10441049
raise exceptions.InterfaceError('a connection is already acquired')
10451050
self.connection = await self.pool._acquire(self.timeout)
@@ -1056,7 +1061,7 @@ async def __aexit__(
10561061
self.connection = None
10571062
await self.pool.release(con)
10581063

1059-
def __await__(self):
1064+
def __await__(self) -> Iterator[PoolConnectionProxy]:
10601065
self.done = True
10611066
return self.pool._acquire(self.timeout).__await__()
10621067

0 commit comments

Comments
 (0)