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

Adds type validation to ray.autoscaler.sdk.request_resources().
This commit is contained in:
truelegion47 2022-07-16 15:40:05 -04:00 committed by GitHub
parent 081bbfbff1
commit 5bd8d121b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View file

@ -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)

View file

@ -3561,6 +3561,21 @@ MemAvailable: 33000000 kB
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