@@ -34,17 +34,19 @@ def test_parameter_type_validation(self) -> None:
3434 InstantiationBase ._get_parameter_type (list )
3535
3636 def test_make_search_space (self ) -> None :
37- with self .assertRaisesRegex (ValueError , "cannot contain spaces" ):
38- InstantiationBase .make_search_space (
39- parameters = [
40- {
41- "name" : "x space 1" ,
42- "type" : "range" ,
43- "bounds" : [0.0 , 1.0 ],
44- }
45- ],
46- parameter_constraints = None ,
47- )
37+ # Parameter names with spaces should be allowed in search spaces
38+ # (only rejected in constraint string parsing).
39+ ss = InstantiationBase .make_search_space (
40+ parameters = [
41+ {
42+ "name" : "x space 1" ,
43+ "type" : "range" ,
44+ "bounds" : [0.0 , 1.0 ],
45+ }
46+ ],
47+ parameter_constraints = None ,
48+ )
49+ self .assertIn ("x space 1" , ss .parameters )
4850
4951 def test_constraint_from_str (self ) -> None :
5052 x1 = RangeParameter (
@@ -96,7 +98,7 @@ def test_constraint_from_str(self) -> None:
9698 "x1 + x2 + x3 <= 3" , {"x1" : x1 , "x2" : x2 , "x3" : x3 }
9799 )
98100
99- with self .assertRaisesRegex (AssertionError , "Outcome constraint 'm1" ):
101+ with self .assertRaisesRegex (ValueError , "Outcome constraint 'm1" ):
100102 InstantiationBase .outcome_constraint_from_str ("m1 == 2*m2" )
101103
102104 self .assertEqual (three_val_constaint .bound , 3.0 )
@@ -191,6 +193,29 @@ def test_constraint_from_str(self) -> None:
191193 "x1 + x2 / 2.0 + x3 >= 3" , {"x1" : x1 , "x2" : x2 , "x3" : x3 }
192194 )
193195
196+ def test_spaces_in_metric_and_parameter_names (self ) -> None :
197+ # Metric and parameter names with spaces are allowed everywhere
198+ # except in constraint string parsing, where split() would break.
199+ metric = InstantiationBase ._make_metric (name = "my metric" )
200+ self .assertEqual (metric .name , "my metric" )
201+
202+ experiment = InstantiationBase .make_experiment (
203+ parameters = [{"name" : "x" , "type" : "range" , "bounds" : [0 , 1 ]}],
204+ tracking_metric_names = ["my metric" , "another metric" ],
205+ )
206+ tracking_metric_names = [m .name for m in experiment .tracking_metrics ]
207+ self .assertIn ("my metric" , tracking_metric_names )
208+ self .assertIn ("another metric" , tracking_metric_names )
209+
210+ # Constraint string parsing rejects spaces (can't tokenize correctly).
211+ x1 = RangeParameter (
212+ name = "x 1" , parameter_type = ParameterType .FLOAT , lower = 0.1 , upper = 4.0
213+ )
214+ with self .assertRaisesRegex (ValueError , "cannot contain spaces" ):
215+ InstantiationBase .constraint_from_str ("x 1 <= 3" , {"x 1" : x1 })
216+ with self .assertRaisesRegex (ValueError , "cannot contain spaces" ):
217+ InstantiationBase .outcome_constraint_from_str ("my metric <= 3" )
218+
194219 def test_add_tracking_metrics (self ) -> None :
195220 experiment = InstantiationBase .make_experiment (
196221 parameters = [{"name" : "x" , "type" : "range" , "bounds" : [0 , 1 ]}],
@@ -212,8 +237,12 @@ def test_make_objectives(self) -> None:
212237 with self .assertRaisesRegex (ValueError , "specify 'minimize' or 'maximize'" ):
213238 InstantiationBase .make_objectives ({"branin" : "unknown" })
214239
215- with self .assertRaisesRegex (ValueError , "cannot contain spaces" ):
216- InstantiationBase .make_objectives ({"branin space" : "maximize" })
240+ # Metric names with spaces should be allowed in objectives
241+ # (only rejected in constraint string parsing).
242+ objectives_with_spaces = InstantiationBase .make_objectives (
243+ {"branin space" : "maximize" }
244+ )
245+ self .assertEqual (objectives_with_spaces [0 ].metric_names [0 ], "branin space" )
217246
218247 objectives = InstantiationBase .make_objectives (
219248 {"branin" : "minimize" , "currin" : "maximize" }
0 commit comments