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
|
# coding: utf-8
from __future__ import print_function
import inspect
import io
import sys
import warnings
import pytest
import deprecated.sphinx
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!"
@pytest.mark.skipif(
sys.version_info < (3, 3), reason="Classes should have mutable docstrings -- resolved in python 3.3"
)
def test_class_deprecation_using_deprecated_decorator():
@deprecated.sphinx.deprecated(version="7.8.9")
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)
@pytest.mark.skipif(
sys.version_info < (3, 3), reason="Classes should have mutable docstrings -- resolved in python 3.3"
)
def test_subclass_deprecation_using_deprecated_decorator():
@deprecated.sphinx.deprecated(version="7.8.9")
class MyBaseClass(object):
pass
@deprecated.sphinx.deprecated(version="7.8.9")
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)
@pytest.mark.skipif(
sys.version_info < (3, 3), reason="Classes should have mutable docstrings -- resolved in python 3.3"
)
def test_isinstance_versionadded():
# https://github.com/laurent-laporte-pro/deprecated/issues/48
@deprecated.sphinx.versionadded(version="X.Y", reason="some reason")
class VersionAddedCls:
pass
@deprecated.sphinx.versionadded(version="X.Y", reason="some reason")
class VersionAddedChildCls(VersionAddedCls):
pass
instance = VersionAddedChildCls()
assert isinstance(instance, VersionAddedChildCls)
assert isinstance(instance, VersionAddedCls)
@pytest.mark.skipif(
sys.version_info < (3, 3), reason="Classes should have mutable docstrings -- resolved in python 3.3"
)
def test_isinstance_versionchanged():
@deprecated.sphinx.versionchanged(version="X.Y", reason="some reason")
class VersionChangedCls:
pass
@deprecated.sphinx.versionchanged(version="X.Y", reason="some reason")
class VersionChangedChildCls(VersionChangedCls):
pass
instance = VersionChangedChildCls()
assert isinstance(instance, VersionChangedChildCls)
assert isinstance(instance, VersionChangedCls)
@pytest.mark.skipif(
sys.version_info < (3, 3), reason="Classes should have mutable docstrings -- resolved in python 3.3"
)
def test_isinstance_deprecated():
@deprecated.sphinx.deprecated(version="X.Y", reason="some reason")
class DeprecatedCls:
pass
@deprecated.sphinx.deprecated(version="Y.Z", reason="some reason")
class DeprecatedChildCls(DeprecatedCls):
pass
instance = DeprecatedChildCls()
assert isinstance(instance, DeprecatedChildCls)
assert isinstance(instance, DeprecatedCls)
@pytest.mark.skipif(
sys.version_info < (3, 3), reason="Classes should have mutable docstrings -- resolved in python 3.3"
)
def test_isinstance_versionadded_versionchanged():
@deprecated.sphinx.versionadded(version="X.Y")
@deprecated.sphinx.versionchanged(version="X.Y.Z")
class AddedChangedCls:
pass
instance = AddedChangedCls()
assert isinstance(instance, AddedChangedCls)
|