|
| 1 | +"""Test algo option type conversion in modules with postponed annotations. |
| 2 | +
|
| 3 | +With `from __future__ import annotations`, dataclass field types are annotation |
| 4 | +strings rather than type objects. This silently disabled the type conversion in |
| 5 | +`Algorithm.__post_init__` for all optimizer modules using the import. The tests in |
| 6 | +this module mirror the type conversion tests in test_algorithm.py for a dummy |
| 7 | +algorithm defined under postponed annotations; test_algorithm.py covers the case |
| 8 | +without the import. |
| 9 | +
|
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +from dataclasses import dataclass, fields |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from optimagic.exceptions import InvalidAlgoOptionError |
| 19 | +from optimagic.optimization.algorithm import Algorithm, InternalOptimizeResult |
| 20 | +from optimagic.optimization.history import HistoryEntry |
| 21 | +from optimagic.typing import ( |
| 22 | + EvalTask, |
| 23 | + NonNegativeFloat, |
| 24 | + PositiveFloat, |
| 25 | + PositiveInt, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +@dataclass(frozen=True) |
| 30 | +class DummyAlgorithm(Algorithm): |
| 31 | + initial_radius: PositiveFloat = 1.0 |
| 32 | + max_radius: PositiveFloat = 10.0 |
| 33 | + convergence_ftol_rel: NonNegativeFloat = 1e-6 |
| 34 | + stopping_maxiter: PositiveInt = 1000 |
| 35 | + |
| 36 | + def _solve_internal_problem(self, problem, x0): |
| 37 | + hist_entry = HistoryEntry( |
| 38 | + params=x0, |
| 39 | + fun=0.0, |
| 40 | + start_time=0.0, |
| 41 | + task=EvalTask.FUN, |
| 42 | + ) |
| 43 | + problem.history.add_entry(hist_entry) |
| 44 | + return InternalOptimizeResult(x=x0, fun=0.0, success=True) |
| 45 | + |
| 46 | + |
| 47 | +def test_field_types_are_annotation_strings(): |
| 48 | + # Guard: this module must keep `from __future__ import annotations`, otherwise |
| 49 | + # the tests below no longer cover the stringified-annotations code path. |
| 50 | + field_types = {f.name: f.type for f in fields(DummyAlgorithm)} |
| 51 | + assert field_types["stopping_maxiter"] == "PositiveInt" |
| 52 | + assert field_types["initial_radius"] == "PositiveFloat" |
| 53 | + |
| 54 | + |
| 55 | +def test_algorithm_does_type_conversion(): |
| 56 | + algo = DummyAlgorithm( |
| 57 | + initial_radius="1.0", |
| 58 | + max_radius="10.0", |
| 59 | + convergence_ftol_rel="1e-6", |
| 60 | + stopping_maxiter="1000", |
| 61 | + ) |
| 62 | + |
| 63 | + assert isinstance(algo.initial_radius, float) |
| 64 | + assert algo.initial_radius == 1.0 |
| 65 | + assert isinstance(algo.max_radius, float) |
| 66 | + assert algo.max_radius == 10.0 |
| 67 | + assert isinstance(algo.convergence_ftol_rel, float) |
| 68 | + assert algo.convergence_ftol_rel == 1e-6 |
| 69 | + assert isinstance(algo.stopping_maxiter, int) |
| 70 | + assert algo.stopping_maxiter == 1000 |
| 71 | + |
| 72 | + |
| 73 | +def test_algorithm_converts_float_to_int(): |
| 74 | + algo = DummyAlgorithm(stopping_maxiter=1000.0) |
| 75 | + assert isinstance(algo.stopping_maxiter, int) |
| 76 | + assert algo.stopping_maxiter == 1000 |
| 77 | + |
| 78 | + |
| 79 | +def test_algorithm_does_type_conversion_in_with_option(): |
| 80 | + algo = DummyAlgorithm() |
| 81 | + new_algo = algo.with_option( |
| 82 | + initial_radius="2.0", |
| 83 | + max_radius="20.0", |
| 84 | + ) |
| 85 | + |
| 86 | + assert isinstance(new_algo.initial_radius, float) |
| 87 | + assert new_algo.initial_radius == 2.0 |
| 88 | + assert isinstance(new_algo.max_radius, float) |
| 89 | + assert new_algo.max_radius == 20.0 |
| 90 | + |
| 91 | + |
| 92 | +def test_error_with_negative_radius(): |
| 93 | + with pytest.raises(InvalidAlgoOptionError): |
| 94 | + DummyAlgorithm(initial_radius=-1.0) |
| 95 | + |
| 96 | + |
| 97 | +def test_error_with_negative_maxiter(): |
| 98 | + with pytest.raises(InvalidAlgoOptionError): |
| 99 | + DummyAlgorithm(stopping_maxiter=-1) |
0 commit comments