Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion flytekit/types/directory/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,17 @@ def serialize_flyte_dir(self) -> Dict[str, str]:
@model_validator(mode="after")
def deserialize_flyte_dir(self, info) -> FlyteDirectory:
if info.context is None or info.context.get("deserialize") is not True:
return self
# Check if all private attributes are already set up (e.g., from __init__)
if hasattr(self, "_downloader") and hasattr(self, "_remote_source"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:
Do we need to check for the other private vars like _downloaded too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or will they all together be set or not set anyways?

return self

# Populate missing private attributes for Pydantic-deserialized instances
dict_obj = {"path": str(self.path)}

return FlyteDirToMultipartBlobTransformer().dict_to_flyte_directory(
dict_obj=dict_obj,
expected_python_type=type(self),
)

pv = FlyteDirToMultipartBlobTransformer().to_python_value(
FlyteContextManager.current_context(),
Expand Down
13 changes: 12 additions & 1 deletion flytekit/types/file/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,18 @@ def serialize_flyte_file(self) -> Dict[str, typing.Any]:
@model_validator(mode="after")
def deserialize_flyte_file(self, info) -> "FlyteFile":
if info.context is None or info.context.get("deserialize") is not True:
return self
if hasattr(self, "_downloader") and hasattr(self, "_remote_source"):
return self

dict_obj = {"path": str(self.path)}
metadata = getattr(self, "metadata", None)
if metadata is not None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sanity check:
@gverkes did you test whether we need to do this for flyte directory too?

dict_obj["metadata"] = metadata

return FlyteFilePathTransformer().dict_to_flyte_file(
dict_obj=dict_obj,
expected_python_type=type(self),
)

pv = FlyteFilePathTransformer().to_python_value(
FlyteContextManager.current_context(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1022,3 +1022,37 @@ def mock_resolve_remote_path(flyte_uri: str):

bm_revived = TypeEngine.to_python_value(ctx, lit, BM)
assert bm_revived.s.literal.uri == "/my/replaced/val"


def test_flytefile_pydantic_model_dump_validate_cycle():
class BM(BaseModel):
ff: FlyteFile

bm = BM(ff=FlyteFile.from_source("s3://my-bucket/file.txt"))

assert bm.ff.remote_source == "s3://my-bucket/file.txt"

bm_dict = bm.model_dump()
bm2 = BM.model_validate(bm_dict)

assert isinstance(bm2.ff, FlyteFile)
assert bm2.ff.remote_source == "s3://my-bucket/file.txt"

bm2.model_dump()


def test_flytedirectory_pydantic_model_dump_validate_cycle():
class BM(BaseModel):
fd: FlyteDirectory

bm = BM(fd=FlyteDirectory.from_source("s3://my-bucket/my-dir"))

assert bm.fd.remote_source == "s3://my-bucket/my-dir"

bm_dict = bm.model_dump()
bm2 = BM.model_validate(bm_dict)

assert isinstance(bm2.fd, FlyteDirectory)
assert bm2.fd.remote_source == "s3://my-bucket/my-dir"

bm2.model_dump()
Loading