diff --git a/optuna/study/_tell.py b/optuna/study/_tell.py index 0e51d63e04..d66f3be2ea 100644 --- a/optuna/study/_tell.py +++ b/optuna/study/_tell.py @@ -59,15 +59,18 @@ def _check_state_and_values( def _check_values_are_feasible(study: Study, values: Sequence[float]) -> str | None: + float_cast = float + isnan = math.isnan + for v in values: # TODO(Imamura): Construct error message taking into account all values and do not early # return `value` is assumed to be ignored on failure so we can set it to any value. try: - float(v) + float_cast(v) except (ValueError, TypeError): return f"The value {repr(v)} could not be cast to float" - if math.isnan(v): + if isnan(v): return f"The value {v} is not acceptable" if len(study.directions) != len(values):