-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Closed
Labels
bug V1Bug related to Pydantic V1.XBug related to Pydantic V1.X
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
Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":
pydantic version: 1.7.2
pydantic compiled: True
install path: /Users/aimestereo/.pyenv/versions/3.7.6/envs/feed/lib/python3.7/site-packages/pydantic
python version: 3.7.6 (default, Jan 30 2020, 16:07:03) [Clang 11.0.0 (clang-1100.0.33.17)]
platform: Darwin-19.6.0-x86_64-i386-64bit
optional deps. installed: ['typing-extensions']
thanks to @PrettyWood 1.7 Introduced cool support of built-in dataclass. But in our project this broke pickle
import pickle
import dataclasses
import pydantic
@dataclasses.dataclass
class BuiltInDataclass:
value: int
class PydanticModel(pydantic.BaseModel):
built_in_dataclass: BuiltInDataclass
print(pickle.dumps(
PydanticModel(built_in_dataclass=BuiltInDataclass(value=0))
))output:
---------------------------------------------------------------------------
PicklingError Traceback (most recent call last)
<ipython-input-14-7d67a44cf353> in <module>
15
16 print(pickle.dumps(
---> 17 PydanticModel(built_in_dataclass=BuiltInDataclass(value=0))
18 ))
PicklingError: Can't pickle <class '__main__.BuiltInDataclass'>: it's not the same object as __main__.BuiltInDataclass
For better understanding here's an issue of pickle outside of pydantic context. This is an old pickle problem, when different classes have the same name, simple way to reproduce:
import pickle
class A: pass
# this is an analogy of how pydantic convert built-in dataclass
B = type(A.__name__, (A,), {})
print(A)
# <class '__main__.A'>
print(B)
# <class '__main__.A'>
pickle.dumps(A)
# b'\x80\x03c__main__\nA\nq\x00.'
pickle.dumps(B)
# error
output
---------------------------------------------------------------------------
PicklingError Traceback (most recent call last)
<ipython-input-2-b97d2c67736c> in <module>
10
11 print(pickle.dumps(A))
---> 12 print(pickle.dumps(B))
PicklingError: Can't pickle <class '__main__.A'>: it's not the same object as __main__.A
As a result we can't use this feature, we're stick to intermediate classes that converts buit-in dataclasses to pydantic:
class SerializedDataclass(pydantic.BaseModel):
# copy/paste of all fields from built-in dataclass
value: float
mitasovr, corbands, riyadparvez and vladpy8
Metadata
Metadata
Assignees
Labels
bug V1Bug related to Pydantic V1.XBug related to Pydantic V1.X