-
Notifications
You must be signed in to change notification settings - Fork 297
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
[TypeTransformer] Support frozen dataclasses #2823
Conversation
Signed-off-by: Future-Outlier <eric901201@gmail.com>
Signed-off-by: Future-Outlier <eric901201@gmail.com>
…is great Signed-off-by: Future-Outlier <eric901201@gmail.com> Co-authored-by: Eduardo Apolinario <eapolinario@users.noreply.github.com>
@@ -663,7 +663,7 @@ def _fix_structured_dataset_type(self, python_type: Type[T], python_val: typing. | |||
elif dataclasses.is_dataclass(python_type): | |||
for field in dataclasses.fields(python_type): | |||
val = python_val.__getattribute__(field.name) | |||
python_val.__setattr__(field.name, self._fix_structured_dataset_type(field.type, val)) | |||
object.__setattr__(python_val, field.name, self._fix_structured_dataset_type(field.type, val)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I learned it
@Future-Outlier can we just delete the _fix_structured_dataset_type function? If we need to keep it, could you add a test please that only tests just that one function? Just want to see what it does. There's a comment on that function saying that it's only needed for python 3.7 and 3.8. is that true? if that's true then it's only needed for 3.8 since flytekit doesn't support 3.7 anymore. We can do a sys check and only invoke it for python 3.8. Also the I think? there might be an issue with line 680 - |
yes I will do this today, thank you |
I'll mentor @Mecoli1219 and @mao3267 to do this in this month, thank you! |
Agreed, thank you! @dataclass
class InnerDC:
ff: FlyteFile
@dataclass
class DC:
inner_dc: InnerDC
@task
def t_dc() -> DC:
return DC(inner_dc=InnerDC(ff="s3://path"))
# -> DC(inner_dc=InnerDC(ff=FlyteFile("s3://path"))) |
* [TypeTransformer] Support frozen dataclasses Signed-off-by: Future-Outlier <eric901201@gmail.com> * break tests Signed-off-by: Future-Outlier <eric901201@gmail.com> * use the base class object's magic method, Suggested by Eduardo, this is great Signed-off-by: Future-Outlier <eric901201@gmail.com> Co-authored-by: Eduardo Apolinario <eapolinario@users.noreply.github.com> * add tests Signed-off-by: Future-Outlier <eric901201@gmail.com> --------- Signed-off-by: Future-Outlier <eric901201@gmail.com> Co-authored-by: Eduardo Apolinario <eapolinario@users.noreply.github.com>
Tracking issue
flyteorg/flyte#5849
Why are the changes needed?
Some users want to use frozen dataclasses in their tasks.
This can protect the value of their task.
What changes are proposed in this pull request?
We propose using
object.__setattr__
whereversetattr
is used in the dataclass transformer. Sinceobject
is the base class of all classes, callingsetattr
on it will always work. In the case of frozen dataclasses, the__setattr__
method at the dataclass level overrides the one fromobject
, enforcing immutability.How was this patch tested?
unit test, local execution, and remote execution.
Setup process
Screenshots
Check all the applicable boxes