-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Closed
Labels
bug V1Bug related to Pydantic V1.XBug related to Pydantic V1.X
Milestone
Description
Checks
- I added a descriptive title to this issue
- I have searched (google, github) for similar issues and couldn't find anything
- I have read and followed the docs and still think this is a bug
Bug
The following code worked in pydantic 1.7.3 (version info shown below), but fails with pydantic 1.8:
from pydantic import BaseModel
class Foo(BaseModel):
x: int
# Failed attempt to fix; see below
# __slots__ = ("__hash__",)
def __hash__(self):
return self.x ** 2
f = Foo(x=2)
assert hash(f) == 4
class Bar(Foo):
y: float
b = Bar(x=2, y=1.1)
try:
assert hash(b) == 4
except TypeError as e:
# TypeError: unhashable type: 'Bar'
print(e)It seems like this code prevents the use of Foo.__hash__ on Bar instances:
https://github.com/samuelcolvin/pydantic/blob/master/pydantic/main.py#L347
I tried to experiment with adding "__hash__" to __slots__, but an error is triggered in ModelMetaclass.__new__:
Traceback (most recent call last):
File "bug.py", line 4, in <module>
class Foo1(BaseModel):
File "pydantic/main.py", line 352, in pydantic.main.ModelMetaclass.__new__
File "/usr/lib/python3.8/abc.py", line 85, in __new__
cls = super().__new__(mcls, name, bases, namespace, **kwargs)
ValueError: '__hash__' in __slots__ conflicts with class variable
I would expect that this continues to work as in 1.7.3, or that the documentation suggests how to avoid having these methods squashed.
Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":
Passing version:
$ python -c "import pydantic.utils; print(pydantic.utils.version_info())"
pydantic version: 1.7.3
pydantic compiled: True
install path: /home/khaeru/.local/lib/python3.8/site-packages/pydantic
python version: 3.8.6 (default, Jan 27 2021, 15:42:20) [GCC 10.2.0]
platform: Linux-5.8.0-43-generic-x86_64-with-glibc2.32
optional deps. installed: ['typing-extensions']
Failing version:
$ python -c "import pydantic.utils; print(pydantic.utils.version_info())"
pydantic version: 1.8
pydantic compiled: True
install path: /home/khaeru/.local/lib/python3.8/site-packages/pydantic
python version: 3.8.6 (default, Jan 27 2021, 15:42:20) [GCC 10.2.0]
platform: Linux-5.8.0-43-generic-x86_64-with-glibc2.32
optional deps. installed: ['typing-extensions']
Metadata
Metadata
Assignees
Labels
bug V1Bug related to Pydantic V1.XBug related to Pydantic V1.X