File: test_deprecated_class.py

package info (click to toggle)
python-deprecated 1.2.18-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,308 kB
  • sloc: python: 1,458; makefile: 32
file content (155 lines) | stat: -rw-r--r-- 4,324 bytes parent folder | download | duplicates (3)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# coding: utf-8
from __future__ import print_function

import inspect
import io
import warnings

import deprecated.classic


def test_simple_class_deprecation():
    # stream is used to store the deprecation message for testing
    stream = io.StringIO()

    # To deprecate a class, it is better to emit a message when ``__new__`` is called.
    # The simplest way is to override the ``__new__``method.
    class MyBaseClass(object):
        def __new__(cls, *args, **kwargs):
            print(u"I am deprecated!", file=stream)
            return super(MyBaseClass, cls).__new__(cls, *args, **kwargs)

    # Of course, the subclass will be deprecated too
    class MySubClass(MyBaseClass):
        pass

    obj = MySubClass()
    assert isinstance(obj, MyBaseClass)
    assert inspect.isclass(MyBaseClass)
    assert stream.getvalue().strip() == u"I am deprecated!"


def test_class_deprecation_using_wrapper():
    # stream is used to store the deprecation message for testing
    stream = io.StringIO()

    class MyBaseClass(object):
        pass

    # To deprecated the class, we use a wrapper function which emits
    # the deprecation message and calls ``__new__```.

    original_new = MyBaseClass.__new__

    def wrapped_new(unused, *args, **kwargs):
        print(u"I am deprecated!", file=stream)
        return original_new(*args, **kwargs)

    # Like ``__new__``, this wrapper is a class method.
    # It is used to patch the original ``__new__``method.
    MyBaseClass.__new__ = classmethod(wrapped_new)

    class MySubClass(MyBaseClass):
        pass

    obj = MySubClass()
    assert isinstance(obj, MyBaseClass)
    assert inspect.isclass(MyBaseClass)
    assert stream.getvalue().strip() == u"I am deprecated!"


def test_class_deprecation_using_a_simple_decorator():
    # stream is used to store the deprecation message for testing
    stream = io.StringIO()

    # To deprecated the class, we use a simple decorator
    # which patches the original ``__new__`` method.

    def simple_decorator(wrapped_cls):
        old_new = wrapped_cls.__new__

        def wrapped_new(unused, *args, **kwargs):
            print(u"I am deprecated!", file=stream)
            return old_new(*args, **kwargs)

        wrapped_cls.__new__ = classmethod(wrapped_new)
        return wrapped_cls

    @simple_decorator
    class MyBaseClass(object):
        pass

    class MySubClass(MyBaseClass):
        pass

    obj = MySubClass()
    assert isinstance(obj, MyBaseClass)
    assert inspect.isclass(MyBaseClass)
    assert stream.getvalue().strip() == u"I am deprecated!"


def test_class_deprecation_using_deprecated_decorator():
    @deprecated.classic.deprecated
    class MyBaseClass(object):
        pass

    class MySubClass(MyBaseClass):
        pass

    with warnings.catch_warnings(record=True) as warns:
        warnings.simplefilter("always")
        obj = MySubClass()

    assert len(warns) == 1
    assert isinstance(obj, MyBaseClass)
    assert inspect.isclass(MyBaseClass)
    assert issubclass(MySubClass, MyBaseClass)


def test_class_respect_global_filter():
    @deprecated.classic.deprecated
    class MyBaseClass(object):
        pass

    with warnings.catch_warnings(record=True) as warns:
        warnings.simplefilter("once")
        obj = MyBaseClass()
        obj = MyBaseClass()

    assert len(warns) == 1


def test_subclass_deprecation_using_deprecated_decorator():
    @deprecated.classic.deprecated
    class MyBaseClass(object):
        pass

    @deprecated.classic.deprecated
    class MySubClass(MyBaseClass):
        pass

    with warnings.catch_warnings(record=True) as warns:
        warnings.simplefilter("always")
        obj = MySubClass()

    assert len(warns) == 2
    assert isinstance(obj, MyBaseClass)
    assert inspect.isclass(MyBaseClass)
    assert issubclass(MySubClass, MyBaseClass)


def test_simple_class_deprecation_with_args():
    @deprecated.classic.deprecated('kwargs class')
    class MyClass(object):
        def __init__(self, arg):
            super(MyClass, self).__init__()
            self.args = arg

    MyClass(5)
    with warnings.catch_warnings(record=True) as warns:
        warnings.simplefilter("always")
        obj = MyClass(5)

    assert len(warns) == 1
    assert isinstance(obj, MyClass)
    assert inspect.isclass(MyClass)