Skip to content

Commit dd39992

Browse files
authored
STY: Capitalize error messages (#2903)
1 parent 482a484 commit dd39992

File tree

15 files changed

+42
-42
lines changed

15 files changed

+42
-42
lines changed

pypdf/_doc_common.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393

9494
def convert_to_int(d: bytes, size: int) -> Union[int, Tuple[Any, ...]]:
9595
if size > 8:
96-
raise PdfReadError("invalid size in convert_to_int")
96+
raise PdfReadError("Invalid size in convert_to_int")
9797
d = b"\x00\x00\x00\x00\x00\x00\x00\x00" + d
9898
d = d[-8:]
9999
return struct.unpack(">q", d)[0]
@@ -761,9 +761,9 @@ def _get_inherited(obj: DictionaryObject, key: str) -> Any:
761761
# to cope with all types
762762
field = cast(DictionaryObject, field.indirect_reference.get_object()) # type: ignore
763763
except Exception as exc:
764-
raise ValueError("field type is invalid") from exc
764+
raise ValueError("Field type is invalid") from exc
765765
if is_null_or_none(_get_inherited(field, "/FT")):
766-
raise ValueError("field is not valid")
766+
raise ValueError("Field is not valid")
767767
ret = []
768768
if field.get("/Subtype", "") == "/Widget":
769769
if "/P" in field:
@@ -827,7 +827,7 @@ def open_destination(
827827

828828
@open_destination.setter
829829
def open_destination(self, dest: Union[None, str, Destination, PageObject]) -> None:
830-
raise NotImplementedError("no setter for open_destination")
830+
raise NotImplementedError("No setter for open_destination")
831831

832832
@property
833833
def outline(self) -> OutlineType:

pypdf/_page.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,13 +455,13 @@ def __getitem__(
455455
if isinstance(index, (str, list, tuple)):
456456
return self.get_function(index)
457457
if not isinstance(index, int):
458-
raise TypeError("invalid sequence indices type")
458+
raise TypeError("Invalid sequence indices type")
459459
len_self = len(lst)
460460
if index < 0:
461461
# support negative indexes
462462
index = len_self + index
463463
if index < 0 or index >= len_self:
464-
raise IndexError("sequence index out of range")
464+
raise IndexError("Sequence index out of range")
465465
return self.get_function(lst[index])
466466

467467
def __iter__(self) -> Iterator[ImageFile]:
@@ -641,7 +641,7 @@ def _get_image(
641641
if self.inline_images is None:
642642
self.inline_images = self._get_inline_images()
643643
if self.inline_images is None: # pragma: no cover
644-
raise KeyError("no inline image can be found")
644+
raise KeyError("No inline image can be found")
645645
return self.inline_images[id]
646646

647647
imgd = _xobj_to_image(cast(DictionaryObject, xobjs[id]))
@@ -986,7 +986,7 @@ def _content_stream_rename(
986986
if isinstance(op, NameObject):
987987
operands[i] = rename.get(op, op)
988988
else:
989-
raise KeyError(f"type of operands is {type(operands)}")
989+
raise KeyError(f"Type of operands is {type(operands)}")
990990
return stream
991991

992992
@staticmethod
@@ -2498,13 +2498,13 @@ def __getitem__(
24982498
cls = type(self)
24992499
return cls(indices.__len__, lambda idx: self[indices[idx]])
25002500
if not isinstance(index, int):
2501-
raise TypeError("sequence indices must be integers")
2501+
raise TypeError("Sequence indices must be integers")
25022502
len_self = len(self)
25032503
if index < 0:
25042504
# support negative indexes
25052505
index = len_self + index
25062506
if index < 0 or index >= len_self:
2507-
raise IndexError("sequence index out of range")
2507+
raise IndexError("Sequence index out of range")
25082508
return self.get_function(index)
25092509

25102510
def __delitem__(self, index: Union[int, slice]) -> None:
@@ -2517,13 +2517,13 @@ def __delitem__(self, index: Union[int, slice]) -> None:
25172517
del self[p] # recursive call
25182518
return
25192519
if not isinstance(index, int):
2520-
raise TypeError("index must be integers")
2520+
raise TypeError("Index must be integers")
25212521
len_self = len(self)
25222522
if index < 0:
25232523
# support negative indexes
25242524
index = len_self + index
25252525
if index < 0 or index >= len_self:
2526-
raise IndexError("index out of range")
2526+
raise IndexError("Index out of range")
25272527
ind = self[index].indirect_reference
25282528
assert ind is not None
25292529
parent: Optional[PdfObject] = cast(DictionaryObject, ind.get_object()).get(

pypdf/_page_labels.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def nums_insert(
223223
nums: Nums array to modify
224224
"""
225225
if len(nums) % 2 != 0:
226-
raise ValueError("a nums like array must have an even number of elements")
226+
raise ValueError("A nums like array must have an even number of elements")
227227

228228
i = len(nums)
229229
while i != 0 and key <= nums[i - 2]:
@@ -252,7 +252,7 @@ def nums_clear_range(
252252
nums: Nums array to modify
253253
"""
254254
if len(nums) % 2 != 0:
255-
raise ValueError("a nums like array must have an even number of elements")
255+
raise ValueError("A nums like array must have an even number of elements")
256256
if page_index_to < key:
257257
raise ValueError("page_index_to must be greater or equal than key")
258258

@@ -276,7 +276,7 @@ def nums_next(
276276
nums: Nums array
277277
"""
278278
if len(nums) % 2 != 0:
279-
raise ValueError("a nums like array must have an even number of elements")
279+
raise ValueError("A nums like array must have an even number of elements")
280280

281281
i = nums.index(key) + 2
282282
if i < len(nums):

pypdf/_reader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ def get_object(
428428
idnum != indirect_reference.idnum
429429
or generation != indirect_reference.generation
430430
):
431-
raise PdfReadError("not matching, we parse the file for it")
431+
raise PdfReadError("Not matching, we parse the file for it")
432432
except Exception:
433433
if hasattr(self.stream, "getbuffer"):
434434
buf = bytes(self.stream.getbuffer())
@@ -972,7 +972,7 @@ def _read_xref_other_error(
972972
self._rebuild_xref_table(stream)
973973
return None
974974
except Exception:
975-
raise PdfReadError("can not rebuild xref")
975+
raise PdfReadError("Cannot rebuild xref")
976976
raise PdfReadError("Could not find xref table at specified location")
977977

978978
def _read_pdf15_xref_stream(

pypdf/_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def _get_max_pdf_version_header(header1: str, header2: str) -> str:
120120
if header2 in versions:
121121
pdf_header_indices.append(versions.index(header2))
122122
if len(pdf_header_indices) == 0:
123-
raise ValueError(f"neither {header1!r} nor {header2!r} are proper headers")
123+
raise ValueError(f"Neither {header1!r} nor {header2!r} are proper headers")
124124
return versions[max(pdf_header_indices)]
125125

126126

pypdf/_writer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ def _replace_object(
453453
) -> PdfObject:
454454
if isinstance(indirect_reference, IndirectObject):
455455
if indirect_reference.pdf != self:
456-
raise ValueError("pdf must be self")
456+
raise ValueError("PDF must be self")
457457
indirect_reference = indirect_reference.idnum
458458
gen = self._objects[indirect_reference - 1].indirect_reference.generation # type: ignore
459459
if (
@@ -1325,7 +1325,7 @@ def encrypt(
13251325
try:
13261326
alg = getattr(EncryptAlgorithm, algorithm.replace("-", "_"))
13271327
except AttributeError:
1328-
raise ValueError(f"algorithm '{algorithm}' NOT supported")
1328+
raise ValueError(f"Algorithm '{algorithm}' NOT supported")
13291329
else:
13301330
alg = EncryptAlgorithm.RC4_128
13311331
if not use_128bit:
@@ -1384,7 +1384,7 @@ def write(self, stream: Union[Path, StrByteType]) -> Tuple[bool, IO[Any]]:
13841384
my_file = False
13851385

13861386
if stream == "":
1387-
raise ValueError(f"Output(stream={stream}) is empty.")
1387+
raise ValueError(f"Output({stream=}) is empty.")
13881388

13891389
if isinstance(stream, (str, Path)):
13901390
stream = FileIO(stream, "wb")

pypdf/filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def decode(
351351
index += 1
352352
if length == 128:
353353
if index < len(data):
354-
raise PdfStreamError("early EOD in RunLengthDecode")
354+
raise PdfStreamError("Early EOD in RunLengthDecode")
355355
else:
356356
break
357357
elif length < 128:

pypdf/generic/_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ def unnumber(sin: bytes) -> bytes:
832832
def read_from_stream(stream: StreamType, pdf: Any) -> "NameObject": # PdfReader
833833
name = stream.read(1)
834834
if name != NameObject.surfix:
835-
raise PdfReadError("name read error")
835+
raise PdfReadError("Name read error")
836836
name += read_until_regex(stream, NameObject.delimiter_pattern)
837837
try:
838838
# Name objects should represent irregular characters

pypdf/generic/_data_structures.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -447,24 +447,24 @@ def get_inherited(self, key: str, default: Any = None) -> Any:
447447
try:
448448
if "/Parent" not in self:
449449
return default
450-
raise KeyError("not present")
450+
raise KeyError("Not present")
451451
except KeyError:
452452
return cast("DictionaryObject", self["/Parent"].get_object()).get_inherited(
453453
key, default
454454
)
455455

456456
def __setitem__(self, key: Any, value: Any) -> Any:
457457
if not isinstance(key, PdfObject):
458-
raise ValueError("key must be PdfObject")
458+
raise ValueError("Key must be a PdfObject")
459459
if not isinstance(value, PdfObject):
460-
raise ValueError("value must be PdfObject")
460+
raise ValueError("Value must be a PdfObject")
461461
return dict.__setitem__(self, key, value)
462462

463463
def setdefault(self, key: Any, value: Optional[Any] = None) -> Any:
464464
if not isinstance(key, PdfObject):
465-
raise ValueError("key must be PdfObject")
465+
raise ValueError("Key must be a PdfObject")
466466
if not isinstance(value, PdfObject):
467-
raise ValueError("value must be PdfObject")
467+
raise ValueError("Value must be a PdfObject")
468468
return dict.setdefault(self, key, value) # type: ignore
469469

470470
def __getitem__(self, key: Any) -> PdfObject:
@@ -1109,7 +1109,7 @@ def set_data(self, data: bytes) -> None:
11091109

11101110
if self.get(SA.FILTER, "") in (FT.FLATE_DECODE, [FT.FLATE_DECODE]):
11111111
if not isinstance(data, bytes):
1112-
raise TypeError("data must be bytes")
1112+
raise TypeError("Data must be bytes")
11131113
if self.decoded_self is None:
11141114
self.get_data() # to create self.decoded_self
11151115
assert self.decoded_self is not None, "mypy"

tests/test_encryption.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def test_pdf_encrypt(pdf_file_path, alg, requires_aes):
264264
owner_password=owner_password,
265265
algorithm=alg,
266266
)
267-
assert exc.value.args[0] == "algorithm 'ABCD' NOT supported"
267+
assert exc.value.args[0] == "Algorithm 'ABCD' NOT supported"
268268
return
269269

270270
if requires_aes and not HAS_AES:

0 commit comments

Comments
 (0)