Skip to content

Commit 8299131

Browse files
committed
BUG: encode_pdfdocencoding() always returns bytes
In the function encode_pdfdocencoding, cast its return value from bytearray to bytes to match its function signature. This casting is necessary because bytearray is duck type compatible with bytes in mypy, however this library expects only bytes in its Encryption class.
1 parent 3fb63f7 commit 8299131

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pypdf/generic/_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -650,4 +650,4 @@ def encode_pdfdocencoding(unicode_string: str) -> bytes:
650650
raise UnicodeEncodeError(
651651
"pdfdocencoding", c, -1, -1, "does not exist in translation table"
652652
)
653-
return retval
653+
return bytes(retval)

tests/test_generic.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Test the pypdf.generic module."""
2+
23
from io import BytesIO
34
from pathlib import Path
45
from unittest.mock import patch
@@ -273,6 +274,16 @@ def test_encode_pdfdocencoding_keyerror():
273274
assert exc.value.args[0] == "pdfdocencoding"
274275

275276

277+
@pytest.mark.parametrize("test_input", ["", "data"])
278+
def test_encode_pdfdocencoding_returns_bytes(test_input):
279+
"""
280+
Test that encode_pdfdocencoding() always returns bytes because bytearray
281+
is duck type compatible with bytes in mypy
282+
"""
283+
out = encode_pdfdocencoding(test_input)
284+
assert isinstance(out, bytes)
285+
286+
276287
def test_read_object_comment_exception():
277288
stream = BytesIO(b"% foobar")
278289
pdf = None

0 commit comments

Comments
 (0)