-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Generate a hash function when allow_mutation
is False
#1880
Comments
The generated hash is the hash of the dict_value of the BaseModel.__dict__ attribute. closes pydantic#1880
The generated hash is the hash of the dict_value of the BaseModel.__dict__ attribute. closes pydantic#1880
The generated hash is the hash of the dict_value of the BaseModel.__dict__ attribute. closes pydantic#1880
@rhuille @samuelcolvin I believe that the introduction of explicitly setting Minimal reproduction: import pydantic
from pydantic import BaseModel
print(f"Version: {pydantic.version.VERSION}")
class HashableModel(BaseModel):
x: int
class Config:
allow_mutation = False
def __hash__(self):
return hash((type(self),) + tuple(self.__dict__.values()))
def __eq__(self, other):
return self.__hash__() == other.__hash__()
class ChildModel(HashableModel):
y: int
# This is fine
test = {HashableModel(x=4): HashableModel(x=2)}
print(test)
# This will fail on 1.8
test = {ChildModel(x=4, y=2): ChildModel(x=2, y=2)}
print(test) On
On
A temporary workaround that is both forwards and backwards compatible is to set class Config:
allow_mutation = False
frozen = True Looking at the patch I'm not sure if there is a simple way to fix this, but I imagine this might hit some folks who have previously defined a common |
Is this fixed by #2423? |
Yes it is fixed by that patch, verified with: $ python3 -m venv venv_test
$ venv_test/bin/pip install wheel
$ venv_test/bin/pip install git+https://github.com/samuelcolvin/pydantic.git@f9fe4aa086e7458f3c3339c408b3215f34e6ca18
$ venv_test/bin/python repro.py
Version: 1.8
{HashableModel(x=4): HashableModel(x=2)}
{ChildModel(x=4, y=2): ChildModel(x=2, y=2)} Thanks and apologies for not seeing the new patch! |
@samuelcolvin any chance you can push a |
Feature Request
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:Hi there,
I need the model to be hashable when I set
allow_mutation
toFalse
. I could write myself a__hash__
function, but I think it could be nice if pydantic generated it by default in the classBaseModel
.This would be the same behavior of the built-in
dataclass
which generate a hash function if the object is immutable (parameterfrozen
isTrue
) and the__eq__
function exists.Quoting the documentation https://docs.python.org/3/library/dataclasses.html :
We could generate a default
__hash__
function forBaseModel
ifallow_mutation
isFalse
, what do you think ?Examples
For example, now we have:
After, we would have:
(Note that if
allow_mutation = True
, the hash function would beNone
and the behavior would not be changed)The text was updated successfully, but these errors were encountered: