Skip to content

Commit

Permalink
BUG: Fix missing return
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed May 8, 2022
1 parent b4581a8 commit a661f10
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
40 changes: 27 additions & 13 deletions PyPDF2/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@
import math
import uuid
from decimal import Decimal
from typing import Any, Dict, Iterable, List, Optional, Tuple, Union
from typing import (
Any,
Callable,
Dict,
Iterable,
List,
Optional,
Tuple,
Union,
cast,
)

from PyPDF2 import utils
from PyPDF2.constants import PageAttributes as PG
Expand Down Expand Up @@ -101,7 +111,9 @@ class PageObject(DictionaryObject):
"""

def __init__(
self, pdf: Optional[Any] = None, indirectRef: Optional[IndirectObject] = None
self,
pdf: Optional[Any] = None, # PdfFileReader
indirectRef: Optional[IndirectObject] = None,
) -> None:
from PyPDF2._reader import PdfFileReader

Expand Down Expand Up @@ -183,11 +195,13 @@ def _rotate(self, angle: float) -> None:

@staticmethod
def _mergeResources(
res1: Any, res2: Any, resource: Any
res1: DictionaryObject, res2: DictionaryObject, resource: Any
) -> Tuple[Dict[str, Any], Dict[str, Any]]:
new_res = DictionaryObject()
new_res.update(res1.get(resource, DictionaryObject()).getObject())
page2res = res2.get(resource, DictionaryObject()).getObject()
page2res = cast(
DictionaryObject, res2.get(resource, DictionaryObject()).getObject()
)
rename_res = {}
for key in list(page2res.keys()):
if key in new_res and new_res.raw_get(key) != page2res.raw_get(key):
Expand Down Expand Up @@ -254,15 +268,15 @@ def _addTransformationMatrix(
)
return contents

def getContents(self) -> Optional[Any]:
def getContents(self) -> Optional[ContentStream]:
"""
Access the page contents.
:return: the ``/Contents`` object, or ``None`` if it doesn't exist.
``/Contents`` is optional, as described in PDF Reference 7.7.3.3
"""
if PG.CONTENTS in self:
return self[PG.CONTENTS].getObject()
return self[PG.CONTENTS].getObject() # type: ignore
else:
return None

Expand All @@ -284,7 +298,7 @@ def mergePage(self, page2: "PageObject") -> None:
def _mergePage(
self,
page2: "PageObject",
page2transformation: Optional[Any] = None,
page2transformation: Optional[Callable[[Any], ContentStream]] = None,
ctm: Optional[Iterable[float]] = None,
expand: bool = False,
) -> None:
Expand All @@ -294,8 +308,8 @@ def _mergePage(

new_resources = DictionaryObject()
rename = {}
original_resources = self[PG.RESOURCES].getObject()
page2resources = page2[PG.RESOURCES].getObject()
original_resources = cast(DictionaryObject, self[PG.RESOURCES].getObject())
page2resources = cast(DictionaryObject, page2[PG.RESOURCES].getObject())
new_annots = ArrayObject()

for page in (self, page2):
Expand Down Expand Up @@ -341,7 +355,7 @@ def _mergePage(
page2content = ContentStream(page2content, self.pdf)
page2content.operations.insert(
0,
[
(
map(
FloatObject,
[
Expand All @@ -352,10 +366,10 @@ def _mergePage(
],
),
"re",
],
),
)
page2content.operations.insert(1, [[], "W"])
page2content.operations.insert(2, [[], "n"])
page2content.operations.insert(1, ([], "W"))
page2content.operations.insert(2, ([], "n"))
if page2transformation is not None:
page2content = page2transformation(page2content)
page2content = PageObject._contentStreamRename(
Expand Down
4 changes: 2 additions & 2 deletions PyPDF2/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def namedDestinations(self) -> Dict[str, Any]:
def getFields(
self,
tree: Optional[TreeObject] = None,
retval: Optional[Any] = None,
retval: Optional[Dict[Any, Any]] = None,
fileobj: Optional[Any] = None,
) -> Optional[Dict[str, Any]]:
"""
Expand Down Expand Up @@ -429,7 +429,7 @@ def getFields(
def _buildField(
self,
field: Union[TreeObject, DictionaryObject],
retval: Any,
retval: Dict[Any, Any],
fileobj: Any,
fieldAttributes: Any,
) -> None:
Expand Down
36 changes: 19 additions & 17 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, Iterable, List, Optional, Tuple, Union
from typing import Any, Dict, Iterable, List, Optional, Tuple, Union, cast

from PyPDF2.constants import FilterTypes as FT
from PyPDF2.constants import StreamAttributes as SA
Expand Down Expand Up @@ -177,8 +177,8 @@ def writeToStream(

@staticmethod
def readFromStream(
stream: StreamType, pdf: Any
) -> "IndirectObject": # PdfFileReader
stream: StreamType, pdf: Any # PdfFileReader
) -> "IndirectObject":
idnum = b_("")
while True:
tok = stream.read(1)
Expand Down Expand Up @@ -548,8 +548,8 @@ def writeToStream(

@staticmethod
def readFromStream(
stream: StreamType, pdf: Any
) -> "DictionaryObject": # PdfFileReader
stream: StreamType, pdf: Any # PdfFileReader
) -> "DictionaryObject":
def getNextObjPos(
p: int, p1: int, remGens: List[int], pdf: Any
) -> int: # PdfFileReader
Expand All @@ -563,8 +563,8 @@ def getNextObjPos(
return getNextObjPos(p, p1, remGens[1:], pdf)

def readUnsizedFromSteam(
stream: StreamType, pdf: Any
) -> bytes: # PdfFileReader
stream: StreamType, pdf: Any # PdfFileReader
) -> bytes:
# we are just pointing at beginning of the stream
eon = getNextObjPos(stream.tell(), 2**32, [g for g in pdf.xref], pdf) - 1
curr = stream.tell()
Expand Down Expand Up @@ -1042,8 +1042,8 @@ def _data(self, value: Union[str, bytes]) -> None:


def readObject(
stream: StreamType, pdf: Any
) -> Union[PdfObject, int, str, ContentStream]: # PdfFileReader
stream: StreamType, pdf: Any # PdfFileReader
) -> Union[PdfObject, int, str, ContentStream]:
tok = stream.read(1)
stream.seek(-1, 1) # reset to start
idx = ObjectPrefix.find(tok)
Expand Down Expand Up @@ -1225,18 +1225,20 @@ def __init__(self, data: Dict[str, Any]) -> None:
except KeyError:
pass

# TABLE 8.69 Entries common to all field dictionaries

@property
def fieldType(self) -> Optional[Any]:
def fieldType(self) -> Optional[NameObject]:
"""Read-only property accessing the type of this field."""
return self.get("/FT")

@property
def parent(self) -> Optional[Any]:
def parent(self) -> Optional[DictionaryObject]:
"""Read-only property accessing the parent of this field."""
return self.get("/Parent")

@property
def kids(self) -> Optional[Any]:
def kids(self) -> Optional[ArrayObject]:
"""Read-only property accessing the kids of this field."""
return self.get("/Kids")

Expand All @@ -1246,12 +1248,12 @@ def name(self) -> Optional[str]:
return self.get("/T")

@property
def altName(self) -> Optional[Any]:
def altName(self) -> Optional[str]:
"""Read-only property accessing the alternate name of this field."""
return self.get("/TU")

@property
def mappingName(self) -> Optional[Any]:
def mappingName(self) -> Optional[str]:
"""
Read-only property accessing the mapping name of this field. This
name is used by PyPDF2 as a key in the dictionary returned by
Expand All @@ -1260,7 +1262,7 @@ def mappingName(self) -> Optional[Any]:
return self.get("/TM")

@property
def flags(self) -> Optional[Any]:
def flags(self) -> Optional[int]:
"""
Read-only property accessing the field flags, specifying various
characteristics of the field (see Table 8.70 of the PDF 1.7 reference).
Expand All @@ -1281,13 +1283,13 @@ def defaultValue(self) -> Optional[Any]:
return self.get("/DV")

@property
def additionalActions(self) -> None:
def additionalActions(self) -> Optional[DictionaryObject]:
"""
Read-only property accessing the additional actions dictionary.
This dictionary defines the field's behavior in response to trigger events.
See Section 8.5.2 of the PDF 1.7 reference.
"""
self.get("/AA")
return self.get("/AA")


class Destination(TreeObject):
Expand Down

0 comments on commit a661f10

Please sign in to comment.