typing: Amend inaccurate type annotations.

This amend some type annotations that turn out to be inaccurate with
django-stubs.
This commit is contained in:
PIG208 2021-07-26 23:32:10 +08:00 committed by Tim Abbott
parent caaa424ef5
commit e517f967b8
7 changed files with 23 additions and 10 deletions

View File

@ -1,5 +1,8 @@
from typing import List, Union
from django.conf.urls import include
from django.urls import path
from django.urls.resolvers import URLPattern, URLResolver
from analytics.views.installation_activity import get_installation_activity
from analytics.views.realm_activity import get_realm_activity
@ -19,7 +22,7 @@ from analytics.views.support import support
from analytics.views.user_activity import get_user_activity
from zerver.lib.rest import rest_path
i18n_urlpatterns = [
i18n_urlpatterns: List[Union[URLPattern, URLResolver]] = [
# Server admin (user_profile.is_staff) visible stats pages
path("activity", get_installation_activity),
path("activity/support", support, name="support"),

View File

@ -5,7 +5,7 @@ from typing import Any, Dict, List, Optional, Sequence
import pytz
from django.conf import settings
from django.db import connection
from django.db.backends.utils import CursorWrapper
from django.db.models.query import QuerySet
from django.template import loader
from django.urls import reverse
@ -39,7 +39,7 @@ def make_table(
return content
def dictfetchall(cursor: connection.cursor) -> List[Dict[str, Any]]:
def dictfetchall(cursor: CursorWrapper) -> List[Dict[str, Any]]:
"Returns all rows from a cursor as a dict"
desc = cursor.description
return [dict(zip((col[0] for col in desc), row)) for row in cursor.fetchall()]

View File

@ -369,7 +369,7 @@ def ad_hoc_queries() -> List[Dict[str, str]]:
cursor.close()
def fix_rows(
i: int, fixup_func: Union[Callable[[Realm], mark_safe], Callable[[datetime], str]]
i: int, fixup_func: Union[Callable[[str], mark_safe], Callable[[datetime], str]]
) -> None:
for row in rows:
row[i] = fixup_func(row[i])

View File

@ -62,7 +62,7 @@ def realm_user_summary_table(
else:
return ""
def is_recent(val: Optional[datetime]) -> bool:
def is_recent(val: datetime) -> bool:
age = timezone_now() - val
return age.total_seconds() < 5 * 60

View File

@ -814,14 +814,16 @@ def client_is_exempt_from_rate_limiting(request: HttpRequest) -> bool:
)
def internal_notify_view(is_tornado_view: bool) -> Callable[[ViewFuncT], ViewFuncT]:
def internal_notify_view(
is_tornado_view: bool,
) -> Callable[[ViewFuncT], Callable[..., HttpResponse]]:
# The typing here could be improved by using the extended Callable types:
# https://mypy.readthedocs.io/en/stable/additional_features.html#extended-callable-types
"""Used for situations where something running on the Zulip server
needs to make a request to the (other) Django/Tornado processes running on
the server."""
def _wrapped_view_func(view_func: ViewFuncT) -> ViewFuncT:
def _wrapped_view_func(view_func: ViewFuncT) -> Callable[..., HttpResponse]:
@csrf_exempt
@require_post
@wraps(view_func)

View File

@ -1989,10 +1989,18 @@ def do_send_messages(
else:
user_list = list(user_ids)
users: List[Dict[str, Union[int, List[str]]]] = []
UserData = TypedDict(
"UserData",
{
"id": int,
"flags": List[str],
"mentioned_user_group_id": Optional[int],
},
)
users: List[UserData] = []
for user_id in user_list:
flags = user_flags.get(user_id, [])
user_data = dict(id=user_id, flags=flags)
user_data: UserData = dict(id=user_id, flags=flags, mentioned_user_group_id=None)
if user_id in send_request.mentioned_user_groups_map:
user_data["mentioned_user_group_id"] = send_request.mentioned_user_groups_map[

View File

@ -269,7 +269,7 @@ class Command(BaseCommand):
"data set for the backend tests.",
)
def handle(self, **options: Any) -> None:
def handle(self, *args: Any, **options: Any) -> None:
# Suppress spammy output from the push notifications logger
push_notifications_logger.disabled = True