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

Add type validation for ray.autoscaler.sdk.request_resources() #26626

Merged
merged 2 commits into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
adding validation to autoscaler
Signed-off-by: Your Name <[email protected]>
  • Loading branch information
Your Name committed Jul 16, 2022
commit 7db9fa039c83c5b4e468c2c68e546db3f4347979
18 changes: 18 additions & 0 deletions python/ray/autoscaler/sdk/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 240,24 @@ 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 not 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