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

Hashable SecretStr #4387

Closed
3 of 14 tasks
chbndrhnns opened this issue Aug 17, 2022 · 3 comments · Fixed by #4399
Closed
3 of 14 tasks

Hashable SecretStr #4387

chbndrhnns opened this issue Aug 17, 2022 · 3 comments · Fixed by #4399

Comments

@chbndrhnns
Copy link
Contributor

chbndrhnns commented Aug 17, 2022

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

I'd like to see instances of the pydantic class SecretStr to be hashable like instances of the str class are hashable.

I use it mainly in instances of BaseSettings and during startup of a server application, I add settings classes which I can successfully construct in a set. This gives me an indication of missing configuration values.
But models containing fields annotated with SecretStr currently cannot be added to a set as Python complains about the SecretStr type: TypeError: unhashable type: 'SecretStr'

Example snippet:

import pydantic

class M(pydantic.BaseModel):
    s: pydantic.SecretStr = pydantic.SecretStr("secret")

    def __hash__(self):
        return hash((type(self),) + tuple(self.__dict__.values()))

def test_can_hash():
    assert hash(M())

Affected Components

@samuelcolvin
Copy link
Member

please give more details on what you're asking for and how it would be useful.

Closing but happy to reopen if more details are provided.

@chbndrhnns
Copy link
Contributor Author

I updated the description and created a PR.

@samuelcolvin samuelcolvin reopened this Aug 19, 2022
@samuelcolvin
Copy link
Member

Ok, I'll review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants