From 1ed618222f17e205b684f6ef63cdaa470beb0dcb Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Mon, 17 Jan 2022 07:54:37 +0100 Subject: [PATCH] [typing] add ExtendedRequest webapp.py monkey-patches the Flask request global. This commit adds a type cast so that e.g. Pyright[1] doesn't show "Cannot access member" errors everywhere. [1]: https://github.com/microsoft/pyright --- searx/webapp.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/searx/webapp.py b/searx/webapp.py index 3df9f2876..905f53d1f 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -14,6 +14,8 @@ from datetime import datetime, timedelta from timeit import default_timer from html import escape from io import StringIO +import typing +from typing import List, Dict import urllib from urllib.parse import urlencode @@ -28,7 +30,6 @@ import flask from flask import ( Flask, - request, render_template, url_for, Response, @@ -89,7 +90,7 @@ from searx.utils import ( ) from searx.version import VERSION_STRING, GIT_URL, GIT_BRANCH from searx.query import RawTextQuery -from searx.plugins import plugins, initialize as plugin_initialize +from searx.plugins import Plugin, plugins, initialize as plugin_initialize from searx.plugins.oa_doi_rewrite import get_doi_resolver from searx.preferences import ( Preferences, @@ -224,6 +225,21 @@ exception_classname_to_text = { _flask_babel_get_translations = flask_babel.get_translations +class ExtendedRequest(flask.Request): + """This class is never initialized and only used for type checking.""" + + preferences: Preferences + errors: List[str] + user_plugins: List[Plugin] + form: Dict[str, str] + start_time: float + render_time: float + timings: List[dict] + + +request = typing.cast(ExtendedRequest, flask.request) + + def _get_translations(): if has_request_context() and request.form.get('use-translation') == 'oc': babel_ext = flask_babel.current_app.extensions['babel']