Improve typechecking

This commit is contained in:
Matthias 2022-11-17 10:16:38 +00:00
parent 097af973d2
commit 93addbe5c3

View File

@ -2,7 +2,7 @@ import asyncio
import logging import logging
from ipaddress import IPv4Address from ipaddress import IPv4Address
from threading import Thread from threading import Thread
from typing import Any, Dict from typing import Any, Dict, Optional
import orjson import orjson
import uvicorn import uvicorn
@ -51,9 +51,9 @@ class ApiServer(RPCHandler):
# Exchange - only available in webserver mode. # Exchange - only available in webserver mode.
_exchange = None _exchange = None
# websocket message queue stuff # websocket message queue stuff
_ws_channel_manager = None _ws_channel_manager: ChannelManager
_ws_thread = None _ws_thread = None
_ws_loop = None _ws_loop: Optional[asyncio.AbstractEventLoop] = None
def __new__(cls, *args, **kwargs): def __new__(cls, *args, **kwargs):
""" """
@ -71,7 +71,7 @@ class ApiServer(RPCHandler):
return return
self._standalone: bool = standalone self._standalone: bool = standalone
self._server = None self._server = None
self._ws_queue = None self._ws_queue: Optional[ThreadedQueue] = None
self._ws_background_task = None self._ws_background_task = None
ApiServer.__initialized = True ApiServer.__initialized = True
@ -186,7 +186,7 @@ class ApiServer(RPCHandler):
self._ws_background_task = asyncio.run_coroutine_threadsafe( self._ws_background_task = asyncio.run_coroutine_threadsafe(
self._broadcast_queue_data(), loop=self._ws_loop) self._broadcast_queue_data(), loop=self._ws_loop)
async def _broadcast_queue_data(self): async def _broadcast_queue_data(self) -> None:
# Instantiate the queue in this coroutine so it's attached to our loop # Instantiate the queue in this coroutine so it's attached to our loop
self._ws_queue = ThreadedQueue() self._ws_queue = ThreadedQueue()
async_queue = self._ws_queue.async_q async_queue = self._ws_queue.async_q
@ -210,7 +210,8 @@ class ApiServer(RPCHandler):
finally: finally:
# Disconnect channels and stop the loop on cancel # Disconnect channels and stop the loop on cancel
await self._ws_channel_manager.disconnect_all() await self._ws_channel_manager.disconnect_all()
self._ws_loop.stop() if self._ws_loop:
self._ws_loop.stop()
# Avoid adding more items to the queue if they aren't # Avoid adding more items to the queue if they aren't
# going to get broadcasted. # going to get broadcasted.
self._ws_queue = None self._ws_queue = None