From fa51d8718c7e53c1b22059d74bb80dfbb57db707 Mon Sep 17 00:00:00 2001 From: Zixuan James Li Date: Tue, 21 Jun 2022 16:53:25 -0400 Subject: [PATCH] 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 --- zerver/lib/home.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zerver/lib/home.py b/zerver/lib/home.py index 919ab43e63..37678a79ca 100644 --- a/zerver/lib/home.py +++ b/zerver/lib/home.py @@ -1,7 +1,7 @@ import calendar import time 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.http import HttpRequest @@ -125,7 +125,7 @@ def build_page_params_for_home_page_load( first_in_realm: bool, prompt_for_invites: 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. @@ -179,7 +179,7 @@ def build_page_params_for_home_page_load( # Pass parameters to the client-side JavaScript code. # These end up in a JavaScript Object named 'page_params'. - page_params = dict( + page_params: Dict[str, object] = dict( ## Server settings. test_suite=settings.TEST_SUITE, insecure_desktop_app=insecure_desktop_app,