From 2cf85d9b584129e59be39cd73e5b383a7ad84465 Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Sun, 29 Mar 2026 14:37:28 -0700 Subject: [PATCH] chore: make whoosh imports lazy to unblock test collection during Tantivy migration Module-level whoosh imports in tasks.py and paperless/views.py prevented test collection after removing whoosh-reloaded. Move to lazy imports inside the functions that use them; will be removed entirely in Task 14. Co-Authored-By: Claude Sonnet 4.6 --- src/documents/tasks.py | 16 ++++++++++++++-- src/paperless/views.py | 3 ++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/documents/tasks.py b/src/documents/tasks.py index adf1f016c..4a3b10b45 100644 --- a/src/documents/tasks.py +++ b/src/documents/tasks.py @@ -20,9 +20,7 @@ from django.db import transaction from django.db.models.signals import post_save from django.utils import timezone from filelock import FileLock -from whoosh.writing import AsyncWriter -from documents import index from documents import sanity_checker from documents.barcodes import BarcodePlugin from documents.bulk_download import ArchiveOnlyStrategy @@ -84,12 +82,20 @@ def _identity(iterable: Iterable[_T]) -> Iterable[_T]: @shared_task def index_optimize() -> None: + from whoosh.writing import AsyncWriter + + from documents import index + ix = index.open_index() writer = AsyncWriter(ix) writer.commit(optimize=True) def index_reindex(*, iter_wrapper: IterWrapper[Document] = _identity) -> None: + from whoosh.writing import AsyncWriter + + from documents import index + documents = Document.objects.all() ix = index.open_index(recreate=True) @@ -270,6 +276,10 @@ def sanity_check(*, scheduled=True, raise_on_error=True): @shared_task def bulk_update_documents(document_ids) -> None: + from whoosh.writing import AsyncWriter + + from documents import index + documents = Document.objects.filter(id__in=document_ids) ix = index.open_index() @@ -300,6 +310,8 @@ def update_document_content_maybe_archive_file(document_id) -> None: Re-creates OCR content and thumbnail for a document, and archive file if it exists. """ + from documents import index + document = Document.objects.get(id=document_id) mime_type = document.mime_type diff --git a/src/paperless/views.py b/src/paperless/views.py index dd41228a7..6c201d1e4 100644 --- a/src/paperless/views.py +++ b/src/paperless/views.py @@ -35,7 +35,6 @@ from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from rest_framework.viewsets import ModelViewSet -from documents.index import DelayedQuery from documents.permissions import PaperlessObjectPermissions from documents.tasks import llmindex_index from paperless.filters import GroupFilterSet @@ -72,6 +71,8 @@ class StandardPagination(PageNumberPagination): ) def get_all_result_ids(self): + from documents.index import DelayedQuery # removed with Whoosh in Task 14 + query = self.page.paginator.object_list if isinstance(query, DelayedQuery): try: