Skip to content

Commit

Permalink
Don't decode bytes (which may not be UTF8) when displaying SecretBytes (
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmojaki committed Nov 6, 2023
1 parent b7d9281 commit 833faaf
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 0 additions & 2 deletions pydantic/types.py
Expand Up @@ -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 ''


Expand Down
6 changes: 4 additions & 2 deletions tests/test_types.py
Expand Up @@ -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'
Expand All @@ -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.
Expand Down

0 comments on commit 833faaf

Please sign in to comment.