Python 3.10 cleanup, manual fixes

This commit is contained in:
James R. Barlow
2024-02-14 12:48:17 -08:00
parent 6a746a1cbb
commit 3a3635f7f9
4 changed files with 13 additions and 19 deletions
+3 -2
View File
@@ -8,6 +8,7 @@
# Do not enable annotations!
# https://github.com/tiangolo/typer/discussions/598
# from __future__ import annotations
from __future__ import annotations
import json
import logging
@@ -131,7 +132,7 @@ def execute_ocrmypdf(
class HandleObserverEvent(PatternMatchingEventHandler):
def __init__(
def __init__( # noqa: D107
self,
patterns=None,
ignore_patterns=None,
@@ -191,7 +192,7 @@ def main(
bool,
typer.Option(
envvar='OCR_OUTPUT_DIRECTORY_YEAR_MONTH',
help='Create a subdirectory in the output directory for each year and month',
help='Create a subdirectory in the output directory for each year/month',
),
] = False,
on_success_delete: Annotated[
+1 -2
View File
@@ -14,7 +14,6 @@ from decimal import Decimal
from pathlib import Path
from subprocess import PIPE, STDOUT
from tempfile import TemporaryDirectory
from typing import Union
from packaging.version import Version
from PIL import Image
@@ -28,7 +27,7 @@ from ocrmypdf.subprocess import get_version, run
UNPAPER_IMAGE_PIXEL_LIMIT = 256 * 1024 * 1024
DecFloat = Union[Decimal, float]
DecFloat = Decimal | float
log = logging.getLogger(__name__)
+7 -7
View File
@@ -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, Union
from typing import AnyStr, BinaryIO
from warnings import warn
import pluggy
@@ -28,8 +28,8 @@ from ocrmypdf._validation import check_options
from ocrmypdf.cli import ArgumentParser, get_parser
from ocrmypdf.helpers import is_iterable_notstr
StrPath = Union[Path, AnyStr]
PathOrIO = Union[BinaryIO, StrPath]
StrPath = Path | AnyStr
PathOrIO = BinaryIO | StrPath
# Installing plugins affects the global state of the Python interpreter,
# so we need to use a lock to prevent multiple threads from installing
@@ -169,7 +169,7 @@ def _kwargs_to_cmdline(
# We have a parameter
cmdline.append(f"--{cmd_style_arg}")
if isinstance(val, (int, float)):
if isinstance(val, int | float):
cmdline.append(str(val))
elif isinstance(val, str):
cmdline.append(val)
@@ -201,11 +201,11 @@ def create_options(
defer_kwargs={'progress_bar', 'plugins', 'parser', 'input_file', 'output_file'},
**kwargs,
)
if isinstance(input_file, (BinaryIO, IOBase)):
if isinstance(input_file, BinaryIO | IOBase):
cmdline.append('stream://input_file')
else:
cmdline.append(os.fspath(input_file))
if isinstance(output_file, (BinaryIO, IOBase)):
if isinstance(output_file, BinaryIO | IOBase):
cmdline.append('stream://output_file')
else:
cmdline.append(os.fspath(output_file))
@@ -343,7 +343,7 @@ def ocr( # noqa: D417
if not plugins:
plugins = []
elif isinstance(plugins, (str, Path)):
elif isinstance(plugins, str | Path):
plugins = [plugins]
else:
plugins = list(plugins)
+2 -8
View File
@@ -10,7 +10,6 @@ import atexit
import logging
import re
import statistics
import sys
from collections import defaultdict
from collections.abc import Callable, Container, Iterable, Iterator, Mapping, Sequence
from contextlib import contextmanager
@@ -1060,12 +1059,7 @@ class PageInfo:
weights = [area / total_drawn_area for area in image_areas]
# Calculate harmonic mean of DPIs weighted by area
if sys.version_info >= (3, 10):
weighted_dpi = statistics.harmonic_mean(image_dpis, weights)
else:
weighted_dpi = sum(weights) / sum(
weight / dpi for weight, dpi in zip(weights, image_dpis)
)
weighted_dpi = statistics.harmonic_mean(image_dpis, weights)
max_dpi = max(image_dpis)
dpi_average_max_ratio = weighted_dpi / max_dpi
@@ -1176,7 +1170,7 @@ class PdfInfo:
@property
def filename(self) -> str | Path:
"""Return filename of PDF."""
if not isinstance(self._infile, (str, Path)):
if not isinstance(self._infile, str | Path):
raise NotImplementedError("can't get filename from stream")
return self._infile