Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: PdfReaderProtocol #1303

Merged
merged 5 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions PyPDF2/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
TextStringObject,
encode_pdfdocencoding,
)
from .types import PdfReaderProtocol


def _get_rectangle(self: Any, name: str, defaults: Iterable[str]) -> RectangleObject:
Expand Down Expand Up @@ -241,13 +242,11 @@ class PageObject(DictionaryObject):

def __init__(
self,
pdf: Optional[Any] = None, # PdfReader
pdf: Optional[PdfReaderProtocol] = None,
indirect_ref: Optional[IndirectObject] = None,
) -> None:
from ._reader import PdfReader

DictionaryObject.__init__(self)
self.pdf: Optional[PdfReader] = pdf
self.pdf: Optional[PdfReaderProtocol] = pdf
self.indirect_ref = indirect_ref

def hash_value_data(self) -> bytes:
Expand Down
4 changes: 2 additions & 2 deletions PyPDF2/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,9 +908,9 @@ def _build_outline_item(self, node: DictionaryObject) -> Optional[Destination]:
return outline_item

@property
def pages(self) -> _VirtualList:
def pages(self) -> List[PageObject]:
"""Read-only property that emulates a list of :py:class:`Page<PyPDF2._page.Page>` objects."""
return _VirtualList(self._get_num_pages, self._get_page)
return _VirtualList(self._get_num_pages, self._get_page) # type: ignore

@property
def page_layout(self) -> Optional[str]:
Expand Down
2 changes: 1 addition & 1 deletion PyPDF2/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,7 @@ def remove_text(self, ignore_byte_string_object: bool = False) -> None:
pg_dict = cast(DictionaryObject, self.get_object(self._pages))
pages = cast(List[IndirectObject], pg_dict[PA.KIDS])
for page in pages:
page_ref = cast(Dict[str, Any], self.get_object(page))
page_ref = cast(PageObject, self.get_object(page))
content = page_ref["/Contents"].get_object()
if not isinstance(content, ContentStream):
content = ContentStream(content, page_ref)
Expand Down
27 changes: 24 additions & 3 deletions PyPDF2/types.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Helpers for working with PDF types."""

from typing import List, Union
from typing import Any, Dict, List, Optional, Union

try:
# Python 3.8+: https://peps.python.org/pep-0586
from typing import Literal # type: ignore[attr-defined]
from typing import Literal, Protocol # type: ignore[attr-defined]
except ImportError:
from typing_extensions import Literal # type: ignore[misc]
from typing_extensions import Literal, Protocol # type: ignore[misc]

try:
# Python 3.10+: https://www.python.org/dev/peps/pep-0484/
Expand Down Expand Up @@ -54,3 +54,24 @@
"/UseOC",
"/UseAttachments",
]


class PdfReaderProtocol(Protocol): # pragma: no cover
@property
def pdf_header(self) -> str:
...

@property
def strict(self) -> bool:
...

@property
def xref(self) -> Dict[int, Dict[int, Any]]:
...

@property
def pages(self) -> List[Any]:
...

def get_object(self, indirect_reference: Any) -> Optional[Any]:
...