Skip to content

Commit

Permalink
accept float for cpu
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Lo <wenchih@apache.org>
  • Loading branch information
lowc1012 committed Feb 17, 2024
1 parent 02400e8 commit e535b34
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
22 changes: 14 additions & 8 deletions flytekit/core/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Resources(object):
Resources(cpu="1", mem="2048") # This is 1 CPU and 2 KB of memory
Resources(cpu="100m", mem="2Gi") # This is 1/10th of a CPU and 2 gigabytes of memory
Resources(cpu=2, mem=1024) # This is 2 CPU and 1 KB of memory
Resources(cpu=0.5, mem=1024) # This is 500m CPU and 1 KB of memory
# For Kubernetes-based tasks, pods use ephemeral local storage for scratch space, caching, and for logs.
# This allocates 1Gi of such local storage.
Expand All @@ -27,22 +27,28 @@ class Resources(object):
Also refer to the `K8s conventions. <https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#resource-units-in-kubernetes>`__
"""

cpu: Optional[Union[str, int]] = None
cpu: Optional[Union[str, int, float]] = None
mem: Optional[Union[str, int]] = None
gpu: Optional[Union[str, int]] = None
ephemeral_storage: Optional[Union[str, int]] = None

def __post_init__(self):
def _check_none_or_str_or_int(value):
def _check_cpu(value):
if value is None:
return
if not isinstance(value, (str, int, float)):
raise AssertionError(f"{value} should be of type str or int or float")

Check warning on line 40 in flytekit/core/resources.py

View check run for this annotation

Codecov / codecov/patch

flytekit/core/resources.py#L40

Added line #L40 was not covered by tests

def _check_others(value):
if value is None:
return
if not isinstance(value, (str, int)):
raise AssertionError(f"{value} should be a string or an integer")
raise AssertionError(f"{value} should be of type str or int")

Check warning on line 46 in flytekit/core/resources.py

View check run for this annotation

Codecov / codecov/patch

flytekit/core/resources.py#L46

Added line #L46 was not covered by tests

_check_none_or_str_or_int(self.cpu)
_check_none_or_str_or_int(self.mem)
_check_none_or_str_or_int(self.gpu)
_check_none_or_str_or_int(self.ephemeral_storage)
_check_cpu(self.cpu)
_check_others(self.mem)
_check_others(self.gpu)
_check_others(self.ephemeral_storage)


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion tests/flytekit/unit/core/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_convert_limits(resource_dict: Dict[str, str], expected_resource_name: _

def test_incorrect_type_resources():
with pytest.raises(AssertionError):
Resources(cpu=0.1) # type: ignore
Resources(cpu=bytes(1)) # type: ignore
with pytest.raises(AssertionError):
Resources(mem=0.1) # type: ignore
with pytest.raises(AssertionError):
Expand Down

0 comments on commit e535b34

Please sign in to comment.