Fixes Pyright type errors for src/flask/app.py

This commit is contained in:
bre-17387639 2024-08-06 16:46:15 -06:00
parent 8a6cdf1e2a
commit adbc23dcb8
2 changed files with 14 additions and 9 deletions

View file

@ -6,6 +6,7 @@ Version 3.1.0
- ``Flask.open_resource``/``open_instance_resource`` and - ``Flask.open_resource``/``open_instance_resource`` and
``Blueprint.open_resource`` take an ``encoding`` parameter to use when ``Blueprint.open_resource`` take an ``encoding`` parameter to use when
opening in text mode. It defaults to ``utf-8``. :issue:`5504` opening in text mode. It defaults to ``utf-8``. :issue:`5504`
- Fixes Pyright type errors in ``src/flask/app.py``. :issue:`5543`
Version 3.0.3 Version 3.0.3
------------- -------------

View file

@ -7,6 +7,7 @@ import typing as t
import weakref import weakref
from datetime import timedelta from datetime import timedelta
from inspect import iscoroutinefunction from inspect import iscoroutinefunction
from io import BufferedReader
from itertools import chain from itertools import chain
from types import TracebackType from types import TracebackType
from urllib.parse import quote as _url_quote from urllib.parse import quote as _url_quote
@ -322,7 +323,7 @@ class Flask(App):
def open_resource( def open_resource(
self, resource: str, mode: str = "rb", encoding: str | None = None self, resource: str, mode: str = "rb", encoding: str | None = None
) -> t.IO[t.AnyStr]: ) -> t.IO[t.AnyStr] | BufferedReader:
"""Open a resource file relative to :attr:`root_path` for reading. """Open a resource file relative to :attr:`root_path` for reading.
For example, if the file ``schema.sql`` is next to the file For example, if the file ``schema.sql`` is next to the file
@ -1167,17 +1168,20 @@ class Flask(App):
# unpack tuple returns # unpack tuple returns
if isinstance(rv, tuple): if isinstance(rv, tuple):
len_rv = len(rv)
# a 3-tuple is unpacked directly # a 3-tuple is unpacked directly
if len_rv == 3: if len(rv) == 3:
rv, status, headers = rv # type: ignore[misc] rv, status, headers = rv
# decide if a 2-tuple has status or headers # decide if a 2-tuple has status or headers
elif len_rv == 2: elif len(rv) == 2:
if isinstance(rv[1], (Headers, dict, tuple, list)): rv0 = rv[0]
rv, headers = rv rv1 = rv[1]
if isinstance(rv1, (Headers, dict, tuple, list)):
rv = rv0
headers = rv1
else: else:
rv, status = rv # type: ignore[assignment,misc] rv = rv0
if isinstance(rv1, int):
status = rv1
# other sized tuples are not allowed # other sized tuples are not allowed
else: else:
raise TypeError( raise TypeError(