[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
This commit is contained in:
Martin Fischer 2022-01-17 07:54:37 +01:00
parent 96ab5e57ff
commit 1ed618222f
1 changed files with 18 additions and 2 deletions

View File

@ -14,6 +14,8 @@ from datetime import datetime, timedelta
from timeit import default_timer from timeit import default_timer
from html import escape from html import escape
from io import StringIO from io import StringIO
import typing
from typing import List, Dict
import urllib import urllib
from urllib.parse import urlencode from urllib.parse import urlencode
@ -28,7 +30,6 @@ import flask
from flask import ( from flask import (
Flask, Flask,
request,
render_template, render_template,
url_for, url_for,
Response, Response,
@ -89,7 +90,7 @@ from searx.utils import (
) )
from searx.version import VERSION_STRING, GIT_URL, GIT_BRANCH from searx.version import VERSION_STRING, GIT_URL, GIT_BRANCH
from searx.query import RawTextQuery 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.plugins.oa_doi_rewrite import get_doi_resolver
from searx.preferences import ( from searx.preferences import (
Preferences, Preferences,
@ -224,6 +225,21 @@ exception_classname_to_text = {
_flask_babel_get_translations = flask_babel.get_translations _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(): def _get_translations():
if has_request_context() and request.form.get('use-translation') == 'oc': if has_request_context() and request.form.get('use-translation') == 'oc':
babel_ext = flask_babel.current_app.extensions['babel'] babel_ext = flask_babel.current_app.extensions['babel']