Skip to content

Commit

Permalink
MAINT: Update requirements + mypy fixes (#2275)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma authored Oct 28, 2023
1 parent 06c1a19 commit faa8c68
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 42 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion docs/user/merging-pdfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions make_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
)


Expand Down
5 changes: 3 additions & 2 deletions pypdf/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import functools
import logging
import re
import sys
import warnings
from dataclasses import dataclass
from datetime import datetime, timezone
Expand All @@ -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 (
Expand Down
26 changes: 19 additions & 7 deletions pypdf/_xobj_image_helpers.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -70,22 +76,28 @@ 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
elif color_space[0] == "/Separation":
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 = {
Expand Down
5 changes: 3 additions & 2 deletions pypdf/annotations/_markup_annotations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from abc import ABC
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union

Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions pypdf/types.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 6 additions & 6 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -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
#
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions requirements/docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
7 changes: 4 additions & 3 deletions tests/test_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_xobject_image_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

0 comments on commit faa8c68

Please sign in to comment.