fix pyright type errors

This commit is contained in:
bre-17387639 2024-08-06 16:46:15 -06:00 committed by David Lord
parent 5e8cb74018
commit 9e831e915f
No known key found for this signature in database
GPG Key ID: 43368A7AA8CC5926
10 changed files with 22 additions and 19 deletions

View File

@ -95,7 +95,7 @@ ignore_missing_imports = true
[tool.pyright]
pythonVersion = "3.8"
include = ["src/flask", "tests"]
include = ["src/flask", "tests/typing"]
typeCheckingMode = "basic"
[tool.ruff]

View File

@ -59,6 +59,7 @@ if t.TYPE_CHECKING: # pragma: no cover
from .testing import FlaskClient
from .testing import FlaskCliRunner
from .typing import HeadersValue
T_shell_context_processor = t.TypeVar(
"T_shell_context_processor", bound=ft.ShellContextProcessorCallable
@ -349,7 +350,7 @@ class Flask(App):
path = os.path.join(self.root_path, resource)
if mode == "rb":
return open(path, mode)
return open(path, mode) # pyright: ignore
return open(path, mode, encoding=encoding)
@ -1163,7 +1164,8 @@ class Flask(App):
response object.
"""
status = headers = None
status: int | None = None
headers: HeadersValue | None = None
# unpack tuple returns
if isinstance(rv, tuple):
@ -1175,7 +1177,7 @@ class Flask(App):
# decide if a 2-tuple has status or headers
elif len_rv == 2:
if isinstance(rv[1], (Headers, dict, tuple, list)):
rv, headers = rv
rv, headers = rv # pyright: ignore
else:
rv, status = rv # type: ignore[assignment,misc]
# other sized tuples are not allowed

View File

@ -123,6 +123,6 @@ class Blueprint(SansioBlueprint):
path = os.path.join(self.root_path, resource)
if mode == "rb":
return open(path, mode)
return open(path, mode) # pyright: ignore
return open(path, mode, encoding=encoding)

View File

@ -323,9 +323,9 @@ class ScriptInfo:
"""
if self._loaded_app is not None:
return self._loaded_app
app: Flask | None = None
if self.create_app is not None:
app: Flask | None = self.create_app()
app = self.create_app()
else:
if self.app_import_path:
path, name = (
@ -549,7 +549,7 @@ class FlaskGroup(AppGroup):
set_debug_flag: bool = True,
**extra: t.Any,
) -> None:
params = list(extra.pop("params", None) or ())
params: list[click.Parameter] = list(extra.pop("params", None) or ())
# Processing is done with option callbacks instead of a group
# callback. This allows users to make a custom group callback
# without losing the behavior. --env-file must come first so
@ -587,7 +587,7 @@ class FlaskGroup(AppGroup):
# Use a backport on Python < 3.10. We technically have
# importlib.metadata on 3.8+, but the API changed in 3.10,
# so use the backport for consistency.
import importlib_metadata as metadata
import importlib_metadata as metadata # pyright: ignore
for ep in metadata.entry_points(group="flask.commands"):
self.add_command(ep.load(), ep.name)
@ -934,7 +934,7 @@ def run_command(
option.
"""
try:
app: WSGIApplication = info.load_app()
app: WSGIApplication = info.load_app() # pyright: ignore
except Exception as e:
if is_running_from_reloader():
# When reloading, print out the error immediately, but raise

View File

@ -599,7 +599,7 @@ def get_root_path(import_name: str) -> str:
return os.getcwd()
if hasattr(loader, "get_filename"):
filepath = loader.get_filename(import_name)
filepath = loader.get_filename(import_name) # pyright: ignore
else:
# Fall back to imports.
__import__(import_name)

View File

@ -628,7 +628,7 @@ class App(Scaffold):
methods = {item.upper() for item in methods}
# Methods that should always be added
required_methods = set(getattr(view_func, "required_methods", ()))
required_methods: set[str] = set(getattr(view_func, "required_methods", ()))
# starting with Flask 0.8 the view_func object can disable and
# force-enable the automatic options handling.

View File

@ -79,8 +79,7 @@ class EnvironBuilder(werkzeug.test.EnvironBuilder):
path = url.path
if url.query:
sep = b"?" if isinstance(url.query, bytes) else "?"
path += sep + url.query
path = f"{path}?{url.query}"
self.app = app
super().__init__(path, base_url, *args, **kwargs)

View File

@ -61,7 +61,7 @@ class View:
#: decorator.
#:
#: .. versionadded:: 0.8
decorators: t.ClassVar[list[t.Callable[[F], F]]] = []
decorators: t.ClassVar[list[t.Callable[..., t.Any]]] = []
#: Create a new instance of this view class for every request by
#: default. If a view subclass sets this to ``False``, the same
@ -110,7 +110,7 @@ class View:
return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return]
else:
self = cls(*class_args, **class_kwargs)
self = cls(*class_args, **class_kwargs) # pyright: ignore
def view(**kwargs: t.Any) -> ft.ResponseReturnValue:
return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return]

View File

@ -129,11 +129,11 @@ class Request(RequestBase):
def on_json_loading_failed(self, e: ValueError | None) -> t.Any:
try:
return super().on_json_loading_failed(e)
except BadRequest as e:
except BadRequest as ebr:
if current_app and current_app.debug:
raise
raise BadRequest() from e
raise BadRequest() from ebr
class Response(ResponseBase):

View File

@ -28,7 +28,9 @@ commands = pre-commit run --all-files
[testenv:typing]
deps = -r requirements/typing.txt
commands = mypy
commands =
mypy
pyright
[testenv:docs]
deps = -r requirements/docs.txt