File size: 5,679 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from __future__ import print_function

import unittest

import abc
import _py_abc

import wrapt

class TestClassInheritance(unittest.TestCase):

    def test_basic_inheritance(self):
        @wrapt.decorator
        def wrapper(wrapped, instance, args, kwargs):
            return wrapped(*args, **kwargs)

        class B1:
            def method(self):
                pass

        @wrapper
        class C1(B1):
            def method(self):
                pass

        class D1(C1):
            def method(self):
                pass

        self.assertTrue(issubclass(B1, B1))
        self.assertTrue(issubclass(C1, B1))
        self.assertTrue(issubclass(D1, B1))

        self.assertFalse(issubclass(B1, C1))
        self.assertTrue(issubclass(C1, C1))
        self.assertTrue(issubclass(D1, C1))

        self.assertFalse(issubclass(B1, D1))
        self.assertFalse(issubclass(C1, D1))
        self.assertTrue(issubclass(D1, D1))

        self.assertTrue(issubclass(B1, (B1, C1, D1)))
        self.assertTrue(issubclass(C1, (B1, C1, D1)))
        self.assertTrue(issubclass(D1, (B1, C1, D1)))

        self.assertTrue(isinstance(C1(), B1))
        self.assertTrue(isinstance(C1(), C1))
        self.assertTrue(isinstance(D1(), B1))
        self.assertTrue(isinstance(D1(), C1))

        def function():
            pass
        class F(wrapt.FunctionWrapper):
            pass
        instance = F(function, wrapper)
        self.assertTrue(isinstance(instance, wrapt.FunctionWrapper))

    def test_abc_inheritance(self):
        # XXX The checks commented out below all fail because the
        # helpers for issubclass() via __subclasscheck__() in ABCMeta
        # base class when C implementation is used does not support duck
        # typing and will fail if the argument it is given is an object
        # proxy like wrapt decorator uses. There is no known workaround
        # for this problem.
        #
        #       def __subclasscheck__(cls, subclass):
        #           """Override for issubclass(subclass, cls)."""
        #   >       return _abc_subclasscheck(cls, subclass)
        #   E       TypeError: issubclass() arg 1 must be a class

        @wrapt.decorator
        def wrapper(wrapped, instance, args, kwargs):
            return wrapped(*args, **kwargs)

        class A1(metaclass=abc.ABCMeta):
            @abc.abstractmethod
            def method(self):
                pass

        class B1(A1):
            def method(self):
                pass

        @wrapper
        class C1(B1):
            def method(self):
                pass

        class D1(C1):
            def method(self):
                pass

        self.assertTrue(issubclass(A1, A1))
        self.assertTrue(issubclass(B1, A1))
        # self.assertTrue(issubclass(C1, A1))
        self.assertTrue(issubclass(D1, A1))

        self.assertFalse(issubclass(A1, B1))
        self.assertTrue(issubclass(B1, B1))
        # self.assertTrue(issubclass(C1, B1))
        self.assertTrue(issubclass(D1, B1))

        self.assertFalse(issubclass(A1, C1))
        self.assertFalse(issubclass(B1, C1))
        self.assertTrue(issubclass(C1, C1))
        self.assertTrue(issubclass(D1, C1))

        self.assertFalse(issubclass(A1, D1))
        self.assertFalse(issubclass(B1, D1))
        # self.assertFalse(issubclass(C1, D1))
        self.assertTrue(issubclass(D1, D1))

        self.assertTrue(issubclass(A1, (A1, B1, C1, D1)))
        self.assertTrue(issubclass(B1, (A1, B1, C1, D1)))
        # self.assertTrue(issubclass(C1, (A1, B1, C1, D1)))
        self.assertTrue(issubclass(D1, (A1, B1, C1, D1)))

        self.assertTrue(isinstance(C1(), B1))
        self.assertTrue(isinstance(C1(), C1))
        self.assertTrue(isinstance(D1(), B1))
        self.assertTrue(isinstance(D1(), C1))

    def test_py_abc_inheritance(self):
        # In contrast to above when C implementation for ABCMeta helpers
        # are used, these all pass as have use the Python implementation
        # instead.

        @wrapt.decorator
        def wrapper(wrapped, instance, args, kwargs):
            return wrapped(*args, **kwargs)

        class A1(metaclass=_py_abc.ABCMeta):
            @abc.abstractmethod
            def method(self):
                pass

        class B1(A1):
            def method(self):
                pass

        @wrapper
        class C1(B1):
            def method(self):
                pass

        class D1(C1):
            def method(self):
                pass

        self.assertTrue(issubclass(A1, A1))
        self.assertTrue(issubclass(B1, A1))
        self.assertTrue(issubclass(C1, A1))
        self.assertTrue(issubclass(D1, A1))

        self.assertFalse(issubclass(A1, B1))
        self.assertTrue(issubclass(B1, B1))
        self.assertTrue(issubclass(C1, B1))
        self.assertTrue(issubclass(D1, B1))

        self.assertFalse(issubclass(A1, C1))
        self.assertFalse(issubclass(B1, C1))
        self.assertTrue(issubclass(C1, C1))
        self.assertTrue(issubclass(D1, C1))

        self.assertFalse(issubclass(A1, D1))
        self.assertFalse(issubclass(B1, D1))
        self.assertFalse(issubclass(C1, D1))
        self.assertTrue(issubclass(D1, D1))

        self.assertTrue(issubclass(A1, (A1, B1, C1, D1)))
        self.assertTrue(issubclass(B1, (A1, B1, C1, D1)))
        self.assertTrue(issubclass(C1, (A1, B1, C1, D1)))
        self.assertTrue(issubclass(D1, (A1, B1, C1, D1)))

        self.assertTrue(isinstance(C1(), B1))
        self.assertTrue(isinstance(C1(), C1))
        self.assertTrue(isinstance(D1(), B1))
        self.assertTrue(isinstance(D1(), C1))

if __name__ == '__main__':
    unittest.main()