diff --git a/src/ocrmypdf/_pipelines/_common.py b/src/ocrmypdf/_pipelines/_common.py index 0044d0a4..b3b88500 100644 --- a/src/ocrmypdf/_pipelines/_common.py +++ b/src/ocrmypdf/_pipelines/_common.py @@ -186,7 +186,7 @@ def configure_debug_logging( return log_file_handler, remover -def worker_init(max_pixels: int) -> None: +def worker_init(max_pixels: int | None) -> None: """Initialize a worker thread or process.""" # In Windows, child process will not inherit our change to this value in # the parent process, so ensure workers get it set. Not needed when running diff --git a/src/ocrmypdf/api.py b/src/ocrmypdf/api.py index 69de7fbd..7fc412ad 100644 --- a/src/ocrmypdf/api.py +++ b/src/ocrmypdf/api.py @@ -14,7 +14,7 @@ from collections.abc import Iterable, Sequence from enum import IntEnum from io import IOBase from pathlib import Path -from typing import AnyStr, BinaryIO +from typing import BinaryIO from warnings import warn import pluggy @@ -28,7 +28,7 @@ from ocrmypdf._validation import check_options from ocrmypdf.cli import ArgumentParser, get_parser from ocrmypdf.helpers import is_iterable_notstr -StrPath = Path | AnyStr +StrPath = Path | str | bytes PathOrIO = BinaryIO | StrPath # Installing plugins affects the global state of the Python interpreter, @@ -140,9 +140,9 @@ def configure_logging( def _kwargs_to_cmdline( *, defer_kwargs: set[str], **kwargs -) -> tuple[list[str], dict[str, AnyStr]]: +) -> tuple[list[str | bytes], dict[str, str | bytes]]: """Convert kwargs to command line arguments.""" - cmdline = [] + cmdline: list[str | bytes] = [] deferred = {} for arg, val in kwargs.items(): if val is None: @@ -279,7 +279,7 @@ def ocr( # noqa: D417 fast_web_view: float | None = None, continue_on_soft_render_error: bool | None = None, invalidate_digital_signatures: bool | None = None, - plugins: Iterable[StrPath] | None = None, + plugins: Iterable[Path | str] | None = None, plugin_manager=None, keep_temporary_files: bool | None = None, progress_bar: bool | None = None, @@ -420,7 +420,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: Sequence[StrPath] | None = None, + plugins: Sequence[Path | str] | None = None, keep_temporary_files: bool | None = None, **kwargs, ): @@ -491,7 +491,7 @@ def _hocr_to_ocr_pdf( # noqa: D417 color_conversion_strategy: str | None = None, fast_web_view: float | None = None, plugin_manager=None, - plugins: Sequence[StrPath] | None = None, + plugins: Sequence[Path | str] | None = None, **kwargs, ): """Run OCRmyPDF on a work folder and produce an output PDF. diff --git a/src/ocrmypdf/hocrtransform/_hocr.py b/src/ocrmypdf/hocrtransform/_hocr.py index 47a1952b..fd4dfe9b 100644 --- a/src/ocrmypdf/hocrtransform/_hocr.py +++ b/src/ocrmypdf/hocrtransform/_hocr.py @@ -357,7 +357,7 @@ class HocrTransform: line_matrix: Matrix, text: Text, fontsize: float, - elem: Element, + elem: Element | None, next_elem: Element | None, text_direction: TextDirection, inject_word_breaks: bool, @@ -434,7 +434,7 @@ class HocrTransform: if ocr_par is None: continue canvas.do.rect( - ocr_par.llx, ocr_par.lly, ocr_par.width, ocr_par.height, fill=0 + ocr_par.llx, ocr_par.lly, ocr_par.width, ocr_par.height, fill=False ) def _debug_draw_line_bbox(self, canvas: Canvas, line_box: Rectangle, color=BLUE): @@ -443,7 +443,7 @@ class HocrTransform: return with canvas.do.save_state(): canvas.do.stroke_color(color).line_width(0.15).rect( - line_box.llx, line_box.lly, line_box.width, line_box.height, fill=0 + line_box.llx, line_box.lly, line_box.width, line_box.height, fill=False ) def _debug_draw_word_triangle( @@ -467,7 +467,7 @@ class HocrTransform: return with canvas.do.save_state(): canvas.do.stroke_color(color).line_width(line_width).rect( - box.llx, box.lly, box.width, box.height, fill=0 + box.llx, box.lly, box.width, box.height, fill=False ) def _debug_draw_space_bbox( @@ -478,7 +478,7 @@ class HocrTransform: return with canvas.do.save_state(): canvas.do.fill_color(color).line_width(line_width).rect( - box.llx, box.lly, box.width, box.height, fill=1 + box.llx, box.lly, box.width, box.height, fill=True ) def _debug_draw_baseline( diff --git a/src/ocrmypdf/optimize.py b/src/ocrmypdf/optimize.py index 43506dc6..c2ea7bf5 100644 --- a/src/ocrmypdf/optimize.py +++ b/src/ocrmypdf/optimize.py @@ -75,10 +75,15 @@ def extract_image_filter( """Determine if an image is extractable.""" if image.Subtype != Name.Image: return None - if image.Length < 100: + if not isinstance(image.Length, int) or image.Length < 100: log.debug(f"xref {xref}: skipping image with small stream size") return None - if image.Width < 8 or image.Height < 8: # Issue 732 + if ( + not isinstance(image.Width, int) + or not isinstance(image.Height, int) + or image.Width < 8 + or image.Height < 8 + ): # Issue 732 log.debug(f"xref {xref}: skipping image with unusually small dimensions") return None