-
Notifications
You must be signed in to change notification settings - Fork 33
Fix pydantic validate and pickle for tagged union #207
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
Changes from 10 commits
7ccbeca
1d32266
00318df
f04f96e
3e88615
8225b56
42f7418
caa10ee
55410bc
d6b6b23
5d22e33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -38,13 +38,23 @@ def tagged_union( | |||
items will be compared as the tuple (index, value) | ||||
eq: If True, the __eq__ method will be generated. | ||||
""" | ||||
# Performance can be improved if it is a issue in another day | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by this comment? Assigning two methods are not a performance issue. Is it the all of the transform function you are talking about? Please explain. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean the
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But it is actually not really a performance issue here and the comment can be removed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @denghz Can we remove this line from the PR? I'm unsure why you added it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure
denghz marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
def transform(cls: Any) -> Any: | ||||
cls = dataclass(init=False, repr=False, order=False, eq=False, kw_only=True)(cls) | ||||
fields_ = fields(cls) | ||||
field_names = tuple(f.name for f in fields_) | ||||
original_init = cls.__init__ | ||||
|
||||
def tagged_union_getstate(self: Any) -> dict[str, Any]: | ||||
return {f.name: getattr(self, f.name) for f in fields(self)} | ||||
|
||||
def tagged_union_setstate(self: Any, state: dict[str, Any]): | ||||
self.__init__(**state) | ||||
|
||||
cls.__setstate__ = tagged_union_setstate | ||||
cls.__getstate__ = tagged_union_getstate | ||||
|
||||
def __init__(self: Any, **kwargs: Any) -> None: | ||||
tag = kwargs.pop("tag", None) | ||||
|
||||
|
Uh oh!
There was an error while loading. Please reload this page.