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

feat(resources): support serialization of Resources dataclasses #2250

Merged
merged 2 commits into from
Mar 10, 2024
Merged
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
6 changes: 4 additions & 2 deletions flytekit/core/resources.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from dataclasses import dataclass
from typing import List, Optional

from mashumaro.mixins.json import DataClassJSONMixin

from flytekit.models import task as task_models


@dataclass
class Resources(object):
class Resources(DataClassJSONMixin):
"""
This class is used to specify both resource requests and resource limits.

Expand Down Expand Up @@ -45,7 +47,7 @@ def _check_none_or_str(value):


@dataclass
class ResourceSpec(object):
class ResourceSpec(DataClassJSONMixin):
requests: Resources
limits: Resources

Expand Down
26 changes: 26 additions & 0 deletions tests/flytekit/unit/core/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,29 @@ def test_incorrect_type_resources():
Resources(gpu=1) # type: ignore
with pytest.raises(AssertionError):
Resources(ephemeral_storage=1) # type: ignore


def test_resources_serialization():
resources = Resources(cpu="2", mem="1Gi", gpu="1", ephemeral_storage="10Gi")
json_str = resources.to_json()
assert isinstance(json_str, str)
assert '"cpu": "2"' in json_str
assert '"mem": "1Gi"' in json_str
assert '"gpu": "1"' in json_str
assert '"ephemeral_storage": "10Gi"' in json_str


def test_resources_deserialization():
json_str = '{"cpu": "2", "mem": "1Gi", "gpu": "1", "ephemeral_storage": "10Gi"}'
resources = Resources.from_json(json_str)
assert resources.cpu == "2"
assert resources.mem == "1Gi"
assert resources.gpu == "1"
assert resources.ephemeral_storage == "10Gi"


def test_resources_round_trip():
original = Resources(cpu="4", mem="2Gi", gpu="2", ephemeral_storage="20Gi")
json_str = original.to_json()
result = Resources.from_json(json_str)
assert original == result
Loading