Upgrade pre-commit and associated tools; various lints

This commit is contained in:
James Barlow
2022-04-03 20:53:01 -07:00
parent f3593c915d
commit 776ada6713
15 changed files with 49 additions and 46 deletions
+6 -6
View File
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: v4.1.0
hooks:
- id: check-case-conflict
- id: check-merge-conflict
@@ -8,26 +8,26 @@ repos:
- id: check-yaml
- id: debug-statements
- repo: https://github.com/pycqa/isort
rev: 5.9.3
rev: 5.10.1
hooks:
- id: isort
args: ["--profile", "black"]
- repo: https://github.com/psf/black
rev: 21.9b0
rev: 22.3.0
hooks:
- id: black
language_version: python
- repo: https://github.com/asottile/setup-cfg-fmt
rev: v1.19.0
rev: v1.20.1
hooks:
- id: setup-cfg-fmt
- repo: https://github.com/asottile/pyupgrade
rev: v2.29.0
rev: v2.31.1
hooks:
- id: pyupgrade
args: ["--py37-plus"]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.910-1
rev: v0.942
hooks:
- id: mypy
additional_dependencies:
+1 -1
View File
@@ -59,7 +59,7 @@ def do_ocrmypdf(file):
return Response("--sidecar not supported", 501, mimetype='text/plain')
ocrmypdf_args = ["ocrmypdf", *cmd_args, up_file, down_file]
proc = run(ocrmypdf_args, stdout=PIPE, stderr=PIPE, encoding="utf-8")
proc = run(ocrmypdf_args, capture_output=True, encoding="utf-8")
if proc.returncode != 0:
stderr = proc.stderr
return Response(stderr, 400, mimetype='text/plain')
+1 -1
View File
@@ -50,7 +50,7 @@ install_requires =
img2pdf>=0.3.0,<0.5 # pure Python
packaging>=20
pdfminer.six!=20200720,>=20191110,<=20220319
pikepdf>=4.0.0,!=5.0.0
pikepdf!=5.0.0,>=4.0.0
pluggy>=0.13.0,<2
reportlab>=3.5.66
tqdm>=4
+8 -10
View File
@@ -24,14 +24,12 @@ from ocrmypdf.exceptions import MissingDependencyError, SubprocessOutputError
from ocrmypdf.helpers import Resolution
from ocrmypdf.subprocess import get_version, run, run_polling_stderr
# Remove this workaround when we require Pillow >= 10
try:
ROTATE_90 = Image.Transpose.ROTATE_90
ROTATE_180 = Image.Transpose.ROTATE_180
ROTATE_270 = Image.Transpose.ROTATE_270
Transpose = Image.Transpose # type: ignore
except AttributeError:
ROTATE_90 = Image.ROTATE_90
ROTATE_180 = Image.ROTATE_180
ROTATE_270 = Image.ROTATE_270
# Pillow 9 shim
Transpose = Image # type: ignore
log = logging.getLogger(__name__)
@@ -47,7 +45,7 @@ For details see:
"""
# Most reliable what to get the bitness of Python interpreter, according to Python docs
_is_64bit = sys.maxsize > 2 ** 32
_is_64bit = sys.maxsize > 2**32
_gswin = None
if os.name == 'nt':
@@ -141,11 +139,11 @@ def rasterize_pdf(
# rotation is a clockwise angle and Image.ROTATE_* is
# counterclockwise so this cancels out the rotation
if rotation == 90:
im = im.transpose(ROTATE_90)
im = im.transpose(Transpose.ROTATE_90)
elif rotation == 180:
im = im.transpose(ROTATE_180)
im = im.transpose(Transpose.ROTATE_180)
elif rotation == 270:
im = im.transpose(ROTATE_270)
im = im.transpose(Transpose.ROTATE_270)
if rotation % 180 == 90:
page_dpi = page_dpi.flip_axis()
im.save(fspath(output_file), dpi=page_dpi)
+2 -2
View File
@@ -18,7 +18,7 @@ from decimal import Decimal
from pathlib import Path
from subprocess import PIPE, STDOUT
from tempfile import TemporaryDirectory
from typing import List, Optional, Tuple, Union
from typing import Iterator, List, Optional, Tuple, Union
from PIL import Image
@@ -76,7 +76,7 @@ def _convert_image(im: Image.Image) -> Tuple[Image.Image, bool, str]:
@contextmanager
def _setup_unpaper_io(input_file: Path) -> Tuple[Path, Path, Path]:
def _setup_unpaper_io(input_file: Path) -> Iterator[Tuple[Path, Path, Path]]:
with Image.open(input_file) as im:
if im.width * im.height >= UNPAPER_IMAGE_PIXEL_LIMIT:
raise UnpaperImageTooLargeError(w=im.width, h=im.height)
+8 -4
View File
@@ -18,7 +18,7 @@ from typing import Dict, Iterable, Optional
import img2pdf
import pikepdf
from pikepdf.models.metadata import encode_pdf_date
from PIL import Image, ImageDraw
from PIL import Image, ImageColor, ImageDraw
from ocrmypdf._concurrent import Executor
from ocrmypdf._exec import unpaper
@@ -38,10 +38,12 @@ from ocrmypdf.optimize import optimize
from ocrmypdf.pdfa import generate_pdfa_ps
from ocrmypdf.pdfinfo import Colorspace, Encoding, PdfInfo
# Remove this workaround when we require Pillow >= 10
try:
BICUBIC = Image.Resampling.BICUBIC
BICUBIC = Image.Resampling.BICUBIC # type: ignore
except AttributeError:
BICUBIC = Image.BICUBIC
# Pillow 9 shim
BICUBIC = Image.BICUBIC # type: ignore
log = logging.getLogger(__name__)
@@ -489,7 +491,9 @@ def preprocess_deskew(input_file: Path, page_context: PageContext):
# According to Pillow docs, .rotate() will automatically use Image.NEAREST
# resampling if image is mode '1' or 'P'
deskewed = im.rotate(
deskew_angle_degrees, resample=BICUBIC, fillcolor='white'
deskew_angle_degrees,
resample=BICUBIC,
fillcolor=ImageColor.getcolor('white', mode=im.mode),
)
deskewed.save(output_file, dpi=dpi)
+1 -1
View File
@@ -41,7 +41,7 @@ log = logging.getLogger(__name__)
def check_platform():
if os.name == 'nt' and sys.maxsize <= 2 ** 32: # pragma: no cover
if os.name == 'nt' and sys.maxsize <= 2**32: # pragma: no cover
# 32-bit interpreter on Windows
log.error(
"You are running OCRmyPDF in a 32-bit (x86) Python interpreter."
+4 -4
View File
@@ -647,8 +647,8 @@ def _pdf_pageinfo_concurrent(
max_workers,
check_pages,
detailed_analysis=False,
) -> List[Optional['PageInfo']]:
pages = [None] * len(pdf.pages)
) -> Sequence[Optional['PageInfo']]:
pages: Sequence[Optional['PageInfo']] = [None] * len(pdf.pages)
def update_pageinfo(result, pbar):
page = result
@@ -925,11 +925,11 @@ class PdfInfo:
@property
def min_version(self) -> str:
# The minimum PDF is the maximum version that any particular page needs
return max(page.min_version for page in self.pages)
return max(page.min_version for page in self.pages if page)
@property
def has_userunit(self) -> bool:
return any(page.userunit != 1.0 for page in self.pages)
return any(page.userunit != 1.0 for page in self.pages if page)
@property
def has_acroform(self) -> bool:
+4
View File
@@ -4,6 +4,9 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# type: ignore
# Non-Windows mypy now breaks when trying to typecheck winreg
import logging
import os
import shutil
@@ -17,6 +20,7 @@ try:
except ModuleNotFoundError as e:
raise ModuleNotFoundError("This module is for Windows only") from e
log = logging.getLogger(__name__)
T = TypeVar('T')
+1 -2
View File
@@ -134,8 +134,7 @@ def run_ocrmypdf(
p = run(
p_args,
stdout=PIPE,
stderr=PIPE,
capture_output=True,
text=text,
check=False,
)
+1 -5
View File
@@ -101,11 +101,7 @@ def get_cache_folder(source_pdf, run_args, parsed_args):
def cached_run(options, run_args, **run_kwargs):
run_args = [str(arg) for arg in run_args] # flatten PosixPaths
try:
args = parser.parse_args(run_args[1:])
except SystemExit:
breakpoint()
return
args = parser.parse_args(run_args[1:])
if args.imagename in ('stdin', '-'):
return run(run_args, **run_kwargs)
+2 -4
View File
@@ -24,8 +24,7 @@ def test_fish():
['fish', '-n', 'misc/completion/ocrmypdf.fish'],
check=True,
encoding='utf-8',
stdout=PIPE,
stderr=PIPE,
capture_output=True,
)
assert proc.stderr == '', proc.stderr
except FileNotFoundError:
@@ -41,8 +40,7 @@ def test_bash():
['bash', '-n', 'misc/completion/ocrmypdf.bash'],
check=True,
encoding='utf-8',
stdout=PIPE,
stderr=PIPE,
capture_output=True,
)
assert proc.stderr == '', proc.stderr
except FileNotFoundError:
+2 -4
View File
@@ -630,8 +630,7 @@ def test_compression_preserved(ocrmypdf_exec, resources, image, outpdf):
]
p = run(
p_args,
stdout=PIPE,
stderr=PIPE,
capture_output=True,
stdin=input_stream,
text=True,
check=False,
@@ -691,8 +690,7 @@ def test_compression_changed(ocrmypdf_exec, resources, image, compression, outpd
]
p = run(
p_args,
stdout=PIPE,
stderr=PIPE,
capture_output=True,
stdin=input_stream,
text=True,
check=False,
+7 -1
View File
@@ -25,6 +25,12 @@ from .conftest import check_ocrmypdf, run_ocrmypdf
# pylintx: disable=unused-variable
# Remove this workaround when we require Pillow >= 10
try:
Transpose = Image.Transpose # type: ignore
except AttributeError:
# Pillow 9 shim
Transpose = Image # type: ignore
RENDERERS = ['hocr', 'sandwich']
@@ -222,7 +228,7 @@ def test_rotate_page_level(image_angle, page_angle, resources, outdir):
with Image.open(fspath(resources / 'typewriter.png')) as im:
if image_angle != 0:
ccw_angle = -image_angle % 360
im = im.transpose(getattr(Image, f'ROTATE_{ccw_angle}'))
im = im.transpose(getattr(Transpose, f'ROTATE_{ccw_angle}'))
im.save(memimg, format='PNG')
memimg.seek(0)
mempdf = BytesIO()
+1 -1
View File
@@ -30,7 +30,7 @@ def test_stdin(ocrmypdf_exec, resources, outpdf):
'--plugin',
'tests/plugins/tesseract_noop.py',
]
run(p_args, stdout=PIPE, stderr=PIPE, stdin=input_stream, check=True)
run(p_args, capture_output=True, stdin=input_stream, check=True)
def test_stdout(ocrmypdf_exec, resources, outpdf):