Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete typing of connresource #1202

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion asyncpg/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,7 @@ async def set_builtin_type_codec(self, typename, *,
# Statement cache is no longer valid due to codec changes.
self._drop_local_statement_cache()

def is_closed(self):
def is_closed(self) -> bool:
"""Return ``True`` if the connection is closed, ``False`` otherwise.

:return bool: ``True`` if the connection is closed, ``False``
Expand Down
38 changes: 34 additions & 4 deletions asyncpg/connresource.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,47 @@
# This module is part of asyncpg and is released under
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0

from __future__ import annotations

import functools
from typing import TYPE_CHECKING, Generic, Protocol, TypeVar
from typing_extensions import ParamSpec

from . import exceptions

if TYPE_CHECKING:
from . import connection

def guarded(meth):
_ConnectionResourceT = TypeVar(
"_ConnectionResourceT", bound="ConnectionResource", contravariant=True
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bound needs to be stringified, even with from __future__ import annotations. I checked this locally.

)
_P = ParamSpec("_P")
_R = TypeVar("_R", covariant=True)


class _ConnectionResourceMethod(
Protocol,
Generic[_ConnectionResourceT, _R, _P],
):
# This indicates that the Protocol is a function and not a lambda
__name__: str

# Type signature of a method on an instance of _ConnectionResourceT
def __call__(
_, self: _ConnectionResourceT, *args: _P.args, **kwds: _P.kwargs
) -> _R:
...


def guarded(
meth: _ConnectionResourceMethod[_ConnectionResourceT, _R, _P]
) -> _ConnectionResourceMethod[_ConnectionResourceT, _R, _P]:
"""A decorator to add a sanity check to ConnectionResource methods."""

@functools.wraps(meth)
def _check(self, *args, **kwargs):
def _check(
self: _ConnectionResourceT, *args: _P.args, **kwargs: _P.kwargs
) -> _R:
self._check_conn_validity(meth.__name__)
return meth(self, *args, **kwargs)

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

def __init__(self, connection):
def __init__(self, connection: connection.Connection) -> None:
self._connection = connection
self._con_release_ctr = connection._pool_release_ctr

def _check_conn_validity(self, meth_name):
def _check_conn_validity(self, meth_name: str) -> None:
con_release_ctr = self._connection._pool_release_ctr
if con_release_ctr != self._con_release_ctr:
raise exceptions.InterfaceError(
Expand Down
12 changes: 10 additions & 2 deletions asyncpg/exceptions/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# This module is part of asyncpg and is released under
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0

from __future__ import annotations
from typing import Optional

import asyncpg
import sys
Expand Down Expand Up @@ -208,11 +210,17 @@ def __str__(self):
class InterfaceError(InterfaceMessage, Exception):
"""An error caused by improper use of asyncpg API."""

def __init__(self, msg, *, detail=None, hint=None):
def __init__(
self,
msg: str,
*,
detail: Optional[str] = None,
hint: Optional[str] = None,
) -> None:
InterfaceMessage.__init__(self, detail=detail, hint=hint)
Exception.__init__(self, msg)

def with_msg(self, msg):
def with_msg(self, msg: str) -> InterfaceError:
return type(self)(
msg,
detail=self.detail,
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ module = [
"asyncpg.cluster",
"asyncpg.connect_utils",
"asyncpg.connection",
"asyncpg.connresource",
"asyncpg.cursor",
"asyncpg.exceptions",
"asyncpg.exceptions.*",
Expand Down
Loading