From 8d4163bef316caadc39ccd797887ccf2ec59a4c6 Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Fri, 13 Mar 2026 12:31:17 -0700 Subject: [PATCH] Refactor: fix type errors in remote parser and signals MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - remote.py: add `if TYPE_CHECKING: assert` guards before the Azure client construction to narrow config.endpoint and config.api_key from str|None to str. The narrowing is safe: engine_is_valid() guarantees both are non-None when it returns True (api_key explicitly; endpoint via `not (engine=="azureai" and endpoint is None)` for the only valid engine). Asserts are wrapped in TYPE_CHECKING so they carry zero runtime cost. - signals.py: add full type annotations — return types, Any-typed sender parameter, and explicit logging_group argument replacing *args. Add `from __future__ import annotations` for consistent annotation style. Co-Authored-By: Claude Sonnet 4.6 --- src/paperless/parsers/remote.py | 7 +++++++ src/paperless_remote/signals.py | 13 +++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/paperless/parsers/remote.py b/src/paperless/parsers/remote.py index 074688cb2..45311cdbd 100644 --- a/src/paperless/parsers/remote.py +++ b/src/paperless/parsers/remote.py @@ -377,6 +377,13 @@ class RemoteDocumentParser: str | None Extracted text, or None if the Azure call failed. """ + if TYPE_CHECKING: + # Callers must have already validated config via engine_is_valid(): + # engine_is_valid() asserts api_key is not None and (for azureai) + # endpoint is not None, so these casts are provably safe. + assert config.endpoint is not None + assert config.api_key is not None + from azure.ai.documentintelligence import DocumentIntelligenceClient from azure.ai.documentintelligence.models import AnalyzeDocumentRequest from azure.ai.documentintelligence.models import AnalyzeOutputOption diff --git a/src/paperless_remote/signals.py b/src/paperless_remote/signals.py index a5de1c3f2..4059126ca 100644 --- a/src/paperless_remote/signals.py +++ b/src/paperless_remote/signals.py @@ -1,10 +1,15 @@ -def get_parser(*args, **kwargs): +from __future__ import annotations + +from typing import Any + + +def get_parser(logging_group: object = None) -> Any: from paperless.parsers.remote import RemoteDocumentParser - return RemoteDocumentParser(*args, **kwargs) + return RemoteDocumentParser(logging_group) -def get_supported_mime_types(): +def get_supported_mime_types() -> dict[str, str]: from django.conf import settings from paperless.parsers.remote import RemoteDocumentParser @@ -20,7 +25,7 @@ def get_supported_mime_types(): return RemoteDocumentParser.supported_mime_types() -def remote_consumer_declaration(sender, **kwargs): +def remote_consumer_declaration(sender: Any, **kwargs: Any) -> dict[str, Any]: return { "parser": get_parser, "weight": 5,