Skip to content

Commit e8b91cd

Browse files
authored
fix pyright type errors (pallets#5620)
2 parents 5e8cb74 + 9e831e9 commit e8b91cd

10 files changed

+22
-19
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ ignore_missing_imports = true
9595

9696
[tool.pyright]
9797
pythonVersion = "3.8"
98-
include = ["src/flask", "tests"]
98+
include = ["src/flask", "tests/typing"]
9999
typeCheckingMode = "basic"
100100

101101
[tool.ruff]

src/flask/app.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959

6060
from .testing import FlaskClient
6161
from .testing import FlaskCliRunner
62+
from .typing import HeadersValue
6263

6364
T_shell_context_processor = t.TypeVar(
6465
"T_shell_context_processor", bound=ft.ShellContextProcessorCallable
@@ -349,7 +350,7 @@ def open_resource(
349350
path = os.path.join(self.root_path, resource)
350351

351352
if mode == "rb":
352-
return open(path, mode)
353+
return open(path, mode) # pyright: ignore
353354

354355
return open(path, mode, encoding=encoding)
355356

@@ -1163,7 +1164,8 @@ def make_response(self, rv: ft.ResponseReturnValue) -> Response:
11631164
response object.
11641165
"""
11651166

1166-
status = headers = None
1167+
status: int | None = None
1168+
headers: HeadersValue | None = None
11671169

11681170
# unpack tuple returns
11691171
if isinstance(rv, tuple):
@@ -1175,7 +1177,7 @@ def make_response(self, rv: ft.ResponseReturnValue) -> Response:
11751177
# decide if a 2-tuple has status or headers
11761178
elif len_rv == 2:
11771179
if isinstance(rv[1], (Headers, dict, tuple, list)):
1178-
rv, headers = rv
1180+
rv, headers = rv # pyright: ignore
11791181
else:
11801182
rv, status = rv # type: ignore[assignment,misc]
11811183
# other sized tuples are not allowed

src/flask/blueprints.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,6 @@ def open_resource(
123123
path = os.path.join(self.root_path, resource)
124124

125125
if mode == "rb":
126-
return open(path, mode)
126+
return open(path, mode) # pyright: ignore
127127

128128
return open(path, mode, encoding=encoding)

src/flask/cli.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,9 @@ def load_app(self) -> Flask:
323323
"""
324324
if self._loaded_app is not None:
325325
return self._loaded_app
326-
326+
app: Flask | None = None
327327
if self.create_app is not None:
328-
app: Flask | None = self.create_app()
328+
app = self.create_app()
329329
else:
330330
if self.app_import_path:
331331
path, name = (
@@ -549,7 +549,7 @@ def __init__(
549549
set_debug_flag: bool = True,
550550
**extra: t.Any,
551551
) -> None:
552-
params = list(extra.pop("params", None) or ())
552+
params: list[click.Parameter] = list(extra.pop("params", None) or ())
553553
# Processing is done with option callbacks instead of a group
554554
# callback. This allows users to make a custom group callback
555555
# without losing the behavior. --env-file must come first so
@@ -587,7 +587,7 @@ def _load_plugin_commands(self) -> None:
587587
# Use a backport on Python < 3.10. We technically have
588588
# importlib.metadata on 3.8+, but the API changed in 3.10,
589589
# so use the backport for consistency.
590-
import importlib_metadata as metadata
590+
import importlib_metadata as metadata # pyright: ignore
591591

592592
for ep in metadata.entry_points(group="flask.commands"):
593593
self.add_command(ep.load(), ep.name)
@@ -934,7 +934,7 @@ def run_command(
934934
option.
935935
"""
936936
try:
937-
app: WSGIApplication = info.load_app()
937+
app: WSGIApplication = info.load_app() # pyright: ignore
938938
except Exception as e:
939939
if is_running_from_reloader():
940940
# When reloading, print out the error immediately, but raise

src/flask/helpers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ def get_root_path(import_name: str) -> str:
599599
return os.getcwd()
600600

601601
if hasattr(loader, "get_filename"):
602-
filepath = loader.get_filename(import_name)
602+
filepath = loader.get_filename(import_name) # pyright: ignore
603603
else:
604604
# Fall back to imports.
605605
__import__(import_name)

src/flask/sansio/app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ def add_url_rule(
628628
methods = {item.upper() for item in methods}
629629

630630
# Methods that should always be added
631-
required_methods = set(getattr(view_func, "required_methods", ()))
631+
required_methods: set[str] = set(getattr(view_func, "required_methods", ()))
632632

633633
# starting with Flask 0.8 the view_func object can disable and
634634
# force-enable the automatic options handling.

src/flask/testing.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ def __init__(
7979
path = url.path
8080

8181
if url.query:
82-
sep = b"?" if isinstance(url.query, bytes) else "?"
83-
path += sep + url.query
82+
path = f"{path}?{url.query}"
8483

8584
self.app = app
8685
super().__init__(path, base_url, *args, **kwargs)

src/flask/views.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def dispatch_request(self, name):
6161
#: decorator.
6262
#:
6363
#: .. versionadded:: 0.8
64-
decorators: t.ClassVar[list[t.Callable[[F], F]]] = []
64+
decorators: t.ClassVar[list[t.Callable[..., t.Any]]] = []
6565

6666
#: Create a new instance of this view class for every request by
6767
#: default. If a view subclass sets this to ``False``, the same
@@ -110,7 +110,7 @@ def view(**kwargs: t.Any) -> ft.ResponseReturnValue:
110110
return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return]
111111

112112
else:
113-
self = cls(*class_args, **class_kwargs)
113+
self = cls(*class_args, **class_kwargs) # pyright: ignore
114114

115115
def view(**kwargs: t.Any) -> ft.ResponseReturnValue:
116116
return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return]

src/flask/wrappers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ def _load_form_data(self) -> None:
129129
def on_json_loading_failed(self, e: ValueError | None) -> t.Any:
130130
try:
131131
return super().on_json_loading_failed(e)
132-
except BadRequest as e:
132+
except BadRequest as ebr:
133133
if current_app and current_app.debug:
134134
raise
135135

136-
raise BadRequest() from e
136+
raise BadRequest() from ebr
137137

138138

139139
class Response(ResponseBase):

tox.ini

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ commands = pre-commit run --all-files
2828

2929
[testenv:typing]
3030
deps = -r requirements/typing.txt
31-
commands = mypy
31+
commands =
32+
mypy
33+
pyright
3234

3335
[testenv:docs]
3436
deps = -r requirements/docs.txt

0 commit comments

Comments
 (0)