Skip to content

Commit

Permalink
ruff linting/Python 3.10 cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarlow83 committed Feb 14, 2024
1 parent 906c130 commit 6a746a1
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/ocrmypdf/_concurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import threading
from abc import ABC, abstractmethod
from collections.abc import Iterable
from typing import Any, Callable, TypeVar
from collections.abc import Callable, Iterable
from typing import Any, TypeVar

from ocrmypdf._progressbar import NullProgressBar, ProgressBar

Expand Down
3 changes: 1 addition & 2 deletions src/ocrmypdf/_exec/unpaper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import logging
import os
import shlex
import sys
from collections.abc import Iterator
from contextlib import contextmanager
from decimal import Decimal
Expand All @@ -20,7 +19,7 @@
from packaging.version import Version
from PIL import Image

from ocrmypdf.exceptions import MissingDependencyError, SubprocessOutputError
from ocrmypdf.exceptions import SubprocessOutputError
from ocrmypdf.subprocess import get_version, run

# unpaper documentation:
Expand Down
4 changes: 2 additions & 2 deletions src/ocrmypdf/_jobcontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def __getstate__(self):
state = self.__dict__.copy()

state['options'] = copy(self.options)
if not isinstance(state['options'].input_file, (str, bytes, os.PathLike)):
if not isinstance(state['options'].input_file, str | bytes | os.PathLike):
state['options'].input_file = 'stream'
if not isinstance(state['options'].output_file, (str, bytes, os.PathLike)):
if not isinstance(state['options'].output_file, str | bytes | os.PathLike):
state['options'].output_file = 'stream'
return state
2 changes: 1 addition & 1 deletion src/ocrmypdf/_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from contextlib import suppress
from io import BytesIO
from pathlib import Path
from shutil import copyfileobj, copystat
from shutil import copyfileobj
from typing import Any, BinaryIO, TypeVar, cast

import img2pdf
Expand Down
4 changes: 2 additions & 2 deletions src/ocrmypdf/_pipelines/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
import shutil
import sys
import threading
from collections.abc import Sequence
from collections.abc import Callable, Sequence
from concurrent.futures.process import BrokenProcessPool
from concurrent.futures.thread import BrokenThreadPool
from contextlib import contextmanager
from dataclasses import dataclass
from pathlib import Path
from typing import Callable, NamedTuple, cast
from typing import NamedTuple, cast

import PIL

Expand Down
23 changes: 14 additions & 9 deletions src/ocrmypdf/builtin_plugins/concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import signal
import sys
import threading
from collections.abc import Iterable
from collections.abc import Callable, Iterable
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed
from contextlib import suppress
from typing import Callable, Union
from typing import Union

from rich.console import Console as RichConsole

Expand All @@ -25,8 +25,10 @@
from ocrmypdf.exceptions import InputFileError
from ocrmypdf.helpers import remove_all_log_handlers

FuturesExecutorClass = Union[type[ThreadPoolExecutor], type[ProcessPoolExecutor]]
Queue = Union[multiprocessing.Queue, queue.Queue]
FuturesExecutorClass = Union[ # noqa: UP007
type[ThreadPoolExecutor], type[ProcessPoolExecutor]
]
Queue = Union[multiprocessing.Queue, queue.Queue] # noqa: UP007
UserInit = Callable[[], None]
WorkerInit = Callable[[Queue, UserInit, int], None]

Expand Down Expand Up @@ -128,11 +130,14 @@ def _execute(
listener = threading.Thread(target=log_listener, args=(log_queue,))
listener.start()

with self.pbar_class(**progress_kwargs) as pbar, executor_class(
max_workers=max_workers,
initializer=initializer,
initargs=(log_queue, worker_initializer, logging.getLogger("").level),
) as executor:
with (
self.pbar_class(**progress_kwargs) as pbar,
executor_class(
max_workers=max_workers,
initializer=initializer,
initargs=(log_queue, worker_initializer, logging.getLogger("").level),
) as executor,
):
futures = [executor.submit(task, *args) for args in task_arguments]
try:
for future in as_completed(futures):
Expand Down
4 changes: 2 additions & 2 deletions src/ocrmypdf/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from __future__ import annotations

import argparse
from collections.abc import Mapping
from typing import Any, Callable, TypeVar
from collections.abc import Callable, Mapping
from typing import Any, TypeVar

from ocrmypdf._version import PROGRAM_NAME as _PROGRAM_NAME
from ocrmypdf._version import __version__ as _VERSION
Expand Down
3 changes: 1 addition & 2 deletions src/ocrmypdf/extra_plugins/semfree.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
import logging
import logging.handlers
import signal
from collections.abc import Iterable, Iterator
from collections.abc import Callable, Iterable, Iterator
from contextlib import suppress
from enum import Enum, auto
from itertools import islice, repeat, takewhile, zip_longest
from multiprocessing import Pipe, Process
from multiprocessing.connection import Connection, wait
from typing import Callable

from ocrmypdf import Executor, hookimpl
from ocrmypdf._concurrent import NullProgressBar
Expand Down
3 changes: 1 addition & 2 deletions src/ocrmypdf/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import os
import shutil
import warnings
from collections.abc import Iterable, Sequence
from collections.abc import Callable, Iterable, Sequence
from contextlib import suppress
from decimal import Decimal
from io import StringIO
Expand All @@ -19,7 +19,6 @@
from statistics import harmonic_mean
from typing import (
Any,
Callable,
Generic,
TypeVar,
)
Expand Down
1 change: 0 additions & 1 deletion src/ocrmypdf/hocrtransform/_hocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def __init__(
debug_render_options: DebugRenderOptions | None = None,
):
"""Initialize the HocrTransform object."""

if debug:
log.warning("Use debug_render_options instead", DeprecationWarning)
self.render_options = DebugRenderOptions(
Expand Down
5 changes: 2 additions & 3 deletions src/ocrmypdf/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
import tempfile
import threading
from collections import defaultdict
from collections.abc import Iterator, MutableSet, Sequence
from collections.abc import Callable, Iterator, MutableSet, Sequence
from os import fspath
from pathlib import Path
from typing import Any, Callable, NamedTuple, NewType
from warnings import warn
from typing import Any, NamedTuple, NewType
from zlib import compress

import img2pdf
Expand Down
4 changes: 2 additions & 2 deletions src/ocrmypdf/pdfinfo/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
import statistics
import sys
from collections import defaultdict
from collections.abc import Container, Iterable, Iterator, Mapping, Sequence
from collections.abc import Callable, Container, Iterable, Iterator, Mapping, Sequence
from contextlib import contextmanager
from decimal import Decimal
from enum import Enum, auto
from functools import partial
from math import hypot, inf, isclose
from os import PathLike
from pathlib import Path
from typing import Callable, NamedTuple
from typing import NamedTuple
from warnings import warn

from pdfminer.layout import LTPage, LTTextBox
Expand Down
4 changes: 2 additions & 2 deletions src/ocrmypdf/pdfinfo/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from __future__ import annotations

import re
from collections.abc import Mapping
from collections.abc import Iterator, Mapping
from contextlib import contextmanager
from math import copysign
from os import PathLike
from pathlib import Path
from typing import Any, Iterator
from typing import Any
from unittest.mock import patch

import pdfminer
Expand Down
5 changes: 2 additions & 3 deletions src/ocrmypdf/subprocess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
import os
import re
import sys
from collections.abc import Mapping, Sequence
from collections.abc import Callable, Mapping, Sequence
from contextlib import suppress
from pathlib import Path
from subprocess import PIPE, STDOUT, CalledProcessError, CompletedProcess, Popen
from subprocess import run as subprocess_run
from typing import Callable, Union

from packaging.version import Version

Expand All @@ -23,7 +22,7 @@

log = logging.getLogger(__name__)

Args = Sequence[Union[Path, str]]
Args = Sequence[Path | str]
OsEnviron = os._Environ # pylint: disable=protected-access


Expand Down
9 changes: 2 additions & 7 deletions src/ocrmypdf/subprocess/_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@
import re
import shutil
import sys
from collections.abc import Iterable, Iterator
from collections.abc import Callable, Iterable, Iterator
from itertools import chain
from pathlib import Path
from typing import Any, Callable, TypeVar
from typing import Any, TypeAlias, TypeVar

from packaging.version import InvalidVersion, Version

if sys.version_info >= (3, 10):
from typing import TypeAlias
else:
from typing_extensions import TypeAlias # pragma: no cover

if sys.platform == 'win32':
# mypy understands 'if sys.platform' better than try/except ModuleNotFoundError
import winreg # pylint: disable=import-error
Expand Down
1 change: 0 additions & 1 deletion tests/plugins/gs_render_soft_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from __future__ import annotations

from pathlib import Path
from subprocess import CalledProcessError
from unittest.mock import patch

Expand Down
2 changes: 1 addition & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import ocrmypdf
from ocrmypdf._exec import tesseract
from ocrmypdf.exceptions import ExitCode, MissingDependencyError, OutputFileAccessError
from ocrmypdf.exceptions import ExitCode, MissingDependencyError
from ocrmypdf.pdfa import file_claims_pdfa
from ocrmypdf.pdfinfo import Colorspace, Encoding, PdfInfo
from ocrmypdf.subprocess import get_version
Expand Down
3 changes: 1 addition & 2 deletions tests/test_userunit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

import pytest

from ocrmypdf.exceptions import ExitCode
from ocrmypdf.pdfinfo import PdfInfo

from .conftest import check_ocrmypdf, run_ocrmypdf_api
from .conftest import check_ocrmypdf

# pylint: disable=redefined-outer-name

Expand Down

0 comments on commit 6a746a1

Please sign in to comment.