From acfdcff80f51ecaf93cfe0697f0b0e360f647836 Mon Sep 17 00:00:00 2001 From: PIG208 <359101898@qq.com> Date: Mon, 26 Jul 2021 23:32:10 +0800 Subject: [PATCH] typing: Amend inaccurate type annotations. This amend some type annotations that turn out to be inaccurate with django-stubs. --- analytics/urls.py | 5 ++++- analytics/views/activity_common.py | 4 ++-- analytics/views/installation_activity.py | 2 +- analytics/views/realm_activity.py | 2 +- zerver/decorator.py | 6 ++++-- zerver/lib/actions.py | 2 +- zilencer/management/commands/populate_db.py | 2 +- 7 files changed, 14 insertions(+), 9 deletions(-) diff --git a/analytics/urls.py b/analytics/urls.py index affedf048b7daf..32ea0366fabf1b 100644 --- a/analytics/urls.py +++ b/analytics/urls.py @@ -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.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"), diff --git a/analytics/views/activity_common.py b/analytics/views/activity_common.py index d752b01dddf9f5..4b11d2f32939d0 100644 --- a/analytics/views/activity_common.py +++ b/analytics/views/activity_common.py @@ -5,7 +5,7 @@ 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 fix_row(row: Any) -> Dict[str, Any]: 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()] diff --git a/analytics/views/installation_activity.py b/analytics/views/installation_activity.py index 39dc6436a2a938..5de2abf3866ace 100644 --- a/analytics/views/installation_activity.py +++ b/analytics/views/installation_activity.py @@ -369,7 +369,7 @@ def get_page( 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]) diff --git a/analytics/views/realm_activity.py b/analytics/views/realm_activity.py index e1f53bc6463af1..d229a294d11958 100644 --- a/analytics/views/realm_activity.py +++ b/analytics/views/realm_activity.py @@ -62,7 +62,7 @@ def get_count(user_summary: Dict[str, Dict[str, str]], k: str) -> str: 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 diff --git a/zerver/decorator.py b/zerver/decorator.py index 2407629a27900f..31d21c38c6b2c9 100644 --- a/zerver/decorator.py +++ b/zerver/decorator.py @@ -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) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index b4cab794b8eb5c..eed095f1618afb 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -1986,7 +1986,7 @@ def do_send_messages( else: user_list = list(user_ids) - users: List[Dict[str, Union[int, List[str]]]] = [] + users: List[Dict[str, object]] = [] for user_id in user_list: flags = user_flags.get(user_id, []) user_data = dict(id=user_id, flags=flags) diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.py index 164de256da52fa..c4df5d2ca7889b 100644 --- a/zilencer/management/commands/populate_db.py +++ b/zilencer/management/commands/populate_db.py @@ -273,7 +273,7 @@ def add_arguments(self, parser: CommandParser) -> None: "data set for the backend tests.", ) - def handle(self, **options: Any) -> None: + def handle(self, *args: Any, **options: Any) -> None: if options["percent_huddles"] + options["percent_personals"] > 100: self.stderr.write("Error! More than 100% of messages allocated.\n") return