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

no deepcopy #127

Open
user706 opened this issue Jul 10, 2018 · 1 comment
Open

no deepcopy #127

user706 opened this issue Jul 10, 2018 · 1 comment

Comments

@user706
Copy link

user706 commented Jul 10, 2018

"Bug": copy.deepcopy is not possible.

from jsonmodels import models, fields, validators
import copy

class A(models.Base):
    x = fields.IntField(required=True)

a1 = A(x = 1)
a2 = copy.deepcopy(a1)
print(a1.x)
print(a2.x)                # fails in this line
@user706
Copy link
Author

user706 commented Jul 10, 2018

Here's a hack, that adds a function __deepcopy__ to class models.Base:

from jsonmodels import models, fields, validators
import copy

def deepcopy( self, memo ):
    return self.__class__(**self.to_struct())

models.Base.__deepcopy__ = deepcopy

class A(models.Base):
    x = fields.IntField(required=True)

a1 = A(x = 1)
a2 = copy.deepcopy(a1)
print(a1.x)
print(a2.x)
assert a1 == a2

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

1 participant