Skip to content

Commit

Permalink
Converted Type Annotations to Py3 syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mimi89999 committed Dec 12, 2019
1 parent 680f5ad commit 7ffc82a
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions aiodns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,69 +37,61 @@


class DNSResolver:
def __init__(self, nameservers=None, loop=None, **kwargs):
# type: (Optional[List[str]], Optional[asyncio.AbstractEventLoop], Any) -> None
def __init__(self, nameservers: Optional[List[str]] = None, loop: Optional[asyncio.AbstractEventLoop] = None,
**kwargs: Any) -> None:
self.loop = loop or asyncio.get_event_loop()
assert self.loop is not None
kwargs.pop('sock_state_cb', None)
self._channel = pycares.Channel(sock_state_cb=self._sock_state_cb, **kwargs)
if nameservers:
self.nameservers = nameservers
self._read_fds = set() # type: Set[int]
self._write_fds = set() # type: Set[int]
self._timer = None # type: Optional[asyncio.TimerHandle]
self._read_fds: Set[int] = set()
self._write_fds: Set[int] = set()
self._timer: Optional[asyncio.TimerHandle] = None

@property
def nameservers(self):
# type: () -> pycares.Channel
def nameservers(self) -> pycares.Channel:
return self._channel.servers

@nameservers.setter
def nameservers(self, value):
# type: (List[str]) -> None
def nameservers(self, value: List[str]) -> None:
self._channel.servers = value

@staticmethod
def _callback(fut, result, errorno):
# type: (asyncio.Future, Any, int) -> None
def _callback(fut: asyncio.Future[Any], result: Any, errorno: int) -> None:
if fut.cancelled():
return
if errorno is not None:
fut.set_exception(error.DNSError(errorno, pycares.errno.strerror(errorno)))
else:
fut.set_result(result)

def query(self, host, qtype):
# type: (str, str) -> asyncio.Future
def query(self, host: str, qtype: str) -> asyncio.Future[Any]:
try:
qtype = query_type_map[qtype]
except KeyError:
raise ValueError('invalid query type: {}'.format(qtype))
fut = asyncio.Future(loop=self.loop) # type: asyncio.Future
fut: asyncio.Future[Any] = asyncio.Future(loop=self.loop)
cb = functools.partial(self._callback, fut)
self._channel.query(host, qtype, cb)
return fut

def gethostbyname(self, host, family):
# type: (str, socket.AddressFamily) -> asyncio.Future
fut = asyncio.Future(loop=self.loop) # type: asyncio.Future
def gethostbyname(self, host: str, family: socket.AddressFamily) -> asyncio.Future[Any]:
fut: asyncio.Future[Any] = asyncio.Future(loop=self.loop)
cb = functools.partial(self._callback, fut)
self._channel.gethostbyname(host, family, cb)
return fut

def gethostbyaddr(self, name):
# type: (str) -> asyncio.Future
fut = asyncio.Future(loop=self.loop) # type: asyncio.Future
def gethostbyaddr(self, name: str) -> asyncio.Future[Any]:
fut: asyncio.Future[Any] = asyncio.Future(loop=self.loop)
cb = functools.partial(self._callback, fut)
self._channel.gethostbyaddr(name, cb)
return fut

def cancel(self):
# type: () -> None
def cancel(self) -> None:
self._channel.cancel()

def _sock_state_cb(self, fd, readable, writable):
# type: (int, bool, bool) -> None
def _sock_state_cb(self, fd: int, readable: bool, writable: bool) -> None:
if readable or writable:
if readable:
self.loop.add_reader(fd, self._handle_event, fd, READ)
Expand All @@ -123,8 +115,7 @@ def _sock_state_cb(self, fd, readable, writable):
self._timer.cancel()
self._timer = None

def _handle_event(self, fd, event):
# type: (int, Any) -> None
def _handle_event(self, fd: int, event: Any) -> None:
read_fd = pycares.ARES_SOCKET_BAD
write_fd = pycares.ARES_SOCKET_BAD
if event == READ:
Expand All @@ -133,8 +124,7 @@ def _handle_event(self, fd, event):
write_fd = fd
self._channel.process_fd(read_fd, write_fd)

def _timer_cb(self):
# type: () -> None
def _timer_cb(self) -> None:
if self._read_fds or self._write_fds:
self._channel.process_fd(pycares.ARES_SOCKET_BAD, pycares.ARES_SOCKET_BAD)
self._timer = self.loop.call_later(1.0, self._timer_cb)
Expand Down

0 comments on commit 7ffc82a

Please sign in to comment.