File: test_experimental.py

package info (click to toggle)
optuna 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,784 kB
  • sloc: python: 40,634; sh: 97; makefile: 30
file content (106 lines) | stat: -rw-r--r-- 3,278 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from typing import Any

import pytest

from optuna import _experimental
from optuna.exceptions import ExperimentalWarning


def _sample_func() -> int:
    return 10


class _Sample:
    def __init__(self, a: Any, b: Any, c: Any) -> None:
        pass

    def _method(self) -> None:
        """summary

        detail
        """
        pass

    def _method_expected(self) -> None:
        """summary

        detail

        .. note::
            Added in v1.1.0 as an experimental feature. The interface may change in newer versions
            without prior notice. See https://github.com/optuna/optuna/releases/tag/v1.1.0.
        """
        pass


@pytest.mark.parametrize("version", ["1.1", 100, None])
def test_experimental_raises_error_for_invalid_version(version: Any) -> None:
    with pytest.raises(ValueError):
        _experimental.experimental_func(version)

    with pytest.raises(ValueError):
        _experimental.experimental_class(version)


def test_experimental_func_decorator() -> None:
    version = "1.1.0"
    decorator_experimental = _experimental.experimental_func(version)
    assert callable(decorator_experimental)

    decorated_func = decorator_experimental(_sample_func)
    assert decorated_func.__name__ == _sample_func.__name__
    assert decorated_func.__doc__ == _experimental._EXPERIMENTAL_NOTE_TEMPLATE.format(ver=version)

    with pytest.warns(ExperimentalWarning):
        decorated_func()


def test_experimental_instance_method_decorator() -> None:
    version = "1.1.0"
    decorator_experimental = _experimental.experimental_func(version)
    assert callable(decorator_experimental)

    decorated_method = decorator_experimental(_Sample._method)
    assert decorated_method.__name__ == _Sample._method.__name__
    assert decorated_method.__doc__ == _Sample._method_expected.__doc__

    with pytest.warns(ExperimentalWarning):
        decorated_method(None)  # type: ignore


def test_experimental_class_decorator() -> None:
    version = "1.1.0"
    decorator_experimental = _experimental.experimental_class(version)
    assert callable(decorator_experimental)

    decorated_class = decorator_experimental(_Sample)
    assert decorated_class.__name__ == "_Sample"
    assert decorated_class.__init__.__name__ == "__init__"
    assert decorated_class.__doc__ == _experimental._EXPERIMENTAL_NOTE_TEMPLATE.format(ver=version)

    with pytest.warns(ExperimentalWarning):
        decorated_class("a", "b", "c")


def test_experimental_class_decorator_name() -> None:
    name = "foo"
    decorator_experimental = _experimental.experimental_class("1.1.0", name=name)
    decorated_sample = decorator_experimental(_Sample)

    with pytest.warns(ExperimentalWarning) as record:
        decorated_sample("a", "b", "c")

    assert isinstance(record.list[0].message, Warning)
    assert name in record.list[0].message.args[0]


def test_experimental_decorator_name() -> None:
    name = "bar"
    decorator_experimental = _experimental.experimental_func("1.1.0", name=name)
    decorated_sample_func = decorator_experimental(_sample_func)

    with pytest.warns(ExperimentalWarning) as record:
        decorated_sample_func()

    assert isinstance(record.list[0].message, Warning)
    assert name in record.list[0].message.args[0]