-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Closed
Labels
Description
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
- Compatibility between releases
- Data validation/parsing
- Data serialization -
.dict()and.json() - JSON Schema
- Dataclasses
- Model Config
- Field Types - adding or changing a particular data type
- Function validation decorator
- Generic Models
- Other Model behaviour -
construct(), pickling, private attributes, ORM mode - Settings Management
- Plugins and integration with other tools - mypy, FastAPI, python-devtools, Hypothesis, VS Code, PyCharm, etc.