Skip to content
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

default values are not supported #26

Closed
ahopey opened this issue Feb 7, 2024 · 3 comments
Closed

default values are not supported #26

ahopey opened this issue Feb 7, 2024 · 3 comments

Comments

@ahopey
Copy link

ahopey commented Feb 7, 2024

Hi,

If I modify the example like this:
`import pydantic
from pydantic_changedetect import ChangeDetectionMixin

class SubSomething(ChangeDetectionMixin, pydantic.BaseModel):
c: str = "c" # <-- notice default values
d: str = "d" # <-- notice default values

class Something(ChangeDetectionMixin, pydantic.BaseModel):
name: str = "name" # <-- notice default values
sub: SubSomething = SubSomething() # <-- notice default values

sub_something = SubSomething() # <-- notice the empty constructor
something = Something() # <-- notice the empty constructor
something.model_has_changed # = False # <--- ERROR HERE: AttributeError: model_self_changed_fields
something.model_changed_fields # = set()
something.name = "something else"
something.model_has_changed # = True
something.model_changed_fields # = {"name"}

original = something.model_restore_original()
original.name # = "something"
original.model_has_changed # = False

something.sub.c = "sub_something else"
print(something.sub.c)`

I get that error above when the execution hits line 17. Any ideas? Thanks!

@ddanier
Copy link
Member

ddanier commented Feb 8, 2024

Thanks for the bug report! I did verify the results with pydantic 2.4.2 on 3.9 on the current dev version of pydantic-changedetect.

But I must look deeper into this. But I expect this to be a "copy" issue again.

Background: Pydantic will copy the default value for each new model instance it creates. This is to avoid the most common Python gotcha that default values are only initialised once (see https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments). In the past this meant to override _copy_and_set_values to also copy the internal state of the changedetect values (pydantic v1). Now we override model_copy to accomplish the same result (pydantic v2). It seems like the copy of the default value does use something different though (or started to use something different at some point). I will have to figure this out and extend the copy management to that case.

Also I will add a test for this. Thanks again!

@ddanier
Copy link
Member

ddanier commented Feb 8, 2024

I just released version 0.6.3, which should fix that issue. 👍

@ddanier ddanier closed this as completed Feb 8, 2024
@ahopey
Copy link
Author

ahopey commented Feb 8, 2024

Thank you so much for the quick reaction. I can confirm that this fixed all of our issues. There were some other manifestations of this but I suspected this example pointed the root cause. And indeed it did! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants