From 72aeaf8d520af286bd69270af48ece29a45a328b Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 17 Apr 2024 14:26:04 -0700 Subject: [PATCH] db: Split reset_queries into a new module zerver.lib.db_connections. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes an import cycle that breaks mypy inference with django-stubs: zproject.settings → zproject.computed_settings → zerver.lib.db → django.db → django.db.backends.base.base → django.db.backends.base.features → django.db.models.base → django.db.models.options → django.contrib.contenttypes.fields → django.contrib.contenttypes.models → confirmation.models → django.conf → zproject.settings Signed-off-by: Anders Kaseorg --- zerver/lib/db.py | 8 -------- zerver/lib/db_connections.py | 7 +++++++ zerver/middleware.py | 2 +- zerver/worker/base.py | 2 +- zerver/worker/missedmessage_emails.py | 2 +- 5 files changed, 10 insertions(+), 11 deletions(-) create mode 100644 zerver/lib/db_connections.py diff --git a/zerver/lib/db.py b/zerver/lib/db.py index df547dba3d..9d3fedead5 100644 --- a/zerver/lib/db.py +++ b/zerver/lib/db.py @@ -51,11 +51,3 @@ class TimeTrackingConnection(connection): def __init__(self, *args: Any, **kwargs: Any) -> None: self.queries: List[Dict[str, str]] = [] super().__init__(*args, **kwargs) - - -def reset_queries() -> None: - from django.db import connections - - for conn in connections.all(): - if conn.connection is not None: - conn.connection.queries = [] diff --git a/zerver/lib/db_connections.py b/zerver/lib/db_connections.py new file mode 100644 index 0000000000..af60299ce6 --- /dev/null +++ b/zerver/lib/db_connections.py @@ -0,0 +1,7 @@ +from django.db import connections + + +def reset_queries() -> None: + for conn in connections.all(): + if conn.connection is not None: + conn.connection.queries = [] diff --git a/zerver/middleware.py b/zerver/middleware.py index 1ecee11652..ec75a91fdb 100644 --- a/zerver/middleware.py +++ b/zerver/middleware.py @@ -25,7 +25,7 @@ from sentry_sdk import set_tag from typing_extensions import Annotated, Concatenate, ParamSpec, override from zerver.lib.cache import get_remote_cache_requests, get_remote_cache_time -from zerver.lib.db import reset_queries +from zerver.lib.db_connections import reset_queries from zerver.lib.debug import maybe_tracemalloc_listen from zerver.lib.exceptions import ErrorCode, JsonableError, MissingAuthenticationError, WebhookError from zerver.lib.markdown import get_markdown_requests, get_markdown_time diff --git a/zerver/worker/base.py b/zerver/worker/base.py index 8b99f0a695..8430b974a9 100644 --- a/zerver/worker/base.py +++ b/zerver/worker/base.py @@ -16,7 +16,7 @@ from returns.curry import partial from typing_extensions import override from zerver.lib.context_managers import lockfile -from zerver.lib.db import reset_queries +from zerver.lib.db_connections import reset_queries from zerver.lib.per_request_cache import flush_per_request_caches from zerver.lib.pysa import mark_sanitized from zerver.lib.queue import SimpleQueueClient diff --git a/zerver/worker/missedmessage_emails.py b/zerver/worker/missedmessage_emails.py index 9ad011d6a7..ccf734d5b4 100644 --- a/zerver/worker/missedmessage_emails.py +++ b/zerver/worker/missedmessage_emails.py @@ -11,7 +11,7 @@ from django.db.utils import IntegrityError from django.utils.timezone import now as timezone_now from typing_extensions import override -from zerver.lib.db import reset_queries +from zerver.lib.db_connections import reset_queries from zerver.lib.email_notifications import MissedMessageData, handle_missedmessage_emails from zerver.lib.per_request_cache import flush_per_request_caches from zerver.models import ScheduledMessageNotificationEmail