diff --git a/pydantic/types.py b/pydantic/types.py index 1c4cb93224..62ac1912f3 100644 --- a/pydantic/types.py +++ b/pydantic/types.py @@ -1495,8 +1495,6 @@ def get_json_schema(_core_schema: core_schema.CoreSchema, handler: GetJsonSchema def _secret_display(value: str | bytes) -> str: - if isinstance(value, bytes): - value = value.decode() return '**********' if value else '' diff --git a/tests/test_types.py b/tests/test_types.py index e303117b12..e1bef40600 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -4174,7 +4174,9 @@ class Foobar(BaseModel): empty_password: SecretBytes # Initialize the model. - f = Foobar(password=b'wearebytes', empty_password=b'') + # Use bytes that can't be decoded with UTF8 (https://github.com/pydantic/pydantic/issues/7971) + password = b'\x89PNG\r\n\x1a\n' + f = Foobar(password=password, empty_password=b'') # Assert correct types. assert f.password.__class__.__name__ == 'SecretBytes' @@ -4187,7 +4189,7 @@ class Foobar(BaseModel): assert repr(f.empty_password) == "SecretBytes(b'')" # Assert retrieval of secret value is correct - assert f.password.get_secret_value() == b'wearebytes' + assert f.password.get_secret_value() == password assert f.empty_password.get_secret_value() == b'' # Assert that SecretBytes is equal to SecretBytes if the secret is the same.