home: Prevent mypy from inferring the type of page_params.

Without an explicit type annotation, mypy infers the type of
values in `page_params` upon its initialization as a `Union`, while
other computed values haven't been assigned yet. We break this
over-conservative inferred type by annotating `page_params` as a
`Dict[str, object]`.

We could have created a `TypedDict` to have accurate type annotation
for all of the fields, but it does not seem worth it at this point since
the data structure is not widely used in the backend.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Zixuan James Li 2022-06-21 16:53:25 -04:00 committed by Tim Abbott
parent 67fda5516f
commit fa51d8718c
1 changed files with 3 additions and 3 deletions

View File

@ -1,7 +1,7 @@
import calendar import calendar
import time import time
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Tuple from typing import Dict, List, Optional, Tuple
from django.conf import settings from django.conf import settings
from django.http import HttpRequest from django.http import HttpRequest
@ -125,7 +125,7 @@ def build_page_params_for_home_page_load(
first_in_realm: bool, first_in_realm: bool,
prompt_for_invites: bool, prompt_for_invites: bool,
needs_tutorial: bool, needs_tutorial: bool,
) -> Tuple[int, Dict[str, Any]]: ) -> Tuple[int, Dict[str, object]]:
""" """
This function computes page_params for when we load the home page. This function computes page_params for when we load the home page.
@ -179,7 +179,7 @@ def build_page_params_for_home_page_load(
# Pass parameters to the client-side JavaScript code. # Pass parameters to the client-side JavaScript code.
# These end up in a JavaScript Object named 'page_params'. # These end up in a JavaScript Object named 'page_params'.
page_params = dict( page_params: Dict[str, object] = dict(
## Server settings. ## Server settings.
test_suite=settings.TEST_SUITE, test_suite=settings.TEST_SUITE,
insecure_desktop_app=insecure_desktop_app, insecure_desktop_app=insecure_desktop_app,