Skip to content

Commit

Permalink
fix pyright type errors (#5620)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism authored Oct 24, 2024
2 parents 5e8cb74 + 9e831e9 commit e8b91cd
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 5 additions & 3 deletions src/flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@

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
Expand Down Expand Up @@ -349,7 +350,7 @@ def open_resource(
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)

Expand Down Expand Up @@ -1163,7 +1164,8 @@ def make_response(self, rv: ft.ResponseReturnValue) -> Response:
response object.
"""

status = headers = None
status: int | None = None
headers: HeadersValue | None = None

# unpack tuple returns
if isinstance(rv, tuple):
Expand All @@ -1175,7 +1177,7 @@ def make_response(self, rv: ft.ResponseReturnValue) -> Response:
# 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
Expand Down
2 changes: 1 addition & 1 deletion src/flask/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,6 @@ def open_resource(
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)
10 changes: 5 additions & 5 deletions src/flask/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,9 @@ def load_app(self) -> Flask:
"""
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 = (
Expand Down Expand Up @@ -549,7 +549,7 @@ def __init__(
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
Expand Down Expand Up @@ -587,7 +587,7 @@ def _load_plugin_commands(self) -> None:
# 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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/flask/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/flask/sansio/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ def add_url_rule(
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.
Expand Down
3 changes: 1 addition & 2 deletions src/flask/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ def __init__(
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)
Expand Down
4 changes: 2 additions & 2 deletions src/flask/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def dispatch_request(self, name):
#: 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
Expand Down Expand Up @@ -110,7 +110,7 @@ def view(**kwargs: t.Any) -> ft.ResponseReturnValue:
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]
Expand Down
4 changes: 2 additions & 2 deletions src/flask/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ def _load_form_data(self) -> None:
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):
Expand Down
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e8b91cd

Please sign in to comment.