From f0acf6efe7001ffc8c9b8d8a7229153ae17f151f Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Sat, 2 Apr 2022 14:45:31 +0100 Subject: [PATCH] Prevent subclasses of bytes being converted to bytes (#3707) * adding a test * fix and add change description --- changes/3706-samuelcolvin.md | 1 + pydantic/validators.py | 2 +- tests/test_edge_cases.py | 26 ++++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 changes/3706-samuelcolvin.md diff --git a/changes/3706-samuelcolvin.md b/changes/3706-samuelcolvin.md new file mode 100644 index 0000000..3a22afe --- /dev/null +++ b/changes/3706-samuelcolvin.md @@ -0,0 +1 @@ +Prevent subclasses of bytes being converted to bytes diff --git a/pydantic/validators.py b/pydantic/validators.py index 63b7a59..d4783d9 100644 --- a/pydantic/validators.py +++ b/pydantic/validators.py @@ -76,7 +76,7 @@ def strict_str_validator(v: Any) -> Union[str]: raise errors.StrError() -def bytes_validator(v: Any) -> bytes: +def bytes_validator(v: Any) -> Union[bytes]: if isinstance(v, bytes): return v elif isinstance(v, bytearray): diff --git a/tests/test_edge_cases.py b/tests/test_edge_cases.py index dd07eb3..5da6225 100644 --- a/tests/test_edge_cases.py +++ b/tests/test_edge_cases.py @@ -1906,3 +1906,29 @@ def test_arbitrary_types_allowed_custom_eq(): arbitrary_types_allowed = True assert Model().x == Foo() + + +def test_bytes_subclass(): + class MyModel(BaseModel): + my_bytes: bytes + + class BytesSubclass(bytes): + def __new__(cls, data: bytes): + self = bytes.__new__(cls, data) + return self + + m = MyModel(my_bytes=BytesSubclass(b'foobar')) + assert m.my_bytes.__class__ == BytesSubclass + + +def test_int_subclass(): + class MyModel(BaseModel): + my_int: int + + class IntSubclass(int): + def __new__(cls, data: int): + self = int.__new__(cls, data) + return self + + m = MyModel(my_int=IntSubclass(123)) + assert m.my_int.__class__ == IntSubclass