Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
python: Replace functools.partial with type-safe returns.curry.partial.
Anders Kaseorgcf4791264c30
diff --git a/pyproject.toml b/pyproject.tomlindex f92928d1e4f..cd7ba7f33fd 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -42,7 +42,11 @@ warn_unreachable = true# with this behavior.local_partial_types = true-plugins = ["mypy_django_plugin.main", "pydantic.mypy"]+plugins = [+ "mypy_django_plugin.main",+ "pydantic.mypy",+ "returns.contrib.mypy.returns_plugin",+][[tool.mypy.overrides]]module = [diff --git a/requirements/common.in b/requirements/common.inindex 965018828ea..aac8f0ab156 100644--- a/requirements/common.in+++ b/requirements/common.in@@ -8,6 +8,9 @@ Django[argon2]==4.2.*# needed for NotRequired, ParamSpectyping-extensions+# For type-safe returns.curry.partial+returns+# Needed for rendering backend templatesJinja2diff --git a/requirements/dev.txt b/requirements/dev.txtindex 245646bb9ac..55785cdad09 100644--- a/requirements/dev.txt+++ b/requirements/dev.txt@@ -2442,6 +2442,10 @@ responses==0.23.3 \# via# -r requirements/dev.in# moto+returns==0.22.0 \+ --hash=sha256:c7bd85bd1e0041b44fe46c7e2f68fcc76a0546142c876229e395174bcd674f37 \+ --hash=sha256:d38d6324692eeb29ec4bd698e1b859ec0ac79fb2c17bf0d302f92c8c42ef35c1+ # via -r requirements/common.inrfc3339-validator==0.1.4 \--hash=sha256:138a2abdf93304ad60530167e51d2dfb9549521a836871b88d7f4695d0022f6b \--hash=sha256:24f6ec1eda14ef823da9e36ec7113124b39c04d50a4d3d3a3c2859577e7791fa@@ -3059,6 +3063,7 @@ typing-extensions==4.7.1 \# pyre-check# pyre-extensions# qrcode+ # returns# rich# semgrep# sqlalchemy2-stubsdiff --git a/requirements/prod.txt b/requirements/prod.txtindex ecd2f46af22..f8b3bba02c9 100644--- a/requirements/prod.txt+++ b/requirements/prod.txt@@ -1921,6 +1921,10 @@ requests-oauthlib==1.3.1 \# -r requirements/common.in# python-twitter# social-auth-core+returns==0.22.0 \+ --hash=sha256:c7bd85bd1e0041b44fe46c7e2f68fcc76a0546142c876229e395174bcd674f37 \+ --hash=sha256:d38d6324692eeb29ec4bd698e1b859ec0ac79fb2c17bf0d302f92c8c42ef35c1+ # via -r requirements/common.inrfc3339-validator==0.1.4 \--hash=sha256:138a2abdf93304ad60530167e51d2dfb9549521a836871b88d7f4695d0022f6b \--hash=sha256:24f6ec1eda14ef823da9e36ec7113124b39c04d50a4d3d3a3c2859577e7791fa@@ -2173,6 +2177,7 @@ typing-extensions==4.7.1 \# pydantic# pydantic-core# qrcode+ # returns# stripe# zulip# zulip-botsdiff --git a/tools/semgrep.yml b/tools/semgrep.ymlindex d1ee2c62858..126d468fbc0 100644--- a/tools/semgrep.yml+++ b/tools/semgrep.yml@@ -250,3 +250,9 @@ rules:message: 'Use ".exists()" instead; it is more efficient'languages: [python]severity: ERROR++ - id: functools-partial+ pattern: functools.partial+ message: "Replace functools.partial with returns.curry.partial for type safety"+ languages: [python]+ severity: ERRORdiff --git a/version.py b/version.pyindex 45e17fbe5dd..e10ee464335 100644--- a/version.py+++ b/version.py@@ -48,4 +48,4 @@ API_FEATURE_LEVEL = 209# historical commits sharing the same major version, in which case a# minor version bump suffices.-PROVISION_VERSION = (249, 0)+PROVISION_VERSION = (249, 1)diff --git a/zerver/data_import/import_util.py b/zerver/data_import/import_util.pyindex e64cbeff758..7e49fb5585f 100644--- a/zerver/data_import/import_util.py+++ b/zerver/data_import/import_util.py@@ -4,7 +4,6 @@ import randomimport shutilfrom collections import defaultdictfrom concurrent.futures import ProcessPoolExecutor, as_completed-from functools import partialfrom typing import (AbstractSet,Any,@@ -25,6 +24,7 @@ import orjsonimport requestsfrom django.forms.models import model_to_dictfrom django.utils.timezone import now as timezone_now+from returns.curry import partialfrom typing_extensions import TypeAliasfrom zerver.data_import.sequencer import NEXT_IDdiff --git a/zerver/lib/test_runner.py b/zerver/lib/test_runner.pyindex 64e43a5b22c..85f5cc27712 100644--- a/zerver/lib/test_runner.py+++ b/zerver/lib/test_runner.py@@ -3,7 +3,6 @@ import osimport randomimport shutilimport unittest-from functools import partialfrom typing import Any, Callable, Dict, Iterable, List, Optional, Set, Tuple, Type, Unionfrom unittest import TestSuite, runnerfrom unittest.result import TestResult@@ -14,6 +13,7 @@ from django.db import ProgrammingError, connectionsfrom django.test import runner as django_runnerfrom django.test.runner import DiscoverRunnerfrom django.test.signals import template_rendered+from returns.curry import partialfrom typing_extensions import TypeAliasfrom scripts.lib.zulip_tools import (diff --git a/zerver/views/video_calls.py b/zerver/views/video_calls.pyindex 4d266817945..d2b08ecfa0e 100644--- a/zerver/views/video_calls.py+++ b/zerver/views/video_calls.py@@ -3,7 +3,6 @@ import jsonimport randomimport secretsfrom base64 import b32encode-from functools import partialfrom typing import Dictfrom urllib.parse import quote, urlencode, urljoin@@ -21,6 +20,7 @@ from django.views.decorators.csrf import csrf_exemptfrom django.views.decorators.http import require_POSTfrom oauthlib.oauth2 import OAuth2Errorfrom requests_oauthlib import OAuth2Session+from returns.curry import partialfrom zerver.actions.video_calls import do_set_zoom_tokenfrom zerver.decorator import zulip_login_requireddiff --git a/zerver/webhooks/bitbucket2/view.py b/zerver/webhooks/bitbucket2/view.pyindex 392b943fbe9..7135bc0991a 100644--- a/zerver/webhooks/bitbucket2/view.py+++ b/zerver/webhooks/bitbucket2/view.py@@ -1,10 +1,10 @@# Webhooks for external integrations.import reimport string-from functools import partialfrom typing import Dict, List, Optional, Protocolfrom django.http import HttpRequest, HttpResponse+from returns.curry import partialfrom zerver.decorator import log_unsupported_webhook_event, webhook_viewfrom zerver.lib.exceptions import UnsupportedWebhookEventTypeErrordiff --git a/zerver/webhooks/bitbucket3/view.py b/zerver/webhooks/bitbucket3/view.pyindex 2e6e739f66f..bc66049430c 100644--- a/zerver/webhooks/bitbucket3/view.py+++ b/zerver/webhooks/bitbucket3/view.py@@ -1,8 +1,8 @@import string-from functools import partialfrom typing import Dict, List, Optional, Protocolfrom django.http import HttpRequest, HttpResponse+from returns.curry import partialfrom zerver.decorator import webhook_viewfrom zerver.lib.exceptions import UnsupportedWebhookEventTypeErrordiff --git a/zerver/webhooks/clubhouse/view.py b/zerver/webhooks/clubhouse/view.pyindex 22856a46f63..e14242d7f07 100644--- a/zerver/webhooks/clubhouse/view.py+++ b/zerver/webhooks/clubhouse/view.py@@ -1,7 +1,7 @@-from functools import partialfrom typing import Callable, Dict, Iterable, Iterator, List, Optionalfrom django.http import HttpRequest, HttpResponse+from returns.curry import partialfrom zerver.decorator import webhook_viewfrom zerver.lib.exceptions import UnsupportedWebhookEventTypeErrordiff --git a/zerver/webhooks/github/view.py b/zerver/webhooks/github/view.pyindex 10f25039563..7ced36228ac 100644--- a/zerver/webhooks/github/view.py+++ b/zerver/webhooks/github/view.py@@ -1,8 +1,8 @@import re-from functools import partialfrom typing import Callable, Dict, Optionalfrom django.http import HttpRequest, HttpResponse+from returns.curry import partialfrom zerver.decorator import log_unsupported_webhook_event, webhook_viewfrom zerver.lib.exceptions import UnsupportedWebhookEventTypeErrordiff --git a/zerver/webhooks/gitlab/view.py b/zerver/webhooks/gitlab/view.pyindex cde6ac75a08..750fe99a0d4 100644--- a/zerver/webhooks/gitlab/view.py+++ b/zerver/webhooks/gitlab/view.py@@ -1,9 +1,9 @@import re-from functools import partialfrom typing import Dict, List, Optional, Protocol, Unionfrom django.http import HttpRequest, HttpResponsefrom pydantic import Json+from returns.curry import partialfrom zerver.decorator import webhook_viewfrom zerver.lib.exceptions import UnsupportedWebhookEventTypeErrordiff --git a/zerver/webhooks/groove/view.py b/zerver/webhooks/groove/view.pyindex 742c7c9abdf..de6b6ca14ec 100644--- a/zerver/webhooks/groove/view.py+++ b/zerver/webhooks/groove/view.py@@ -1,8 +1,8 @@# Webhooks for external integrations.-from functools import partialfrom typing import Callable, Dict, Optionalfrom django.http import HttpRequest, HttpResponse+from returns.curry import partialfrom zerver.decorator import webhook_viewfrom zerver.lib.exceptions import UnsupportedWebhookEventTypeErrordiff --git a/zerver/webhooks/intercom/view.py b/zerver/webhooks/intercom/view.pyindex 8e0526ce14d..2c2550999e3 100644--- a/zerver/webhooks/intercom/view.py+++ b/zerver/webhooks/intercom/view.py@@ -1,8 +1,8 @@-from functools import partialfrom html.parser import HTMLParserfrom typing import Callable, Dict, List, Tuplefrom django.http import HttpRequest, HttpResponse+from returns.curry import partialfrom zerver.decorator import return_success_on_head_request, webhook_viewfrom zerver.lib.exceptions import UnsupportedWebhookEventTypeErrordiff --git a/zerver/worker/queue_processors.py b/zerver/worker/queue_processors.pyindex 170541b50f4..58adf217f3e 100644--- a/zerver/worker/queue_processors.py+++ b/zerver/worker/queue_processors.py@@ -4,7 +4,6 @@ import copyimport datetimeimport emailimport email.policy-import functoolsimport loggingimport osimport signal@@ -43,6 +42,7 @@ from django.db.utils import IntegrityErrorfrom django.utils.timezone import now as timezone_nowfrom django.utils.translation import gettext as _from django.utils.translation import override as override_language+from returns.curry import partialfrom sentry_sdk import add_breadcrumb, configure_scopefrom zulip_bots.lib import extract_query_without_mention@@ -309,7 +309,7 @@ class QueueProcessingWorker(ABC):try:signal.signal(signal.SIGALRM,- functools.partial(self.timer_expired, self.MAX_CONSUME_SECONDS, events),+ partial(self.timer_expired, self.MAX_CONSUME_SECONDS, events),)try:signal.alarm(self.MAX_CONSUME_SECONDS * len(events))@@ -357,7 +357,7 @@ class QueueProcessingWorker(ABC):self.do_consume(consume_func, [event])def timer_expired(- self, limit: int, events: List[Dict[str, Any]], signal: int, frame: FrameType+ self, limit: int, events: List[Dict[str, Any]], signal: int, frame: Optional[FrameType]) -> None:raise WorkerTimeoutError(self.queue_name, limit, len(events))@@ -911,7 +911,7 @@ class FetchLinksEmbedData(QueueProcessingWorker):do_update_embedded_data(message.sender, message, message.content, rendering_result)def timer_expired(- self, limit: int, events: List[Dict[str, Any]], signal: int, frame: FrameType+ self, limit: int, events: List[Dict[str, Any]], signal: int, frame: Optional[FrameType]) -> None:assert len(events) == 1event = events[0]