Skip to content

Commit

Permalink
Input validation for add_categorical_param.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 573967682
  • Loading branch information
belenkil authored and Copybara-Service committed Oct 17, 2023
1 parent c5e243f commit 42405af
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion vizier/_src/pyvizier/shared/parameter_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,8 +977,13 @@ def add_categorical_param(
ParameterConfigSelector for the newly added parameter(s).
Raises:
ValueError: If `index` is invalid (e.g. negative).
ValueError: If `index` is invalid (e.g. negative), or `feasible_values`
are invalid (not strings).
"""
for value in feasible_values:
if not isinstance(value, str):
raise ValueError(f'feasible_values must be strings; got: {value}')

param_names = self._get_parameter_names_to_create(name=name, index=index)

new_params = []
Expand Down
6 changes: 6 additions & 0 deletions vizier/_src/pyvizier/shared/parameter_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,12 @@ def testParseMultiDimensionalParameterName(self, name, expected):
name)
self.assertEqual(base_name_index, expected)

def testValidateCategoricalInput(self):
space = pc.SearchSpace()
root = space.select_root()
with self.assertRaises(ValueError):
root.add_categorical_param('categorical', ['3.2', '2', 5])


class SearchSpaceContainsTest(absltest.TestCase):

Expand Down

0 comments on commit 42405af

Please sign in to comment.