Skip to content

Commit

Permalink
some types
Browse files Browse the repository at this point in the history
  • Loading branch information
sonic182 committed Dec 11, 2020
1 parent 98fdbd1 commit f731152
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 21 deletions.
18 changes: 9 additions & 9 deletions aiosonic/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class Connection:
"""Connection class."""
def __init__(self, connector: TCPConnector):
def __init__(self, connector: TCPConnector) -> None:
self.connector = connector
self.reader: Optional[StreamReader] = None
self.writer: Optional[StreamWriter] = None
Expand All @@ -43,7 +43,7 @@ async def connect(self,
verify: bool,
ssl_context: SSLContext,
timeouts: Timeouts,
http2: bool = False):
http2: bool = False) -> None:
"""Connet with timeout."""
try:
await wait_for(self._connect(urlparsed, verify,
Expand All @@ -54,7 +54,7 @@ async def connect(self,
raise ConnectTimeout()

async def _connect(self, urlparsed: ParseResult, verify: bool,
ssl_context: SSLContext, http2: bool):
ssl_context: SSLContext, http2: bool) -> None:
"""Get reader and writer."""
if not urlparsed.hostname:
raise HttpParsingError('missing hostname')
Expand Down Expand Up @@ -90,7 +90,7 @@ def is_closing():
self.temp_key = key
await self._connection_made()

async def _connection_made(self):
async def _connection_made(self) -> None:
tls_conn = self.writer.get_extra_info('ssl_object')
if not tls_conn:
return
Expand All @@ -104,7 +104,7 @@ async def _connection_made(self):
self.h2conn = h2.connection.H2Connection()
self.h2handler = Http2Handler(self)

def keep_alive(self):
def keep_alive(self) -> None:
"""Check if keep alive."""
self.keep = True

Expand All @@ -116,7 +116,7 @@ async def __aenter__(self):
"""Get connection from pool."""
return self

async def __aexit__(self, exc_type, exc, tb):
async def __aexit__(self, exc_type: None, exc: None, tb: None) -> None:
"""Release connection."""
if self.keep and not exc:
self.key = self.temp_key
Expand All @@ -131,19 +131,19 @@ async def __aexit__(self, exc_type, exc, tb):
if self.h2handler:
self.h2handler.cleanup()

async def release(self):
async def release(self) -> None:
"""Release connection."""
await self.connector.release(self)

@property
def timeouts(self) -> Timeouts:
return self.connector.timeouts

def __del__(self):
def __del__(self) -> None:
"""Cleanup."""
self.close(True)

def close(self, check_closing=False):
def close(self, check_closing: bool=False) -> None:
"""Close connection if opened."""
if self.writer:
is_closing = getattr(self.writer, 'is_closing',
Expand Down
11 changes: 3 additions & 8 deletions aiosonic/connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@

from asyncio import wait_for
from asyncio import sleep as asyncio_sleep
from asyncio import StreamReader
from asyncio import StreamWriter
import ssl
from ssl import SSLContext
from typing import Coroutine
from typing import Optional
from urllib.parse import ParseResult

#import h2.connection (unused)
# import h2.connection (unused)
from hyperframe.frame import SettingsFrame

#from concurrent import futures (unused)
from aiosonic.exceptions import ConnectTimeout
# from concurrent import futures (unused)
from aiosonic.exceptions import ConnectionPoolAcquireTimeout
from aiosonic.exceptions import TimeoutException
from aiosonic.pools import SmartPool
Expand Down Expand Up @@ -54,7 +49,7 @@ async def acquire(self, urlparsed: ParseResult):

try:
return await wait_for(self.pool.acquire(urlparsed),
self.timeouts.pool_acquire)
self.timeouts.pool_acquire)
except TimeoutException:
raise ConnectionPoolAcquireTimeout()

Expand Down
5 changes: 3 additions & 2 deletions aiosonic/pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from asyncio import Semaphore
from asyncio import Queue


class CyclicQueuePool:
"""Cyclic queue pool of connections."""
def __init__(self, connector, pool_size, connection_cls):
Expand Down Expand Up @@ -53,7 +54,7 @@ async def acquire(self, urlparsed: ParseResult = None):
return item
return self.pool.pop()

def release(self, conn):
def release(self, conn) -> None:
"""Release connection."""
self.pool.add(conn)
self.sem.release()
Expand All @@ -62,7 +63,7 @@ def is_all_free(self):
"""Indicates if all pool is free."""
return self.pool_size == self.sem._value

async def cleanup(self):
async def cleanup(self) -> None:
"""Get all conn and close them, this method let this pool unusable."""
for count in range(self.pool_size):
conn = await self.acquire()
Expand Down
2 changes: 1 addition & 1 deletion aiosonic/timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def __init__(self,
sock_connect: Optional[float] = 5,
sock_read: Optional[float] = 30,
pool_acquire: Optional[float] = None,
request_timeout: Optional[float] = 60):
request_timeout: Optional[float] = 60) -> None:
"""Timeouts.
Arguments:
Expand Down
3 changes: 2 additions & 1 deletion aiosonic/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Utils."""
from typing import Callable


def cache_decorator(size=512):
def cache_decorator(size: int=512) -> Callable:
"""Dummy cache decorator."""
_cache = {}

Expand Down

0 comments on commit f731152

Please sign in to comment.