diff --git a/python/ray/autoscaler/sdk/sdk.py b/python/ray/autoscaler/sdk/sdk.py index 4299fccdf..42c5557ac 100644 --- a/python/ray/autoscaler/sdk/sdk.py +++ b/python/ray/autoscaler/sdk/sdk.py @@ -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) diff --git a/python/ray/tests/test_autoscaler.py b/python/ray/tests/test_autoscaler.py index f4f3c198f..772492a5b 100644 --- a/python/ray/tests/test_autoscaler.py +++ b/python/ray/tests/test_autoscaler.py @@ -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