From 396b70344e5541b96194621924e3b5cfbfe373f9 Mon Sep 17 00:00:00 2001 From: nabenabe0928 Date: Thu, 31 Oct 2024 05:29:49 +0100 Subject: [PATCH] Apply ozaki's comment --- .../auto_sampler/tests/test_sampler.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/package/samplers/auto_sampler/tests/test_sampler.py b/package/samplers/auto_sampler/tests/test_sampler.py index 6677d1a8..13b3933a 100644 --- a/package/samplers/auto_sampler/tests/test_sampler.py +++ b/package/samplers/auto_sampler/tests/test_sampler.py @@ -29,7 +29,7 @@ from multiprocessing.managers import DictProxy import os from typing import Any -from unittest.mock import Mock +from unittest.mock import patch import warnings from _pytest.fixtures import SubRequest @@ -677,15 +677,15 @@ def mock_before_trial(study: Study, trial: FrozenTrial) -> None: assert study._thread_local.cached_all_trials is None original_before_trial(study, trial) - sampler.before_trial = Mock(side_effect=mock_before_trial) - study = optuna.study.create_study(sampler=sampler) + with patch.object(sampler, "before_trial", side_effect=mock_before_trial): + study = optuna.study.create_study(sampler=sampler) - def objective(trial: Trial) -> float: - assert trial._relative_params is None + def objective(trial: Trial) -> float: + assert trial._relative_params is None - trial.suggest_float("x", -10, 10) - trial.suggest_float("y", -10, 10) - assert trial._relative_params is not None - return -1 + trial.suggest_float("x", -10, 10) + trial.suggest_float("y", -10, 10) + assert trial._relative_params is not None + return -1 - study.optimize(objective, n_trials=10, n_jobs=n_jobs) + study.optimize(objective, n_trials=10, n_jobs=n_jobs)