diff --git a/app/tasks/process_document.py b/app/tasks/process_document.py index 3d51a400..9b4a3810 100644 --- a/app/tasks/process_document.py +++ b/app/tasks/process_document.py @@ -1,11 +1,14 @@ #!/usr/bin/env python3 +from __future__ import annotations + import json import logging import mimetypes import os import shutil import uuid +from typing import TYPE_CHECKING import pypdf # Upgraded from PyPDF2 to fix CVE-2023-36464 from pypdf.errors import PdfReadError @@ -21,10 +24,13 @@ from app.utils import get_unique_filepath_with_counter, hash_file, log_task_prog from app.utils.step_manager import initialize_file_steps from app.utils.text_quality import check_text_quality, detect_pdf_text_source +if TYPE_CHECKING: + from sqlalchemy.orm import Session + logger = logging.getLogger(__name__) -def _get_pipeline_ocr_language(db, file_record: FileRecord, owner_id: str | None) -> str | None: +def _get_pipeline_ocr_language(db: "Session", file_record: FileRecord, owner_id: str | None) -> str | None: """Look up the OCR language override from the file's pipeline OCR step config. Resolution order: @@ -80,7 +86,7 @@ def _get_pipeline_ocr_language(db, file_record: FileRecord, owner_id: str | None try: step_config = json.loads(ocr_step.config) - lang = step_config.get("ocr_language") or None + lang = step_config.get("ocr_language") # "auto" is treated as no override return lang if lang and lang != "auto" else None except Exception: diff --git a/app/tasks/process_with_ocr.py b/app/tasks/process_with_ocr.py index 8447ec1f..8503c983 100644 --- a/app/tasks/process_with_ocr.py +++ b/app/tasks/process_with_ocr.py @@ -17,7 +17,6 @@ task with a multi-engine OCR pipeline that: import logging import os -from typing import Optional from app.celery_app import celery from app.config import settings @@ -36,9 +35,9 @@ logger = logging.getLogger(__name__) def process_with_ocr( self, filename: str, - file_id: Optional[int] = None, - original_text: Optional[str] = None, - language: Optional[str] = None, + file_id: int | None = None, + original_text: str | None = None, + language: str | None = None, ): """Run the configured OCR providers on *filename* and continue the pipeline. diff --git a/tests/test_process_document.py b/tests/test_process_document.py index 2eb9f144..53a00b3b 100644 --- a/tests/test_process_document.py +++ b/tests/test_process_document.py @@ -977,13 +977,15 @@ def test_get_pipeline_ocr_language_explicit_pipeline_takes_priority(db_session): db_session.add(sys_pipeline) db_session.commit() - PipelineStep( + sys_step = PipelineStep( pipeline_id=sys_pipeline.id, position=0, step_type="ocr", config=json.dumps({"ocr_language": "eng"}), enabled=True, ) + db_session.add(sys_step) + db_session.commit() # Explicit pipeline with "fra" explicit_pipeline = Pipeline(