Skip to content

Commit

Permalink
Python 3.10 cleanup, manual fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarlow83 committed Feb 14, 2024
1 parent 6a746a1 commit 3a3635f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
5 changes: 3 additions & 2 deletions misc/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -131,7 +132,7 @@ def execute_ocrmypdf(


class HandleObserverEvent(PatternMatchingEventHandler):
def __init__(
def __init__( # noqa: D107
self,
patterns=None,
ignore_patterns=None,
Expand Down Expand Up @@ -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[
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 @@ -14,7 +14,6 @@
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
Expand All @@ -28,7 +27,7 @@

UNPAPER_IMAGE_PIXEL_LIMIT = 256 * 1024 * 1024

DecFloat = Union[Decimal, float]
DecFloat = Decimal | float

log = logging.getLogger(__name__)

Expand Down
14 changes: 7 additions & 7 deletions src/ocrmypdf/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
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
Expand All @@ -28,8 +28,8 @@
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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 2 additions & 8 deletions src/ocrmypdf/pdfinfo/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
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
Expand Down Expand Up @@ -1060,12 +1059,7 @@ def page_dpi_profile(self) -> PageResolutionProfile | None:

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

Expand Down Expand Up @@ -1176,7 +1170,7 @@ def is_tagged(self) -> bool:
@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

Expand Down

0 comments on commit 3a3635f

Please sign in to comment.