Model copy(update=) not validating
See original GitHub issueQuestion
Currently calling copy
with update=
does not validate the updated data and we can end up with odd cases like this:
import pydantic
class ChildModel(BaseModel):
v: int
class Model(BaseModel):
c: ChildModel
>>> m = Model(c={"v": 5})
>>> print(m.c)
ChildModel v=5
>>> m2 = m.copy(update={"c": {"v": 3}})
>>> print(m2.c)
{'v': 3}
One can also imagine a case where the updated data is completely incorrect as well. A current workaround would be to do the following:
>>> m3 = m.copy(update={"c": m.c.copy(update={"v": 3})})
>> print(m3.c)
ChildModel v=3
This seems a bit awkward. A possible fix would be to call __init__
rather than construct
in the copy constructor if update is not None
. We use this feature quite a bit to alter immutable models.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:11 (9 by maintainers)
Top Results From Across the Web
Model copy(update=) not validating · Issue #418 · pydantic ...
Question Currently calling copy with update= does not validate the updated data and we can end up with odd cases like this: import...
Read more >How to validate a pydantic object after editing it - Stack Overflow
Is there any obvious to validate a pydantic model after some ... A.validate(a) # no error a.copy(update=dict(b='foobar')) # no error.
Read more >[SOLVED] Copy and Paste Not Working on Windows 10
Copy and paste not working properly on your Windows 10 PC? Here's the real fix! Try these fixes and make your broken copy-paste...
Read more >Error when you validate a copy of Windows - Microsoft Learn
When you try to validate a copy of Windows, you may receive an error message that resembles the following: Update installation failed.
Read more >What if Copy and Paste are not working in Windows 10?
1. Temporarily disable your antivirus. 2. Reset the rdpclip.exe and dwm.exe processes. 3. Restart Windows Explorer.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@samuelcolvin 👋😸 Was this ever implemented? Or should we still use community solutions? Thank you!
how about
update
complies withvalidate_assignment
, eg. it’s validated only ifvalidate_assignment = True
?