diff --git a/src/ocrmypdf/_pipeline.py b/src/ocrmypdf/_pipeline.py index c0333704..80d39feb 100644 --- a/src/ocrmypdf/_pipeline.py +++ b/src/ocrmypdf/_pipeline.py @@ -14,7 +14,7 @@ from contextlib import suppress from datetime import datetime, timezone from pathlib import Path from shutil import copyfileobj -from typing import Any, Iterable, Sequence +from typing import Any, BinaryIO, Iterable, Sequence, cast import img2pdf import pikepdf @@ -575,7 +575,9 @@ def ocr_engine_hocr(input_file: Path, page_context: PageContext) -> tuple[Path, def should_visible_page_image_use_jpg(pageinfo: PageInfo) -> bool: # If all images were JPEGs originally, produce a JPEG as output - return pageinfo.images and all(im.enc == Encoding.jpeg for im in pageinfo.images) + return bool(pageinfo.images) and all( + im.enc == Encoding.jpeg for im in pageinfo.images + ) def create_visible_page_jpg(image: Path, page_context: PageContext) -> Path: @@ -892,14 +894,16 @@ def merge_sidecars(txt_files: Iterable[Path | None], context: PdfContext) -> Pat return output_file -def copy_final(input_file, output_file, _context: PdfContext) -> None: +def copy_final( + input_file: Path, output_file: str | Path | BinaryIO, _context: PdfContext +) -> None: log.debug('%s -> %s', input_file, output_file) - with open(input_file, 'rb') as input_stream: + with input_file.open('rb') as input_stream: if output_file == '-': copyfileobj(input_stream, sys.stdout.buffer) sys.stdout.flush() elif hasattr(output_file, 'writable'): - output_stream = output_file + output_stream = cast(BinaryIO, output_file) copyfileobj(input_stream, output_stream) with suppress(AttributeError): output_stream.flush() diff --git a/src/ocrmypdf/_sync.py b/src/ocrmypdf/_sync.py index 54cb0e08..9f077966 100644 --- a/src/ocrmypdf/_sync.py +++ b/src/ocrmypdf/_sync.py @@ -418,13 +418,13 @@ def run_pipeline( options, start_input_file, options.output_file, optimize_messages ) - except (KeyboardInterrupt if not api else NeverRaise): + except KeyboardInterrupt if not api else NeverRaise: if options.verbose >= 1: log.exception("KeyboardInterrupt") else: log.error("KeyboardInterrupt") return ExitCode.ctrl_c - except (ExitCodeException if not api else NeverRaise) as e: + except ExitCodeException if not api else NeverRaise as e: e = cast(ExitCodeException, e) if options.verbose >= 1: log.exception("ExitCodeException") @@ -433,7 +433,7 @@ def run_pipeline( else: log.error(type(e).__name__) return e.exit_code - except (PIL.Image.DecompressionBombError if not api else NeverRaise): + except PIL.Image.DecompressionBombError if not api else NeverRaise: log.exception( "A decompression bomb error was encountered while executing the " "pipeline. Use the argument --max-image-mpixels to raise the maximum " @@ -451,7 +451,7 @@ def run_pipeline( "argument." ) return ExitCode.child_process_error - except (Exception if not api else NeverRaise): # pylint: disable=broad-except + except Exception if not api else NeverRaise: # pylint: disable=broad-except log.exception("An exception occurred while executing the pipeline") return ExitCode.other_error finally: diff --git a/src/ocrmypdf/api.py b/src/ocrmypdf/api.py index 8cc7ee41..a29787db 100644 --- a/src/ocrmypdf/api.py +++ b/src/ocrmypdf/api.py @@ -207,51 +207,51 @@ def ocr( # noqa: ruff: disable=D417 input_file: PathOrIO, output_file: PathOrIO, *, - language: Iterable[str] = None, - image_dpi: int = None, - output_type=None, + language: Iterable[str] | None = None, + image_dpi: int | None = None, + output_type: str | None = None, sidecar: StrPath | None = None, - jobs: int = None, - use_threads: bool = None, - title: str = None, - author: str = None, - subject: str = None, - keywords: str = None, - rotate_pages: bool = None, - remove_background: bool = None, - deskew: bool = None, - clean: bool = None, - clean_final: bool = None, - unpaper_args: str = None, - oversample: int = None, - remove_vectors: bool = None, - force_ocr: bool = None, - skip_text: bool = None, - redo_ocr: bool = None, - skip_big: float = None, - optimize: int = None, - jpg_quality: int = None, - png_quality: int = None, - jbig2_lossy: bool = None, - jbig2_page_group_size: int = None, - pages: str = None, - max_image_mpixels: float = None, - tesseract_config: Iterable[str] = None, - tesseract_pagesegmode: int = None, - tesseract_oem: int = None, - tesseract_thresholding: int = None, - pdf_renderer=None, - tesseract_timeout: float = None, - tesseract_non_ocr_timeout: float = None, - rotate_pages_threshold: float = None, - pdfa_image_compression=None, - user_words: os.PathLike = None, - user_patterns: os.PathLike = None, - fast_web_view: float = None, - plugins: Iterable[StrPath] = None, + jobs: int | None = None, + use_threads: bool | None = None, + title: str | None = None, + author: str | None = None, + subject: str | None = None, + keywords: str | None = None, + rotate_pages: bool | None = None, + remove_background: bool | None = None, + deskew: bool | None = None, + clean: bool | None = None, + clean_final: bool | None = None, + unpaper_args: str | None = None, + oversample: int | None = None, + remove_vectors: bool | None = None, + force_ocr: bool | None = None, + skip_text: bool | None = None, + redo_ocr: bool | None = None, + skip_big: float | None = None, + optimize: int | None = None, + jpg_quality: int | None = None, + png_quality: int | None = None, + jbig2_lossy: bool | None = None, + jbig2_page_group_size: int | None = None, + pages: str | None = None, + max_image_mpixels: float | None = None, + tesseract_config: Iterable[str] | None = None, + tesseract_pagesegmode: int | None = None, + tesseract_oem: int | None = None, + tesseract_thresholding: int | None = None, + pdf_renderer: str | None = None, + tesseract_timeout: float | None = None, + tesseract_non_ocr_timeout: float | None = None, + rotate_pages_threshold: float | None = None, + pdfa_image_compression: str | None = None, + user_words: os.PathLike | None = None, + user_patterns: os.PathLike | None = None, + fast_web_view: float | None = None, + plugins: Iterable[StrPath] | None = None, plugin_manager=None, - keep_temporary_files: bool = None, - progress_bar: bool = None, + keep_temporary_files: bool | None = None, + progress_bar: bool | None = None, **kwargs, ): """Run OCRmyPDF on one PDF or image. diff --git a/src/ocrmypdf/extra_plugins/semfree.py b/src/ocrmypdf/extra_plugins/semfree.py index 231f5f24..6e7dd3c2 100644 --- a/src/ocrmypdf/extra_plugins/semfree.py +++ b/src/ocrmypdf/extra_plugins/semfree.py @@ -168,8 +168,7 @@ class LambdaExecutor(Executor): continue if msg_type == MessageType.result: - if task_finished: - task_finished(msg, pbar) + task_finished(msg, pbar) elif msg_type == 'log': record = msg logger = logging.getLogger(record.name) diff --git a/src/ocrmypdf/pdfa.py b/src/ocrmypdf/pdfa.py index 199c02f1..9be3649a 100644 --- a/src/ocrmypdf/pdfa.py +++ b/src/ocrmypdf/pdfa.py @@ -23,8 +23,8 @@ def _postscript_objdef( alias: str, dictionary: dict[str, str], *, - stream_name: str = None, - stream_data: bytes = None, + stream_name: str | None = None, + stream_data: bytes | None = None, ) -> Iterator[str]: assert (stream_name is None) == (stream_data is None) diff --git a/src/ocrmypdf/pdfinfo/info.py b/src/ocrmypdf/pdfinfo/info.py index 9a18e64d..9c59bc82 100644 --- a/src/ocrmypdf/pdfinfo/info.py +++ b/src/ocrmypdf/pdfinfo/info.py @@ -959,7 +959,7 @@ class PdfInfo: *, detailed_analysis: bool = False, progbar: bool = False, - max_workers: int = None, + max_workers: int | None = None, check_pages=None, executor: Executor = DEFAULT_EXECUTOR, ): diff --git a/src/ocrmypdf/pluginspec.py b/src/ocrmypdf/pluginspec.py index de7c19fe..598bd538 100644 --- a/src/ocrmypdf/pluginspec.py +++ b/src/ocrmypdf/pluginspec.py @@ -28,6 +28,7 @@ if TYPE_CHECKING: hookspec = pluggy.HookspecMarker('ocrmypdf') # pylint: disable=unused-argument +# mypy: @hookspec(firstresult=True) @@ -43,7 +44,7 @@ def get_logging_console() -> Handler: @hookspec -def initialize(plugin_manager: pluggy.PluginManager): +def initialize(plugin_manager: pluggy.PluginManager) -> None: """Called when this plugin is first loaded into OCRmyPDF. The primary intended use of this is for plugins to check compatibility with other