Skip to content

Commit e1659c4

Browse files
committed
Complete typing of connresource
1 parent a273e0e commit e1659c4

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

asyncpg/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ async def set_builtin_type_codec(self, typename, *,
14821482
# Statement cache is no longer valid due to codec changes.
14831483
self._drop_local_statement_cache()
14841484

1485-
def is_closed(self):
1485+
def is_closed(self) -> bool:
14861486
"""Return ``True`` if the connection is closed, ``False`` otherwise.
14871487
14881488
:return bool: ``True`` if the connection is closed, ``False``

asyncpg/connresource.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,47 @@
55
# This module is part of asyncpg and is released under
66
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
77

8+
from __future__ import annotations
89

910
import functools
11+
from typing import TYPE_CHECKING, Generic, Protocol, TypeVar
12+
from typing_extensions import ParamSpec
1013

1114
from . import exceptions
1215

16+
if TYPE_CHECKING:
17+
from . import connection
1318

14-
def guarded(meth):
19+
_ConnectionResourceT = TypeVar(
20+
"_ConnectionResourceT", bound="ConnectionResource", contravariant=True
21+
)
22+
_P = ParamSpec("_P")
23+
_R = TypeVar("_R", covariant=True)
24+
25+
26+
class _ConnectionResourceMethod(
27+
Protocol,
28+
Generic[_ConnectionResourceT, _R, _P],
29+
):
30+
# This indicates that the Protocol is a function and not a lambda
31+
__name__: str
32+
33+
# Type signature of a method on an instance of _ConnectionResourceT
34+
def __call__(
35+
_, self: _ConnectionResourceT, *args: _P.args, **kwds: _P.kwargs
36+
) -> _R:
37+
...
38+
39+
40+
def guarded(
41+
meth: _ConnectionResourceMethod[_ConnectionResourceT, _R, _P]
42+
) -> _ConnectionResourceMethod[_ConnectionResourceT, _R, _P]:
1543
"""A decorator to add a sanity check to ConnectionResource methods."""
1644

1745
@functools.wraps(meth)
18-
def _check(self, *args, **kwargs):
46+
def _check(
47+
self: _ConnectionResourceT, *args: _P.args, **kwargs: _P.kwargs
48+
) -> _R:
1949
self._check_conn_validity(meth.__name__)
2050
return meth(self, *args, **kwargs)
2151

@@ -25,11 +55,11 @@ def _check(self, *args, **kwargs):
2555
class ConnectionResource:
2656
__slots__ = ('_connection', '_con_release_ctr')
2757

28-
def __init__(self, connection):
58+
def __init__(self, connection: connection.Connection) -> None:
2959
self._connection = connection
3060
self._con_release_ctr = connection._pool_release_ctr
3161

32-
def _check_conn_validity(self, meth_name):
62+
def _check_conn_validity(self, meth_name: str) -> None:
3363
con_release_ctr = self._connection._pool_release_ctr
3464
if con_release_ctr != self._con_release_ctr:
3565
raise exceptions.InterfaceError(

asyncpg/exceptions/_base.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# This module is part of asyncpg and is released under
55
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
66

7+
from __future__ import annotations
8+
from typing import Optional
79

810
import asyncpg
911
import sys
@@ -208,11 +210,17 @@ def __str__(self):
208210
class InterfaceError(InterfaceMessage, Exception):
209211
"""An error caused by improper use of asyncpg API."""
210212

211-
def __init__(self, msg, *, detail=None, hint=None):
213+
def __init__(
214+
self,
215+
msg: str,
216+
*,
217+
detail: Optional[str] = None,
218+
hint: Optional[str] = None,
219+
) -> None:
212220
InterfaceMessage.__init__(self, detail=detail, hint=hint)
213221
Exception.__init__(self, msg)
214222

215-
def with_msg(self, msg):
223+
def with_msg(self, msg: str) -> InterfaceError:
216224
return type(self)(
217225
msg,
218226
detail=self.detail,

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ module = [
136136
"asyncpg.cluster",
137137
"asyncpg.connect_utils",
138138
"asyncpg.connection",
139-
"asyncpg.connresource",
140139
"asyncpg.cursor",
141140
"asyncpg.exceptions",
142141
"asyncpg.exceptions.*",

0 commit comments

Comments
 (0)