Skip to content

Commit 4cb9ca8

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 7dad499 commit 4cb9ca8

File tree

2 files changed

+39
-39
lines changed

2 files changed

+39
-39
lines changed

src/marshmallow/fields.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -879,41 +879,41 @@ def _deserialize(self, value, attr, data, **kwargs) -> str:
879879

880880

881881
class Bytes(Field[bytes]):
882-
"""
883-
Marshmallow field type for any bytes array.
884-
"""
885-
886-
def _deserialize(
887-
self,
888-
value: typing.Any,
889-
attr: str | None,
890-
data: typing.Mapping[str, typing.Any] | None,
891-
**kwargs: typing.Any,
892-
) -> bytes:
893-
try:
894-
match value:
895-
case bytes() as b:
896-
return b
897-
case bytearray() as ba:
898-
return bytes(ba)
899-
case str() as s:
900-
return bytes(
901-
s,
902-
encoding="utf-8",
903-
errors="ignore",
904-
)
905-
case int() as i:
906-
return i.to_bytes(
907-
length=max(1, (7 + i.bit_length()) // 8),
908-
byteorder="big",
909-
signed=i < 0,
910-
)
911-
case obj:
912-
if isinstance(obj, (typing.SupportsBytes, typing.Iterable)):
913-
return bytes(obj)
914-
raise ValidationError("not a bytes-like object")
915-
except TypeError as e:
916-
raise ValidationError("not a bytes-like object") from e
882+
"""
883+
Marshmallow field type for any bytes array.
884+
"""
885+
886+
def _deserialize(
887+
self,
888+
value: typing.Any,
889+
attr: str | None,
890+
data: typing.Mapping[str, typing.Any] | None,
891+
**kwargs: typing.Any,
892+
) -> bytes:
893+
try:
894+
match value:
895+
case bytes() as b:
896+
return b
897+
case bytearray() as ba:
898+
return bytes(ba)
899+
case str() as s:
900+
return bytes(
901+
s,
902+
encoding="utf-8",
903+
errors="ignore",
904+
)
905+
case int() as i:
906+
return i.to_bytes(
907+
length=max(1, (7 + i.bit_length()) // 8),
908+
byteorder="big",
909+
signed=i < 0,
910+
)
911+
case obj:
912+
if isinstance(obj, (typing.SupportsBytes, typing.Iterable)):
913+
return bytes(obj)
914+
raise ValidationError("not a bytes-like object")
915+
except TypeError as e:
916+
raise ValidationError("not a bytes-like object") from e
917917

918918

919919
class UUID(Field[uuid.UUID]):

tests/test_deserialization.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,16 +327,16 @@ def test_bytes_field_deserialization(self):
327327
assert field.deserialize(b"foo") == b"foo"
328328
assert field.deserialize(bytearray(b"foo")) == b"foo"
329329
assert field.deserialize("foo") == b"foo"
330-
assert field.deserialize(0xDEAD) == b"\xDE\xAD"
331-
assert field.deserialize([0xBE, 0xEF]) == b"\xBE\xEF"
332-
assert field.deserialize((0xB, 0xA, 0xB, 0xE)) == b"\x0B\x0A\x0B\x0E"
330+
assert field.deserialize(0xDEAD) == b"\xde\xad"
331+
assert field.deserialize([0xBE, 0xEF]) == b"\xbe\xef"
332+
assert field.deserialize((0xB, 0xA, 0xB, 0xE)) == b"\x0b\x0a\x0b\x0e"
333333

334334
with pytest.raises(ValidationError) as excinfo:
335335
print(field.deserialize({"hi": 222}))
336336
assert excinfo.value.args[0] == "not a bytes-like object"
337337

338338
with pytest.raises(ValidationError) as excinfo:
339-
field.deserialize(['12345'])
339+
field.deserialize(["12345"])
340340
assert excinfo.value.args[0] == "not a bytes-like object"
341341

342342
def test_boolean_field_deserialization(self):

0 commit comments

Comments
 (0)