-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
_pydantic_post_init()
reassigns __dict__
and removes properties added to the object, for example in __new__()
, breaking integration with SQLAlchemy and other libraries
#3043
Labels
bug V1
Bug related to Pydantic V1.X
Comments
Updated the issue above to correct an issue with the example code. |
zulrang
added a commit
to zulrang/pydantic
that referenced
this issue
Aug 13, 2021
…added to the object, for example in __new__(), breaking integration with SQLAlchemy and other libraries pydantic#3043
5 tasks
@DomWeldon I've created a PR to fix this and added your test, but full credit should go to you. |
jpribyl
pushed a commit
to liquet-ai/pydantic
that referenced
this issue
Oct 7, 2021
…ydantic#3093) * Fix: _pydantic_post_init() reassigns __dict__ and removes properties added to the object, for example in __new__(), breaking integration with SQLAlchemy and other libraries pydantic#3043 * Added changes file
3 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Checks
Bug
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:Related issues: #1089 #2924 .
Summary of Problem
The pydantic dataclass decorator calls
object.__setattr__()
onself.__dict__
for a dataclass post init, using only the output ofvalidate_model()
, which removes any other attributes in the object's namespace, causing many issues including those well described for SQLAlchemy.Full code example
Output
As you can see from the example above, this behaviour is particular to pydantic's dataclass implementation, and does not match with standard library dataclasses.
The fix for this could be very simple: simply calling
self.__dict__.update()
with the validated values, rather than re-assigning__dict__
. This fixed the issue in my minimal test case for SQLAlchemy integration, but I am not sure of the wider consequences of the fix yet.I also realise there may be a philosophy behind the decision to use this approach (and tests with pydantic.BaseModel-derived classes are similar), so appreciate any input if this is the case. However, based on PEP 557 I believe that a dataclass shouldn't alter class/instance behaviour in this way.
I'm working on a PR suggesting this fix, but it's the first time I've checked out this repo and am still getting the dev environment setup.
The text was updated successfully, but these errors were encountered: