File: test_deprecation.py

package info (click to toggle)
scikit-learn 0.20.2%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 51,036 kB
  • sloc: python: 108,171; ansic: 8,722; cpp: 5,651; makefile: 192; sh: 40
file content (78 lines) | stat: -rw-r--r-- 2,158 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
# Authors: Raghav RV <rvraghav93@gmail.com>
# License: BSD 3 clause


import sys
import pickle

from sklearn.utils.deprecation import _is_deprecated
from sklearn.utils.deprecation import deprecated
from sklearn.utils.testing import assert_warns_message
from sklearn.utils.testing import assert_no_warnings
from sklearn.utils.testing import SkipTest
from sklearn.utils.deprecation import DeprecationDict


@deprecated('qwerty')
class MockClass1:
    pass


class MockClass2:
    @deprecated('mockclass2_method')
    def method(self):
        pass


class MockClass3:
    @deprecated()
    def __init__(self):
        pass


class MockClass4:
    pass


@deprecated()
def mock_function():
    return 10


def test_deprecated():
    assert_warns_message(DeprecationWarning, 'qwerty', MockClass1)
    assert_warns_message(DeprecationWarning, 'mockclass2_method',
                         MockClass2().method)
    assert_warns_message(DeprecationWarning, 'deprecated', MockClass3)
    val = assert_warns_message(DeprecationWarning, 'deprecated', mock_function)
    assert val == 10


def test_is_deprecated():
    if sys.version_info < (3, 5):
        raise SkipTest("This test will run only on python3.5 and above")
    # Test if _is_deprecated helper identifies wrapping via deprecated
    # NOTE it works only for class methods and functions
    assert _is_deprecated(MockClass1.__init__)
    assert _is_deprecated(MockClass2().method)
    assert _is_deprecated(MockClass3.__init__)
    assert not _is_deprecated(MockClass4.__init__)
    assert _is_deprecated(mock_function)


def test_pickle():
    pickle.loads(pickle.dumps(mock_function))


def test_deprecationdict():
    dd = DeprecationDict()
    dd.add_warning('a', 'hello')
    dd.add_warning('b', 'world', DeprecationWarning)
    assert 1 == assert_warns_message(UserWarning, 'hello', dd.get, 'a', 1)
    dd['a'] = 5
    dd['b'] = 6
    dd['c'] = 7
    assert 5 == assert_warns_message(UserWarning, 'hello', dd.__getitem__, 'a')
    assert 6 == assert_warns_message(DeprecationWarning, 'world',
                                     dd.__getitem__, 'b')
    assert 7 == assert_no_warnings(dd.get, 'c')