replace "type: ignore" with typing.cast and add Application.workers property

This commit is contained in:
Izalia Mae 2024-08-26 22:20:48 -04:00
parent 501ac2cabb
commit d44cee6d01

View file

@ -18,9 +18,9 @@ from datetime import datetime, timedelta
from mimetypes import guess_type from mimetypes import guess_type
from pathlib import Path from pathlib import Path
from threading import Event, Thread from threading import Event, Thread
from typing import Any from typing import Any, cast
from . import logger as logging, workers from . import logger as logging
from .cache import Cache, get_cache from .cache import Cache, get_cache
from .config import Config from .config import Config
from .database import Connection, get_database from .database import Connection, get_database
@ -32,6 +32,7 @@ from .template import Template
from .views import VIEWS from .views import VIEWS
from .views.api import handle_api_path from .views.api import handle_api_path
from .views.frontend import handle_frontend_path from .views.frontend import handle_frontend_path
from .workers import PushWorkers
def get_csp(request: web.Request) -> str: def get_csp(request: web.Request) -> str:
@ -78,7 +79,7 @@ class Application(web.Application):
self['cache'].setup() self['cache'].setup()
self['template'] = Template(self) self['template'] = Template(self)
self['push_queue'] = multiprocessing.Queue() self['push_queue'] = multiprocessing.Queue()
self['workers'] = workers.PushWorkers(self.config.workers) self['workers'] = PushWorkers(self.config.workers)
self.cache.setup() self.cache.setup()
self.on_cleanup.append(handle_cleanup) # type: ignore self.on_cleanup.append(handle_cleanup) # type: ignore
@ -95,27 +96,27 @@ class Application(web.Application):
@property @property
def cache(self) -> Cache: def cache(self) -> Cache:
return self['cache'] # type: ignore[no-any-return] return cast(Cache, self['cache'])
@property @property
def client(self) -> HttpClient: def client(self) -> HttpClient:
return self['client'] # type: ignore[no-any-return] return cast(HttpClient, self['client'])
@property @property
def config(self) -> Config: def config(self) -> Config:
return self['config'] # type: ignore[no-any-return] return cast(Config, self['config'])
@property @property
def database(self) -> Database[Connection]: def database(self) -> Database[Connection]:
return self['database'] # type: ignore[no-any-return] return cast(Database[Connection], self['database'])
@property @property
def signer(self) -> Signer: def signer(self) -> Signer:
return self['signer'] # type: ignore[no-any-return] return cast(Signer, self['signer'])
@signer.setter @signer.setter
@ -129,7 +130,7 @@ class Application(web.Application):
@property @property
def template(self) -> Template: def template(self) -> Template:
return self['template'] # type: ignore[no-any-return] return cast(Template, self['template'])
@property @property
@ -142,6 +143,11 @@ class Application(web.Application):
return timedelta(seconds=uptime.seconds) return timedelta(seconds=uptime.seconds)
@property
def workers(self) -> PushWorkers:
return cast(PushWorkers, self['workers'])
def push_message(self, inbox: str, message: Message, instance: Instance) -> None: def push_message(self, inbox: str, message: Message, instance: Instance) -> None:
self['workers'].push_message(inbox, message, instance) self['workers'].push_message(inbox, message, instance)