Skip to content

Commit

Permalink
add typings to Connection class
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcoding committed Jun 19, 2022
1 parent cca9189 commit c04fef2
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 52 deletions.
125 changes: 84 additions & 41 deletions asynctnt/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import enum
import functools
import os
import typing
from typing import Optional
from typing import Optional, Union, Awaitable, Any, List, Tuple, Dict

from .exceptions import TarantoolDatabaseError, \
ErrorCode, TarantoolNotConnectedError
Expand All @@ -25,7 +24,11 @@ class ConnectionState(enum.IntEnum):
DISCONNECTED = 5


_MethodRet = typing.Union[typing.Awaitable[protocol.Response], asyncio.Future]
_MethodRet = Union[Awaitable[protocol.Response], asyncio.Future]
SpaceType = Union[str, int]
IndexType = Union[str, int]
KeyType = Union[List[Any], Tuple]
TupleType = Union[List[Any], Tuple, Dict[str, Any]]


class Connection:
Expand All @@ -41,18 +44,18 @@ class Connection:
)

def __init__(self, *,
host='127.0.0.1',
port=3301,
username=None,
password=None,
fetch_schema=True,
auto_refetch_schema=True,
connect_timeout=3.,
request_timeout=-1.,
reconnect_timeout=1. / 3.,
ping_timeout=5.,
encoding=None,
initial_read_buffer_size=None,
host: str = '127.0.0.1',
port: Union[int, str] = 3301,
username: Optional[str] = None,
password: Optional[str] = None,
fetch_schema: bool = True,
auto_refetch_schema: bool = True,
connect_timeout: float = 3.,
request_timeout: float = -1.,
reconnect_timeout: float = 1. / 3.,
ping_timeout: float = 5.,
encoding: Optional[str] = None,
initial_read_buffer_size: Optional[int] = None,
**kwargs):

"""
Expand Down Expand Up @@ -159,7 +162,7 @@ def __init__(self, *,
else:
self.__create_task = asyncio.ensure_future

def _set_state(self, new_state):
def _set_state(self, new_state: ConnectionState):
if self._state != new_state:
logger.debug('Changing state %s -> %s',
self._state.name, new_state.name)
Expand Down Expand Up @@ -196,7 +199,7 @@ async def _ping_task_func(self):

await asyncio.sleep(self._ping_timeout)

def _start_reconnect(self, return_exceptions=False):
def _start_reconnect(self, return_exceptions: bool = False):
if self._state in [ConnectionState.CONNECTING,
ConnectionState.RECONNECTING]:
logger.debug('%s Cannot start reconnect: already reconnecting',
Expand All @@ -213,7 +216,9 @@ def _start_reconnect(self, return_exceptions=False):
self._connect(return_exceptions=return_exceptions)
)

def protocol_factory(self, connected_fut, cls=protocol.Protocol):
def protocol_factory(self,
connected_fut: asyncio.Future,
cls=protocol.Protocol):
return cls(host=self._host,
port=self._port,
username=self._username,
Expand All @@ -228,7 +233,7 @@ def protocol_factory(self, connected_fut, cls=protocol.Protocol):
on_connection_lost=self.connection_lost,
loop=self._loop)

async def _connect(self, return_exceptions=True):
async def _connect(self, return_exceptions: bool = True):
async with self._connect_lock:
while True:
try:
Expand Down Expand Up @@ -587,7 +592,7 @@ async def refetch_schema(self):
"""
await self._protocol.refetch_schema()

def ping(self, *, timeout=-1.0) -> _MethodRet:
def ping(self, *, timeout: float = -1.0) -> _MethodRet:
"""
Ping request coroutine
Expand All @@ -597,8 +602,12 @@ def ping(self, *, timeout=-1.0) -> _MethodRet:
"""
return self._db.ping(timeout=timeout)

def call16(self, func_name, args=None, *,
timeout=-1.0, push_subscribe=False) -> _MethodRet:
def call16(self,
func_name: str,
args: Optional[List[Any]] = None,
*,
timeout: float = -1.0,
push_subscribe: bool = False) -> _MethodRet:
"""
Call16 request coroutine. It is a call with an old behaviour
(return result of a Tarantool procedure is wrapped into a tuple,
Expand All @@ -615,8 +624,12 @@ def call16(self, func_name, args=None, *,
timeout=timeout,
push_subscribe=push_subscribe)

def call(self, func_name, args=None, *,
timeout=-1.0, push_subscribe=False) -> _MethodRet:
def call(self,
func_name: str,
args: Optional[List[Any]] = None,
*,
timeout: float = -1.0,
push_subscribe: bool = False) -> _MethodRet:
"""
Call request coroutine. It is a call with a new behaviour
(return result of a Tarantool procedure is not wrapped into
Expand Down Expand Up @@ -648,8 +661,12 @@ def call(self, func_name, args=None, *,
return self._db.call(func_name, args,
timeout=timeout, push_subscribe=push_subscribe)

def eval(self, expression, args=None, *,
timeout=-1.0, push_subscribe=False) -> _MethodRet:
def eval(self,
expression: str,
args: Optional[List[Any]] = None,
*,
timeout: float = -1.0,
push_subscribe: bool = False) -> _MethodRet:
"""
Eval request coroutine.
Expand All @@ -675,7 +692,10 @@ def eval(self, expression, args=None, *,
return self._db.eval(expression, args,
timeout=timeout, push_subscribe=push_subscribe)

def select(self, space, key=None, **kwargs) -> _MethodRet:
def select(self,
space: SpaceType,
key: Optional[KeyType] = None,
**kwargs) -> _MethodRet:
"""
Select request coroutine.
Expand Down Expand Up @@ -720,7 +740,12 @@ def select(self, space, key=None, **kwargs) -> _MethodRet:
"""
return self._db.select(space, key, **kwargs)

def insert(self, space, t, *, replace=False, timeout=-1) -> _MethodRet:
def insert(self,
space: SpaceType,
t: TupleType,
*,
replace: bool = False,
timeout: float = -1) -> _MethodRet:
"""
Insert request coroutine.
Expand Down Expand Up @@ -754,7 +779,11 @@ def insert(self, space, t, *, replace=False, timeout=-1) -> _MethodRet:
replace=replace,
timeout=timeout)

def replace(self, space, t, *, timeout=-1.0) -> _MethodRet:
def replace(self,
space: SpaceType,
t: TupleType,
*,
timeout: float = -1.0) -> _MethodRet:
"""
Replace request coroutine. Same as insert, but replace.
Expand All @@ -766,7 +795,10 @@ def replace(self, space, t, *, timeout=-1.0) -> _MethodRet:
"""
return self._db.replace(space, t, timeout=timeout)

def delete(self, space, key, **kwargs) -> _MethodRet:
def delete(self,
space: SpaceType,
key: KeyType,
**kwargs) -> _MethodRet:
"""
Delete request coroutine.
Expand All @@ -790,7 +822,11 @@ def delete(self, space, key, **kwargs) -> _MethodRet:
"""
return self._db.delete(space, key, **kwargs)

def update(self, space, key, operations, **kwargs) -> _MethodRet:
def update(self,
space: SpaceType,
key: KeyType,
operations: List[Any],
**kwargs) -> _MethodRet:
"""
Update request coroutine.
Expand Down Expand Up @@ -828,7 +864,11 @@ def update(self, space, key, operations, **kwargs) -> _MethodRet:
"""
return self._db.update(space, key, operations, **kwargs)

def upsert(self, space, t, operations, **kwargs) -> _MethodRet:
def upsert(self,
space: SpaceType,
t: TupleType,
operations: List[Any],
**kwargs) -> _MethodRet:
"""
Update request coroutine. Performs either insert or update
(depending of either tuple exists or not)
Expand Down Expand Up @@ -858,26 +898,29 @@ def upsert(self, space, t, operations, **kwargs) -> _MethodRet:
"""
return self._db.upsert(space, t, operations, **kwargs)

def sql(self, query, args=None, *,
parse_metadata=True, timeout=-1.0) -> _MethodRet:
def execute(self,
query: str,
args: Optional[List[Any]] = None, *,
parse_metadata: bool = True,
timeout: float = -1.0) -> _MethodRet:
"""
Executes an SQL statement (only for Tarantool > 2)
Examples:
.. code-block:: pycon
>>> await conn.sql("select 1 as a, 2 as b")
>>> await conn.execute("select 1 as a, 2 as b")
<Response sync=3 rowcount=1 data=[<TarantoolTuple A=1 B=2>]>
>>> await conn.sql("select * from sql_space")
>>> await conn.execute("select * from sql_space")
<Response sync=3 rowcount=2 data=[
<TarantoolTuple ID=1 NAME='James Bond'>,
<TarantoolTuple ID=2 NAME='Ethan Hunt'>
]>
>>> await conn.sql("select * from sql_space",
... parse_metadata=False)
>>> await conn.execute("select * from sql_space",
... parse_metadata=False)
<Response sync=3 rowcount=2 data=[
<TarantoolTuple 0=1 1='James Bond'>,
<TarantoolTuple 0=2 1='Ethan Hunt'>
Expand All @@ -891,9 +934,9 @@ def sql(self, query, args=None, *,
:returns: :class:`asynctnt.Response` instance
"""
return self._db.sql(query, args,
parse_metadata=parse_metadata,
timeout=timeout)
return self._db.execute(query, args,
parse_metadata=parse_metadata,
timeout=timeout)

def _normalize_api(self):
if (1, 6) <= self.version < (1, 7): # pragma: nocover
Expand Down
2 changes: 1 addition & 1 deletion asynctnt/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TarantoolDatabaseError(TarantoolError):
"""
Exception is raised when Tarantool responds with code != 0
"""
def __init__(self, code, message):
def __init__(self, code: int, message: str):
super(TarantoolDatabaseError, self).__init__(code, message)
self.code = code
self.message = message
Expand Down
2 changes: 0 additions & 2 deletions asynctnt/iproto/db.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -283,5 +283,3 @@ cdef class Db:
def execute(self, query, args, parse_metadata=True, timeout=-1):
return self._execute(query, args, <bint> parse_metadata, timeout,
<bint> False, <bint> True)

sql = execute
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[metadata]
description-file = README.md
description_file = README.md
14 changes: 7 additions & 7 deletions temp/superbench.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ def main():

scenarios = [
['ping', []],
# ['call', ['test']],
# ['call', ['test'], dict(push_subscribe=True)],
# ['eval', ['return "hello"']],
# ['select', [512]],
# ['replace', [512, [2, 'hhhh']]],
# ['update', [512, [2], [(':', 1, 1, 3, 'yo!')]]],
# ['sql', ['select 1 as a, 2 as b'], dict(parse_metadata=False)],
['call', ['test']],
['call', ['test'], dict(push_subscribe=True)],
['eval', ['return "hello"']],
['select', [512]],
['replace', [512, [2, 'hhhh']]],
['update', [512, [2], [(':', 1, 1, 3, 'yo!')]]],
['sql', ['select 1 as a, 2 as b'], dict(parse_metadata=False)],
]

for use_uvloop in [True, ]:
Expand Down

0 comments on commit c04fef2

Please sign in to comment.