question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model copy(update=) not validating

See original GitHub issue

Question

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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

6reactions
indigoSamcommented, Apr 27, 2022

I think best to implement this in our own logic.

@samuelcolvin 👋😸 Was this ever implemented? Or should we still use community solutions? Thank you!

3reactions
samuelcolvincommented, Mar 15, 2019

how about update complies with validate_assignment, eg. it’s validated only if validate_assignment = True?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found