Skip to content

Commit db3439b

Browse files
authored
MAINT: Package updates; solve mypy strict remarks (#1163)
1 parent ec30171 commit db3439b

File tree

15 files changed

+47
-41
lines changed

15 files changed

+47
-41
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ repos:
3030
hooks:
3131
- id: isort
3232
- repo: https://github.com/psf/black
33-
rev: 22.3.0
33+
rev: 22.6.0
3434
hooks:
3535
- id: black
3636
args: [--target-version, py36]
@@ -40,7 +40,7 @@ repos:
4040
- id: blacken-docs
4141
additional_dependencies: [black==22.1.0]
4242
- repo: https://github.com/asottile/pyupgrade
43-
rev: v2.34.0
43+
rev: v2.37.2
4444
hooks:
4545
- id: pyupgrade
4646
args: [--py36-plus]

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ mutation-results:
3131

3232
benchmark:
3333
pytest tests/bench.py
34+
35+
mypy:
36+
mypy PyPDF2 --ignore-missing-imports --check-untyped --strict

PyPDF2/_cmap.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ def build_char_map(
5656
float(sp_width / 2),
5757
encoding,
5858
# https://github.com/python/mypy/issues/4374
59-
map_dict, # type: ignore
60-
) # type: ignore
59+
map_dict,
60+
)
6161

6262

6363
# used when missing data, e.g. font def missing
64-
unknown_char_map: Tuple[str, float, Union[str, Dict[int, str]], Dict] = (
64+
unknown_char_map: Tuple[str, float, Union[str, Dict[int, str]], Dict[Any, Any]] = (
6565
"Unknown",
6666
9999,
6767
dict(zip(range(256), ["�"] * 256)),
@@ -108,15 +108,15 @@ def parse_encoding(
108108
encoding: Union[str, List[str], Dict[int, str]] = []
109109
if "/Encoding" not in ft:
110110
try:
111-
if "/BaseFont" in ft and ft["/BaseFont"] in charset_encoding:
111+
if "/BaseFont" in ft and cast(str, ft["/BaseFont"]) in charset_encoding:
112112
encoding = dict(
113113
zip(range(256), charset_encoding[cast(str, ft["/BaseFont"])])
114114
)
115115
else:
116116
encoding = "charmap"
117117
return encoding, _default_fonts_space_width[cast(str, ft["/BaseFont"])]
118118
except Exception:
119-
if ft["/Subtype"] == "/Type1":
119+
if cast(str, ft["/Subtype"]) == "/Type1":
120120
return "charmap", space_code
121121
else:
122122
return "", space_code
@@ -314,7 +314,7 @@ def compute_space_width(
314314
except Exception:
315315
w1[-1] = 1000.0
316316
if "/W" in ft1:
317-
w = list(ft1["/W"]) # type: ignore
317+
w = list(ft1["/W"])
318318
else:
319319
w = []
320320
while len(w) > 0:

PyPDF2/_encryption.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
import random
3030
import struct
3131
from enum import IntEnum
32-
from typing import Optional, Tuple, Union, cast
32+
from typing import Any, Dict, Optional, Tuple, Union, cast
3333

34-
from PyPDF2.errors import DependencyError
3534
from PyPDF2._utils import logger_warning
35+
from PyPDF2.errors import DependencyError
3636
from PyPDF2.generic import (
3737
ArrayObject,
3838
ByteStringObject,
@@ -566,7 +566,7 @@ def verify_perms(
566566
@staticmethod
567567
def generate_values(
568568
user_pwd: bytes, owner_pwd: bytes, key: bytes, p: int, metadata_encrypted: bool
569-
) -> dict:
569+
) -> Dict[Any, Any]:
570570
u_value, ue_value = AlgV5.compute_U_value(user_pwd, key)
571571
o_value, oe_value = AlgV5.compute_O_value(owner_pwd, key, u_value)
572572
perms = AlgV5.compute_Perms_value(key, p, metadata_encrypted)

PyPDF2/_merger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ def find_bookmark(
556556
res = self.find_bookmark(bookmark, b) # type: ignore
557557
if res:
558558
return [i] + res
559-
elif b == bookmark or b["/Title"] == bookmark:
559+
elif b == bookmark or cast(Dict[Any, Any], b["/Title"]) == bookmark:
560560
# we found a leaf node
561561
return [i]
562562

PyPDF2/_page.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,9 @@ def _merge_page(
506506
# Combine /ProcSet sets.
507507
new_resources[NameObject(RES.PROC_SET)] = ArrayObject(
508508
frozenset(
509-
original_resources.get(RES.PROC_SET, ArrayObject()).get_object() # type: ignore
509+
original_resources.get(RES.PROC_SET, ArrayObject()).get_object()
510510
).union(
511-
frozenset(page2resources.get(RES.PROC_SET, ArrayObject()).get_object()) # type: ignore
511+
frozenset(page2resources.get(RES.PROC_SET, ArrayObject()).get_object())
512512
)
513513
)
514514

@@ -1248,7 +1248,7 @@ def process_operation(operator: bytes, operands: List) -> None:
12481248
cmaps[operands[0]][2],
12491249
cmaps[operands[0]][3],
12501250
operands[0],
1251-
) # type:ignore
1251+
)
12521252
except KeyError: # font not found
12531253
_space_width = unknown_char_map[1]
12541254
cmap = (
@@ -1395,7 +1395,7 @@ def process_operation(operator: bytes, operands: List) -> None:
13951395
except IndexError:
13961396
pass
13971397
try:
1398-
xobj = resources_dict["/XObject"] # type: ignore
1398+
xobj = resources_dict["/XObject"]
13991399
if xobj[operands[0]]["/Subtype"] != "/Image": # type: ignore
14001400
# output += text
14011401
text = self.extract_xform_text(xobj[operands[0]], space_width) # type: ignore

PyPDF2/_reader.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,9 @@ def _build_outline(self, node: DictionaryObject) -> Optional[Destination]:
861861
elif isinstance(dest, str):
862862
# named destination, addresses NameObject Issue #193
863863
try:
864-
outline = self._build_destination(title, self._namedDests[dest].dest_array) # type: ignore
864+
outline = self._build_destination(
865+
title, self._namedDests[dest].dest_array
866+
)
865867
except KeyError:
866868
# named destination not found in Name Dict
867869
outline = self._build_destination(title, None)
@@ -1045,7 +1047,7 @@ def _get_object_from_stream(
10451047
stmnum, idx = self.xref_objStm[indirect_reference.idnum]
10461048
obj_stm: EncodedStreamObject = IndirectObject(stmnum, 0, self).get_object() # type: ignore
10471049
# This is an xref to a stream, so its type better be a stream
1048-
assert obj_stm["/Type"] == "/ObjStm"
1050+
assert cast(str, obj_stm["/Type"]) == "/ObjStm"
10491051
# /N is the number of indirect objects in the stream
10501052
assert idx < obj_stm["/N"]
10511053
stream_data = BytesIO(b_(obj_stm.get_data())) # type: ignore
@@ -1501,7 +1503,7 @@ def _read_pdf15_xref_stream(
15011503
stream.seek(-1, 1)
15021504
idnum, generation = self.read_object_header(stream)
15031505
xrefstream = cast(ContentStream, read_object(stream, self))
1504-
assert xrefstream["/Type"] == "/XRef"
1506+
assert cast(str, xrefstream["/Type"]) == "/XRef"
15051507
self.cache_indirect_object(generation, idnum, xrefstream)
15061508
stream_data = BytesIO(b_(xrefstream.get_data()))
15071509
# Index pairs specify the subsections in the dictionary. If

PyPDF2/_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
# Python 3.10+: https://www.python.org/dev/peps/pep-0484/
4747
from typing import TypeAlias # type: ignore[attr-defined]
4848
except ImportError:
49-
from typing_extensions import TypeAlias # type: ignore[misc]
49+
from typing_extensions import TypeAlias
5050

5151
from .errors import STREAM_TRUNCATED_PREMATURELY, PdfStreamError
5252

@@ -130,7 +130,7 @@ def skip_over_comment(stream: StreamType) -> None:
130130

131131

132132
def read_until_regex(
133-
stream: StreamType, regex: Pattern, ignore_eof: bool = False
133+
stream: StreamType, regex: Pattern[bytes], ignore_eof: bool = False
134134
) -> bytes:
135135
"""
136136
Read until the regular expression pattern matched (ignore the match).

PyPDF2/_writer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def getObject(self, ido: IndirectObject) -> PdfObject: # pragma: no cover
195195
def _add_page(
196196
self, page: PageObject, action: Callable[[Any, IndirectObject], None]
197197
) -> None:
198-
assert page[PA.TYPE] == CO.PAGE
198+
assert cast(str, page[PA.TYPE]) == CO.PAGE
199199
if page.pdf is not None:
200200
other = page.pdf.pdf_header
201201
if isinstance(other, str):
@@ -292,7 +292,7 @@ def get_page(
292292
raise ValueError("Please specify the page_number")
293293
pages = cast(Dict[str, Any], self.get_object(self._pages))
294294
# TODO: crude hack
295-
return pages[PA.KIDS][page_number].get_object()
295+
return cast(PageObject, pages[PA.KIDS][page_number].get_object())
296296

297297
def getPage(self, pageNumber: int) -> PageObject: # pragma: no cover
298298
"""

PyPDF2/generic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def readFromStream(stream: StreamType) -> "BooleanObject": # pragma: no cover
192192

193193

194194
class ArrayObject(list, PdfObject):
195-
def items(self) -> Iterable:
195+
def items(self) -> Iterable[Any]:
196196
"""
197197
Emulate DictionaryObject.items for a list
198198
(index, object)
@@ -517,7 +517,7 @@ def read_string_from_stream(
517517
return create_string_object(txt, forced_encoding)
518518

519519

520-
class ByteStringObject(bytes, PdfObject): # type: ignore
520+
class ByteStringObject(bytes, PdfObject):
521521
"""
522522
Represents a string object where the text encoding could not be determined.
523523
This occurs quite often, as the PDF spec doesn't provide an alternate way to

0 commit comments

Comments
 (0)