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
BaseModel.copy produces copy that modifies the original #1383
Comments
|
I disagree that this is similar to normal python behavior. To be clear, I'm talking specifically about shallow copying behavior. I agree that a standard python shallow copy copies the "reference". The difference is that for a normal object when you update the "reference" of a copy you do not update the "reference" of the original. If you modify what the "reference points" to, then of course that will modify what the reference points to in the original. This is standard python behavior:
This is pydantic behavior:
|
I 100% agree this is weird, it's not what I would expect to happen, and it's a bug. But it seems to have been inadvertently fixed in v1.5 (as yet unreleased). To resolve this, I'm going to:
|
Awesome! Thanks :) |
* confirm shallow copy works as expected, fix #1383 * fix linting
Bug
When copying a model, changing the value of an attribute on the copy updates the value of the attribute on the original. This only happens if deep != True. If this is expected behavior, I would suggest the documentation needs to be updated, because it's different than the intuition one would have for shallow copies of standard python objects.
My expectation of a shallow copy: Modifying a mutable attribute on the child SHOULD modify the mutable attribute on the parent (the attribute should be the same object). Setting the value of an attribute on the child, however, SHOULD NOT set the value on the original.
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:Reproduction:
The text was updated successfully, but these errors were encountered: