From 4dbc5e1dbaa848c8a3ca7ed54455c98cc8688001 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Sun, 15 Oct 2023 22:48:02 -0700 Subject: [PATCH] Fix some typing issues --- src/ocrmypdf/_pipelines/_common.py | 4 +++- src/ocrmypdf/_plugin_manager.py | 4 ++-- src/ocrmypdf/api.py | 6 +++--- src/ocrmypdf/helpers.py | 6 +++++- src/ocrmypdf/pdfinfo/info.py | 3 ++- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/ocrmypdf/_pipelines/_common.py b/src/ocrmypdf/_pipelines/_common.py index b27c6fa4..83fcf89f 100644 --- a/src/ocrmypdf/_pipelines/_common.py +++ b/src/ocrmypdf/_pipelines/_common.py @@ -57,9 +57,11 @@ log = logging.getLogger(__name__) tls = threading.local() tls.pageno = None + def _set_logging_tls(tls): """Inject current page number (when available) into log records.""" old_factory = logging.getLogRecordFactory() + def wrapper(*args, **kwargs): record = old_factory(*args, **kwargs) if hasattr(tls, 'pageno'): @@ -72,7 +74,7 @@ def _set_logging_tls(tls): _set_logging_tls(tls) -def set_thread_pageno(pageno:int): +def set_thread_pageno(pageno: int | None): """Set page number (1-based) that the current thread is processing.""" tls.pageno = pageno diff --git a/src/ocrmypdf/_plugin_manager.py b/src/ocrmypdf/_plugin_manager.py index bee6b328..e380e7bb 100644 --- a/src/ocrmypdf/_plugin_manager.py +++ b/src/ocrmypdf/_plugin_manager.py @@ -33,7 +33,7 @@ class OcrmypdfPluginManager(pluggy.PluginManager): def __init__( self, *args, - plugins: list[str | Path], + plugins: Sequence[str | Path], builtins: bool = True, **kwargs, ): @@ -101,7 +101,7 @@ class OcrmypdfPluginManager(pluggy.PluginManager): def get_plugin_manager( - plugins: list[str | Path] | None = None, builtins=True + plugins: Sequence[str | Path] | None = None, builtins=True ) -> OcrmypdfPluginManager: return OcrmypdfPluginManager( project_name='ocrmypdf', diff --git a/src/ocrmypdf/api.py b/src/ocrmypdf/api.py index ba3ac225..eb5096b0 100644 --- a/src/ocrmypdf/api.py +++ b/src/ocrmypdf/api.py @@ -10,7 +10,7 @@ import os import sys import threading from argparse import Namespace -from collections.abc import Iterable +from collections.abc import Iterable, Sequence from enum import IntEnum from io import IOBase from pathlib import Path @@ -414,7 +414,7 @@ def pdf_to_hocr( # noqa: D417 continue_on_soft_render_error: bool | None = None, invalidate_digital_signatures: bool | None = None, plugin_manager=None, - plugins: Iterable[StrPath] | None = None, + plugins: Sequence[StrPath] | None = None, keep_temporary_files: bool | None = None, **kwargs, ): @@ -483,7 +483,7 @@ def hocr_to_ocr_pdf( # noqa: D417 color_conversion_strategy: str | None = None, fast_web_view: float | None = None, plugin_manager=None, - plugins: Iterable[StrPath] | None = None, + plugins: Sequence[StrPath] | None = None, **kwargs, ): """Run OCRmyPDF on a work folder and produce an output PDF. diff --git a/src/ocrmypdf/helpers.py b/src/ocrmypdf/helpers.py index 69085fd3..131ebcd7 100644 --- a/src/ocrmypdf/helpers.py +++ b/src/ocrmypdf/helpers.py @@ -324,7 +324,11 @@ def pikepdf_enable_mmap() -> None: pikepdf._core.set_access_default_mmap(True) log.debug( "pikepdf mmap " - + ('enabled' if pikepdf._core.get_access_default_mmap() else 'disabled') + + ( + 'enabled' + if pikepdf._core.get_access_default_mmap() # type: ignore[attr-defined] + else 'disabled' + ) ) except AttributeError: log.debug("pikepdf mmap not available") diff --git a/src/ocrmypdf/pdfinfo/info.py b/src/ocrmypdf/pdfinfo/info.py index 73620801..05320dce 100644 --- a/src/ocrmypdf/pdfinfo/info.py +++ b/src/ocrmypdf/pdfinfo/info.py @@ -32,6 +32,7 @@ from pikepdf import ( PdfImage, PdfInlineImage, PdfMatrix, + Stream, UnsupportedImageTypeError, parse_content_stream, ) @@ -357,7 +358,7 @@ class ImageInfo: if inline is not None: self._origin = 'inline' pim = inline - elif pdfimage is not None: + elif pdfimage is not None and isinstance(pdfimage, Stream): self._origin = 'xobject' pim = PdfImage(pdfimage) else: