diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 312bc8dfc..ad3856f14 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,7 @@ # pre-commit run --all-files repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v4.5.0 hooks: - id: check-ast - id: check-byte-order-marker @@ -18,7 +18,7 @@ repos: - id: check-added-large-files args: ['--maxkb=1000'] - repo: https://github.com/psf/black - rev: 23.9.1 + rev: 23.10.1 hooks: - id: black args: [--target-version, py36] @@ -29,12 +29,12 @@ repos: additional_dependencies: [black==22.1.0] exclude: "docs/user/robustness.md" - repo: https://github.com/charliermarsh/ruff-pre-commit - rev: v0.0.290 + rev: v0.1.3 hooks: - id: ruff args: ['--fix'] - repo: https://github.com/asottile/pyupgrade - rev: v3.12.0 + rev: v3.15.0 hooks: - id: pyupgrade args: [--py36-plus] @@ -45,7 +45,7 @@ repos: args: ["--ignore", "E,W,F"] - repo: https://github.com/pre-commit/mirrors-mypy - rev: 'v1.5.1' + rev: 'v1.6.1' hooks: - id: mypy additional_dependencies: [types-Pillow==10.0.0.2] diff --git a/docs/user/merging-pdfs.md b/docs/user/merging-pdfs.md index afb640cc2..ca01fa985 100644 --- a/docs/user/merging-pdfs.md +++ b/docs/user/merging-pdfs.md @@ -131,7 +131,7 @@ It means that you may copy lots of objects which will be saved in the output PDF In order to prevent this, you can provide the list of fields in the dictionaries to be ignored: ```python -new_page = writer.add_page(reader.pages[0], excluded_fields=["/B"]) +new_page = writer.add_page(reader.pages[0], excluded_fields=["/B"]) ``` ### Merging rotated pages diff --git a/make_release.py b/make_release.py index 9b106c7d8..8f4ff9f60 100644 --- a/make_release.py +++ b/make_release.py @@ -197,7 +197,9 @@ def get_formatted_changes(git_tag: str) -> Tuple[str, str]: for commit in commits: if commit.prefix not in grouped: grouped[commit.prefix] = [] - grouped[commit.prefix].append({"msg": commit.message, "author": commit.author_login}) + grouped[commit.prefix].append( + {"msg": commit.message, "author": commit.author_login} + ) # Order prefixes order = [ @@ -240,9 +242,7 @@ def get_formatted_changes(git_tag: str) -> Tuple[str, str]: output_with_user += tmp for commit in grouped[prefix]: output += f"- {commit['msg']}\n" - output_with_user += ( - f"- {commit['msg']} by @{commit['author']}\n" - ) + output_with_user += f"- {commit['msg']} by @{commit['author']}\n" del grouped[prefix] if grouped: @@ -357,7 +357,11 @@ def parse_commit_line(line: str, authors: Dict[str, str]) -> Change: prefix = "DOC" return Change( - commit_hash=commit_hash, prefix=prefix, message=message, author=author, author_login=author_login + commit_hash=commit_hash, + prefix=prefix, + message=message, + author=author, + author_login=author_login, ) diff --git a/pypdf/_utils.py b/pypdf/_utils.py index 684d46616..9613cd1b0 100644 --- a/pypdf/_utils.py +++ b/pypdf/_utils.py @@ -32,6 +32,7 @@ import functools import logging import re +import sys import warnings from dataclasses import dataclass from datetime import datetime, timezone @@ -51,10 +52,10 @@ overload, ) -try: +if sys.version_info[:2] >= (3, 10): # Python 3.10+: https://www.python.org/dev/peps/pep-0484/ from typing import TypeAlias -except ImportError: +else: from typing_extensions import TypeAlias from .errors import ( diff --git a/pypdf/_xobj_image_helpers.py b/pypdf/_xobj_image_helpers.py index f6ee69c23..f41b772b0 100644 --- a/pypdf/_xobj_image_helpers.py +++ b/pypdf/_xobj_image_helpers.py @@ -1,5 +1,6 @@ """Code in here is only used by pypdf.filters._xobj_to_image""" +import sys from io import BytesIO from typing import Any, List, Tuple, Union, cast @@ -14,12 +15,17 @@ NullObject, ) -try: - from typing import Literal, TypeAlias -except ImportError: +if sys.version_info[:2] >= (3, 8): + from typing import Literal +else: # PEP 586 introduced typing.Literal with Python 3.8 # For older Python versions, the backport typing_extensions is necessary: - from typing_extensions import Literal, TypeAlias # type: ignore[assignment] + from typing_extensions import Literal # type: ignore[assignment] + +if sys.version_info[:2] >= (3, 10): + from typing import TypeAlias +else: + from typing_extensions import TypeAlias try: @@ -70,7 +76,9 @@ def _get_imagemode( color_space = color_space[1] if isinstance(color_space, IndirectObject): color_space = color_space.get_object() - mode2, invert_color = _get_imagemode(color_space, color_components, prev_mode, depth + 1) + mode2, invert_color = _get_imagemode( + color_space, color_components, prev_mode, depth + 1 + ) if mode2 in ("RGB", "CMYK"): mode2 = "P" return mode2, invert_color @@ -78,14 +86,18 @@ def _get_imagemode( color_space = color_space[2] if isinstance(color_space, IndirectObject): color_space = color_space.get_object() - mode2, invert_color = _get_imagemode(color_space, color_components, prev_mode, depth + 1) + mode2, invert_color = _get_imagemode( + color_space, color_components, prev_mode, depth + 1 + ) return mode2, True elif color_space[0] == "/DeviceN": color_components = len(color_space[1]) color_space = color_space[2] if isinstance(color_space, IndirectObject): # pragma: no cover color_space = color_space.get_object() - mode2, invert_color = _get_imagemode(color_space, color_components, prev_mode, depth + 1) + mode2, invert_color = _get_imagemode( + color_space, color_components, prev_mode, depth + 1 + ) return mode2, invert_color mode_map = { diff --git a/pypdf/annotations/_markup_annotations.py b/pypdf/annotations/_markup_annotations.py index 09c9c256e..c5e0a2fed 100644 --- a/pypdf/annotations/_markup_annotations.py +++ b/pypdf/annotations/_markup_annotations.py @@ -1,3 +1,4 @@ +import sys from abc import ABC from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union @@ -14,9 +15,9 @@ from ..generic._utils import hex_to_rgb from ._base import NO_FLAGS, AnnotationDictionary -try: +if sys.version_info[:2] >= (3, 10): from typing import TypeAlias -except ImportError: +else: # PEP 613 introduced typing.TypeAlias with Python 3.10 # For older Python versions, the backport typing_extensions is necessary: from typing_extensions import TypeAlias diff --git a/pypdf/types.py b/pypdf/types.py index 04d52c36c..0fb3c88b9 100644 --- a/pypdf/types.py +++ b/pypdf/types.py @@ -1,17 +1,18 @@ """Helpers for working with PDF types.""" +import sys from typing import List, Union -try: +if sys.version_info[:2] >= (3, 8): # Python 3.8+: https://peps.python.org/pep-0586 from typing import Literal -except ImportError: +else: from typing_extensions import Literal # type: ignore[assignment] -try: +if sys.version_info[:2] >= (3, 10): # Python 3.10+: https://www.python.org/dev/peps/pep-0484/ from typing import TypeAlias -except ImportError: +else: from typing_extensions import TypeAlias from .generic._base import NameObject, NullObject, NumberObject diff --git a/requirements/dev.txt b/requirements/dev.txt index edab7dd28..d5f0ffbf6 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -1,6 +1,6 @@ # -# This file is autogenerated by pip-compile with Python 3.7 -# by the following command: +# This file is autogenerated by pip-compile with python 3.7 +# To update, run: # # pip-compile requirements/dev.in # @@ -12,7 +12,7 @@ certifi==2023.7.22 # via requests cfgv==3.3.1 # via pre-commit -charset-normalizer==3.3.0 +charset-normalizer==3.3.1 # via requests click==8.1.7 # via @@ -70,7 +70,7 @@ pre-commit==2.17.0 # via -r requirements/dev.in pyproject-hooks==1.0.0 # via build -pytest==7.4.2 +pytest==7.4.3 # via pytest-cov pytest-cov==4.1.0 # via -r requirements/dev.in @@ -96,9 +96,9 @@ typing-extensions==4.7.1 # black # importlib-metadata # platformdirs -urllib3==2.0.6 +urllib3==2.0.7 # via requests -virtualenv==20.24.5 +virtualenv==20.24.6 # via pre-commit wheel==0.41.2 # via diff --git a/requirements/docs.txt b/requirements/docs.txt index d63751481..b4f9fa7a1 100644 --- a/requirements/docs.txt +++ b/requirements/docs.txt @@ -8,11 +8,11 @@ alabaster==0.7.13 # via sphinx attrs==23.1.0 # via -r requirements/docs.in -babel==2.13.0 +babel==2.13.1 # via sphinx certifi==2023.7.22 # via requests -charset-normalizer==3.3.0 +charset-normalizer==3.3.1 # via requests docutils==0.17.1 # via @@ -81,7 +81,7 @@ typing-extensions==4.7.1 # via # importlib-metadata # markdown-it-py -urllib3==2.0.6 +urllib3==2.0.7 # via requests zipp==3.15.0 # via importlib-metadata diff --git a/tests/test_encryption.py b/tests/test_encryption.py index af4d599e0..cb2d1f719 100644 --- a/tests/test_encryption.py +++ b/tests/test_encryption.py @@ -343,6 +343,7 @@ def test_aes_decrypt_corrupted_data(): for num in [0, 17, 32]: aes.decrypt(secrets.token_bytes(num)) + def test_encrypt_stream_dictionary(pdf_file_path): user_password = secrets.token_urlsafe(10) @@ -353,9 +354,9 @@ def test_encrypt_stream_dictionary(pdf_file_path): writer = PdfWriter() writer.add_page(reader.pages[0]) writer.encrypt( - user_password=user_password, - owner_password=None, - algorithm="RC4-128", + user_password=user_password, + owner_password=None, + algorithm="RC4-128", ) with open(pdf_file_path, "wb") as output_stream: writer.write(output_stream) diff --git a/tests/test_utils.py b/tests/test_utils.py index 1c710e221..f00be5d62 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -241,7 +241,6 @@ def foo(old_param: int = 1, baz: int = 2) -> None: def test_deprecate_with_replacement(): def foo() -> None: deprecate_with_replacement("foo", "bar", removed_in="4.3.2") - pass with pytest.warns( DeprecationWarning, @@ -253,7 +252,6 @@ def foo() -> None: def test_deprecation_no_replacement(): def foo() -> None: deprecation_no_replacement("foo", removed_in="4.3.2") - pass with pytest.raises( DeprecationError, diff --git a/tests/test_xobject_image_helpers.py b/tests/test_xobject_image_helpers.py index 8903d41c2..e6d9e8e0f 100644 --- a/tests/test_xobject_image_helpers.py +++ b/tests/test_xobject_image_helpers.py @@ -21,7 +21,7 @@ def test_get_imagemode_recursion_depth(): target = b"\n10 0 obj\n[ /DeviceN [ /HKS#2044#20K /Magenta /Yellow /Black ] 10 0 R 11 0 R 12 0 R ]\nendobj\n" reader = PdfReader(BytesIO(content.replace(source, target))) with pytest.raises( - PdfReadError, - match="Color spaces nested too deep. If required, consider increasing MAX_IMAGE_MODE_NESTING_DEPTH." + PdfReadError, + match="Color spaces nested too deep. If required, consider increasing MAX_IMAGE_MODE_NESTING_DEPTH.", ): reader.pages[0].images[0]