Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't decode bytes (which may not be UTF8) when displaying SecretBytes #8012

Merged
merged 1 commit into from Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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