Skip to content

Commit

Permalink
DEV: Fix type annotations for add_bookmarks (#1000)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma authored Jun 16, 2022
1 parent faebc9d commit 1ccf484
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 9 deletions.
5 changes: 3 additions & 2 deletions PyPDF2/_merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
OutlinesType,
PagemodeType,
ZoomArgsType,
ZoomArgType,
)

ERR_CLOSED_WRITER = "close() was called and thus the writer cannot be used anymore"
Expand Down Expand Up @@ -622,7 +623,7 @@ def addBookmark(
bold: bool = False,
italic: bool = False,
fit: FitType = "/Fit",
*args: ZoomArgsType,
*args: ZoomArgType,
) -> IndirectObject: # pragma: no cover
"""
.. deprecated:: 1.28.0
Expand All @@ -642,7 +643,7 @@ def add_bookmark(
bold: bool = False,
italic: bool = False,
fit: FitType = "/Fit",
*args: ZoomArgsType,
*args: ZoomArgType,
) -> IndirectObject:
"""
Add a bookmark to this PDF file.
Expand Down
4 changes: 2 additions & 2 deletions PyPDF2/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ def add_bookmark(
bold: bool = False,
italic: bool = False,
fit: FitType = "/Fit",
*args: ZoomArgsType,
*args: ZoomArgType,
) -> IndirectObject:
"""
Add a bookmark to this PDF file.
Expand Down Expand Up @@ -1098,7 +1098,7 @@ def addBookmark(
bold: bool = False,
italic: bool = False,
fit: FitType = "/Fit",
*args: ZoomArgsType,
*args: ZoomArgType,
) -> IndirectObject: # pragma: no cover
"""
.. deprecated:: 1.28.0
Expand Down
9 changes: 6 additions & 3 deletions PyPDF2/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import re
import warnings
from io import BytesIO
from typing import Any, Dict, List, Optional, Tuple, Union
from typing import Any, Dict, List, Optional, Tuple, Union, cast

from ._utils import (
WHITESPACES,
Expand Down Expand Up @@ -847,12 +847,15 @@ def add_child(self, child: Any, pdf: Any) -> None: # PdfReader
child = pdf.get_reference(child_obj)
assert isinstance(child, IndirectObject)

prev: Optional[DictionaryObject]
if "/First" not in self:
self[NameObject("/First")] = child
self[NameObject("/Count")] = NumberObject(0)
prev = None
else:
prev = self["/Last"]
prev = cast(
DictionaryObject, self["/Last"]
) # TABLE 8.3 Entries in the outline dictionary

self[NameObject("/Last")] = child
self[NameObject("/Count")] = NumberObject(self[NameObject("/Count")] + 1) # type: ignore
Expand All @@ -861,7 +864,7 @@ def add_child(self, child: Any, pdf: Any) -> None: # PdfReader
prev_ref = pdf.get_reference(prev)
assert isinstance(prev_ref, IndirectObject)
child_obj[NameObject("/Prev")] = prev_ref
prev[NameObject("/Next")] = child # type: ignore
prev[NameObject("/Next")] = child

parent_ref = pdf.get_reference(self)
assert isinstance(parent_ref, IndirectObject)
Expand Down
2 changes: 1 addition & 1 deletion PyPDF2/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"/Fit", "/XYZ", "/FitH", "/FitV", "/FitR", "/FitB", "/FitBH", "/FitBV"
]
# Those go with the FitType: They specify values for the fit
ZoomArgType: TypeAlias = Union[NumberObject, NullObject]
ZoomArgType: TypeAlias = Union[NumberObject, NullObject, float]
ZoomArgsType: TypeAlias = List[ZoomArgType]

# Recursive types are not yet supported by mypy:
Expand Down
26 changes: 25 additions & 1 deletion tests/test_merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,29 @@ def test_merge():
merger.append(fh)

bookmark = merger.add_bookmark("A bookmark", 0)
merger.add_bookmark("deeper", 0, parent=bookmark, italic=True, bold=True)
bm2 = merger.add_bookmark("deeper", 1, parent=bookmark, italic=True, bold=True)
merger.add_bookmark(
"Let's see", merger.pages[2], bm2, (255, 255, 0), True, True, "/FitBV", 12
)
merger.add_bookmark(
"The XYZ fit", 0, bookmark, (255, 0, 15), True, True, "/XYZ", 10, 20, 3
)
merger.add_bookmark(
"The FitH fit", 0, bookmark, (255, 0, 15), True, True, "/FitH", 10
)
merger.add_bookmark(
"The FitV fit", 0, bookmark, (255, 0, 15), True, True, "/FitV", 10
)
merger.add_bookmark(
"The FitR fit", 0, bookmark, (255, 0, 15), True, True, "/FitR", 10, 20, 30, 40
)
merger.add_bookmark("The FitB fit", 0, bookmark, (255, 0, 15), True, True, "/FitB")
merger.add_bookmark(
"The FitBH fit", 0, bookmark, (255, 0, 15), True, True, "/FitBH", 10
)
merger.add_bookmark(
"The FitBV fit", 0, bookmark, (255, 0, 15), True, True, "/FitBV", 10
)
merger.add_metadata({"author": "Martin Thoma"})
merger.add_named_destination("title", 0)
merger.set_page_layout("/SinglePage")
Expand Down Expand Up @@ -66,5 +88,7 @@ def test_merge():
"foo",
]

# TODO: There seem to be no destionations for those links?

# Clean up
os.remove(tmp_path)
15 changes: 15 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ def test_writer_operations():
writer.insert_page(reader_outline.pages[0], 0)
writer.add_bookmark_destination(page)
writer.remove_links()
writer.add_bookmark_destination(page)
bm = writer.add_bookmark(
"A bookmark", 0, None, (255, 0, 15), True, True, "/FitBV", 10
)
writer.add_bookmark(
"The XYZ fit", 0, bm, (255, 0, 15), True, True, "/XYZ", 10, 20, 3
)
writer.add_bookmark("The FitH fit", 0, bm, (255, 0, 15), True, True, "/FitH", 10)
writer.add_bookmark("The FitV fit", 0, bm, (255, 0, 15), True, True, "/FitV", 10)
writer.add_bookmark(
"The FitR fit", 0, bm, (255, 0, 15), True, True, "/FitR", 10, 20, 30, 40
)
writer.add_bookmark("The FitB fit", 0, bm, (255, 0, 15), True, True, "/FitB")
writer.add_bookmark("The FitBH fit", 0, bm, (255, 0, 15), True, True, "/FitBH", 10)
writer.add_bookmark("The FitBV fit", 0, bm, (255, 0, 15), True, True, "/FitBV", 10)
writer.add_blank_page()
writer.add_uri(2, "https://example.com", RectangleObject([0, 0, 100, 100]))
writer.add_link(2, 1, RectangleObject([0, 0, 100, 100]))
Expand Down

0 comments on commit 1ccf484

Please sign in to comment.