mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
Let SecretStr/SecretBytes be value equals to them selves (#1079)
fix #1078
This commit is contained in:
committed by
Samuel Colvin
parent
4318fc183e
commit
33fee6d9be
@@ -0,0 +1 @@
|
||||
Add `__eq__` to SecretStr and SecretBytes to allow "value equals"
|
||||
@@ -477,6 +477,9 @@ class SecretStr:
|
||||
def __str__(self) -> str:
|
||||
return '**********' if self._secret_value else ''
|
||||
|
||||
def __eq__(self, other: Any) -> bool:
|
||||
return isinstance(other, SecretStr) and self.get_secret_value() == other.get_secret_value()
|
||||
|
||||
def display(self) -> str:
|
||||
warnings.warn('`secret_str.display()` is deprecated, use `str(secret_str)` instead', DeprecationWarning)
|
||||
return str(self)
|
||||
@@ -504,6 +507,9 @@ class SecretBytes:
|
||||
def __str__(self) -> str:
|
||||
return '**********' if self._secret_value else ''
|
||||
|
||||
def __eq__(self, other: Any) -> bool:
|
||||
return isinstance(other, SecretBytes) and self.get_secret_value() == other.get_secret_value()
|
||||
|
||||
def display(self) -> str:
|
||||
warnings.warn('`secret_bytes.display()` is deprecated, use `str(secret_bytes)` instead', DeprecationWarning)
|
||||
return str(self)
|
||||
|
||||
@@ -1699,6 +1699,17 @@ def test_secretstr():
|
||||
with pytest.warns(DeprecationWarning, match=r'`secret_str.display\(\)` is deprecated'):
|
||||
assert f.empty_password.display() == ''
|
||||
|
||||
# Assert that SecretStr is equal to SecretStr if the secret is the same.
|
||||
assert f == f.copy()
|
||||
assert f != f.copy(update=dict(password='4321'))
|
||||
|
||||
|
||||
def test_secretstr_equality():
|
||||
assert SecretStr('abc') == SecretStr('abc')
|
||||
assert SecretStr('123') != SecretStr('321')
|
||||
assert SecretStr('123') != '123'
|
||||
assert SecretStr('123') is not SecretStr('123')
|
||||
|
||||
|
||||
def test_secretstr_error():
|
||||
class Foobar(BaseModel):
|
||||
@@ -1736,6 +1747,17 @@ def test_secretbytes():
|
||||
with pytest.warns(DeprecationWarning, match=r'`secret_bytes.display\(\)` is deprecated'):
|
||||
assert f.empty_password.display() == ''
|
||||
|
||||
# Assert that SecretBytes is equal to SecretBytes if the secret is the same.
|
||||
assert f == f.copy()
|
||||
assert f != f.copy(update=dict(password=b'4321'))
|
||||
|
||||
|
||||
def test_secretbytes_equality():
|
||||
assert SecretBytes(b'abc') == SecretBytes(b'abc')
|
||||
assert SecretBytes(b'123') != SecretBytes(b'321')
|
||||
assert SecretBytes(b'123') != b'123'
|
||||
assert SecretBytes(b'123') is not SecretBytes(b'123')
|
||||
|
||||
|
||||
def test_secretbytes_error():
|
||||
class Foobar(BaseModel):
|
||||
|
||||
Reference in New Issue
Block a user