Pydantic: Should SecretStrs be "string equal"?

Created on 4 Dec 2019  路  3Comments  路  Source: samuelcolvin/pydantic

Question

I would have thought that a1 == a2 here:

>>> import pydantic; print(pydantic.VERSION)
1.2
>>> from pydantic import BaseModel, SecretStr
>>> class A(BaseModel):
...     a: SecretStr
... 
>>> a1 = A(a='abcd')
>>> a2 = A(a='abcd')         
>>> a1 == a2
False

Please complete:

  • OS: Linux, Ubuntu
  • Python version: 3.7.3
  • Pydantic version import pydantic; print(pydantic.VERSION): 1.2
Change Feedback Wanted help wanted

Most helpful comment

Okay, I'll accept a PR to fix SecretStr('xxx') == SecretStr('xxx'), let's leave the case of SecretStr('xxx') != 'xxx' for now.

PR welcome.

All 3 comments

Humm, good question. Also, what should happen if you compare SecretStr('xxx') == 'xxx'?

I think it should either work or raise an expressive exception. It should also be documented.

Feedback welcome.

In my use case i have a big structure where I relied on if new == old: return when one of the sub sub items introduced a secret string and then suddenly new was never equal to old anymore.

With a json_encoder for SecretStr (lambda x: x.get_secret_value()) I now tentatively rely on new.json() == old.json()

Okay, I'll accept a PR to fix SecretStr('xxx') == SecretStr('xxx'), let's leave the case of SecretStr('xxx') != 'xxx' for now.

PR welcome.

Was this page helpful?
0 / 5 - 0 ratings