Make PoolAcquireContext return a Connection type#578
Closed
Yrlish wants to merge 1 commit intoMagicStack:masterfrom
Yrlish:patch-1
Closed
Make PoolAcquireContext return a Connection type#578Yrlish wants to merge 1 commit intoMagicStack:masterfrom Yrlish:patch-1
Yrlish wants to merge 1 commit intoMagicStack:masterfrom
Yrlish:patch-1
Conversation
`__aenter__` should be defined to return a `Connection` type. This will help IDEs to properly help suggesting methods when using with `pool.acquire() as con`
Author
|
I forgot I have made an relevant issue 1.5 years ago #387. |
Member
|
There is a comprehensive effort to add type annotations in #577 |
Contributor
|
This type annotation would actually be incorrect. The correct patch is: diff --git a/asyncpg/pool.py b/asyncpg/pool.py
index e3898d5..0bea17a 100644
--- a/asyncpg/pool.py
+++ b/asyncpg/pool.py
@@ -10,6 +10,7 @@ import functools
import inspect
import logging
import time
+from typing import Optional
import warnings
from . import compat
@@ -384,7 +385,7 @@ class Pool:
self._holders = []
self._initialized = False
self._initializing = False
- self._queue = None
+ self._queue: Optional[asyncio.LifoQueue[PoolConnectionHolder]] = None
self._connection_class = connection_class
self._record_class = record_class
@@ -842,11 +843,12 @@ class Pool:
"""
return PoolAcquireContext(self, timeout)
- async def _acquire(self, timeout):
- async def _acquire_impl():
- ch = await self._queue.get() # type: PoolConnectionHolder
+ async def _acquire(self, timeout: Optional[float]) -> PoolConnectionProxy:
+ async def _acquire_impl() -> PoolConnectionProxy:
+ assert self._queue is not None, "Pool is not initialized"
+ ch = await self._queue.get()
try:
- proxy = await ch.acquire() # type: PoolConnectionProxy
+ proxy = await ch.acquire()
except (Exception, asyncio.CancelledError):
self._queue.put_nowait(ch)
raise
@@ -1015,10 +1017,10 @@ class PoolAcquireContext:
def __init__(self, pool, timeout):
self.pool = pool
self.timeout = timeout
- self.connection = None
+ self.connection: Optional[PoolConnectionProxy] = None
self.done = False
- async def __aenter__(self):
+ async def __aenter__(self) -> PoolConnectionProxy:
if self.connection is not None or self.done:
raise exceptions.InterfaceError('a connection is already acquired')
self.connection = await self.pool._acquire(self.timeout)
Since |
This was referenced Oct 29, 2024
Contributor
|
This is superseded by #1209 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
__aenter__inPoolAcquireContextshould be defined to return aConnectiontype. This will help IDEs to properly help suggesting methods when using syntax:instead of doing this:
Before:

After:
