Skip to content

Commit

Permalink
Add type validation for ray.autoscaler.sdk.request_resources()(ray-pr…
Browse files Browse the repository at this point in the history
…oject#26626)

Adds type validation to ray.autoscaler.sdk.request_resources().

Signed-off-by: Xiaowei Jiang <[email protected]>
  • Loading branch information
truelegion47 authored and xwjiang2010 committed Jul 19, 2022
1 parent b27b9b2 commit f7cf3e9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
16 changes: 16 additions & 0 deletions python/ray/autoscaler/sdk/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 240,22 @@ def request_resources(
>>> request_resources( # doctest: SKIP
... bundles=[{"CPU": 1}, {"CPU": 1}, {"CPU": 1}])
"""
if num_cpus is not None and not isinstance(num_cpus, int):
raise TypeError("num_cpus should be of type int.")
if bundles is not None:
if isinstance(bundles, List):
for bundle in bundles:
if isinstance(bundle, Dict):
for key in bundle.keys():
if not (isinstance(key, str) and isinstance(bundle[key], int)):
raise TypeError(
"each bundle key should be str and value as int."
)
else:
raise TypeError("each bundle should be a Dict.")
else:
raise TypeError("bundles should be of type List")

return commands.request_resources(num_cpus, bundles)


Expand Down
15 changes: 15 additions & 0 deletions python/ray/tests/test_autoscaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3561,6 3561,21 @@ def __init__(self, *args, **kwargs):
monitor.run()
mock_publish.assert_called_once()

def testInitializeSDKArguments(self):
# https://github.com/ray-project/ray/issues/23166
from ray.autoscaler.sdk import request_resources

with self.assertRaises(TypeError):
request_resources(num_cpus="bar")
with self.assertRaises(TypeError):
request_resources(bundles="bar")
with self.assertRaises(TypeError):
request_resources(bundles=["foo"])
with self.assertRaises(TypeError):
request_resources(bundles=[{"foo": "bar"}])
with self.assertRaises(TypeError):
request_resources(bundles=[{"foo": 1}, {"bar": "baz"}])


def test_import():
"""This test ensures that all the autoscaler imports work as expected to
Expand Down

0 comments on commit f7cf3e9

Please sign in to comment.