-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmetaclass_test.py
325 lines (229 loc) · 6.33 KB
/
metaclass_test.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
from abc import ABCMeta, abstractmethod, abstractproperty
from inspect import getdoc, ismethod
from types import FunctionType, MethodType
import pytest
from six import add_metaclass
from custom_inherit import DocInheritMeta
from custom_inherit._doc_parse_tools.section_items import _RE_PATTERN_ITEMS
try:
from inspect import signature
except ImportError:
from inspect import getargspec as signature
def style(x, y):
return "valid"
""" With ABC"""
@add_metaclass(DocInheritMeta(style=style, abstract_base_class=True))
class Parent(object):
def method(self, x, y=None):
""""""
pass
@classmethod
def clsmthd(cls):
""""""
pass
@staticmethod
def static():
""""""
pass
@property
def prop(self):
""""""
return None
@abstractmethod
def absmthd(self):
""""""
pass
@abstractproperty
def absproperty(self):
""""""
return None
class Kid(Parent):
def __init__(self):
"""kid"""
pass
def kid_method(self):
"""kid"""
pass
def method(self, x, y=None):
pass
@classmethod
def clsmthd(cls):
pass
@staticmethod
def static():
pass
@property
def prop(self):
return None
def absmthd(self):
pass
@property
def absproperty(self):
return None
def test_abc():
assert isinstance(Parent, ABCMeta)
assert isinstance(Kid, ABCMeta)
def test_sideeffect():
assert getdoc(Kid.kid_method) == "kid"
assert signature(Kid.method) == signature(Parent.method)
def test_special_method():
# by default, Kid.__init__ docstring should not inherit from its parent
assert isinstance(Kid().__init__, MethodType)
assert getdoc(Kid.__init__) == "kid"
def test_method():
assert isinstance(Kid().method, MethodType)
assert getdoc(Kid.method) == "valid"
def test_classmethod():
assert ismethod(Kid.clsmthd) and Kid.clsmthd.__self__ is Kid
assert getdoc(Kid.clsmthd) == "valid"
def test_staticmethod():
assert isinstance(Kid().static, FunctionType)
assert getdoc(Kid.static) == "valid"
def test_property():
assert isinstance(Kid.prop, property)
assert getdoc(Kid.prop) == "valid"
def test_abstract_method():
assert "absmthd" in Parent.__abstractmethods__
assert getdoc(Kid.absmthd) == "valid"
def test_abstract_property():
assert "absproperty" in Parent.__abstractmethods__
assert getdoc(Kid.absproperty) == "valid"
""" Without ABC"""
@add_metaclass(DocInheritMeta(style=style))
class Parent2(object):
def method(self, x, y=None):
""""""
pass
@classmethod
def clsmthd(cls):
""""""
pass
@staticmethod
def static():
""""""
pass
@property
def prop(self):
""""""
return None
class Kid2(Parent2):
def __init__(self):
"""kid"""
pass
def kid_method(self):
"""kid"""
pass
def method(self, x, y=None):
pass
@classmethod
def clsmthd(cls):
pass
@staticmethod
def static():
pass
@property
def prop(self):
return None
def test_sideeffect2():
assert getdoc(Kid2.kid_method) == "kid"
assert signature(Kid2.method) == signature(Parent.method)
def test_special_method2():
# by default, Kid.__init__ docstring should not inherit from its parent
assert isinstance(Kid2().__init__, MethodType)
assert getdoc(Kid2.__init__) == "kid"
def test_method2():
assert isinstance(Kid2().method, MethodType)
assert getdoc(Kid2.method) == "valid"
def test_classmethod2():
assert ismethod(Kid2.clsmthd) and Kid2.clsmthd.__self__ is Kid2
assert getdoc(Kid2.clsmthd) == "valid"
def test_staticmethod2():
assert isinstance(Kid2.static, FunctionType)
assert getdoc(Kid2.static) == "valid"
def test_property2():
assert isinstance(Kid2.prop, property)
assert getdoc(Kid2.prop) == "valid"
def test_class_docstring():
@add_metaclass(DocInheritMeta(style="numpy"))
class Parent(object):
"""
Parent class.
Returns
-------
foo
"""
class Mixin(object):
"""
This is mixin which does something.
"""
class Child(Mixin, Parent):
"""
Attributes
----------
bar
"""
assert (
getdoc(Child)
== "This is mixin which does something.\n\nAttributes\n----------\nbar\n\nReturns\n-------\nfoo"
)
def test_class_docstring_merge_hierarchy_numpy():
@add_metaclass(DocInheritMeta(style="numpy_with_merge"))
class GrandParent(object):
"""GrandParent.
Attributes
----------
foo
"""
class Parent(GrandParent):
"""
Attributes
----------
bar
"""
class Child(Parent):
pass
assert getdoc(Child) == "GrandParent.\n\nAttributes\n----------\nfoo\nbar"
def test_class_docstring_merge_hierarchy_google():
@add_metaclass(DocInheritMeta(style="google_with_merge"))
class GrandParent(object):
"""GrandParent.
Args:
foo
"""
class Parent(GrandParent):
"""
Args:
bar
"""
class Child(Parent):
pass
assert getdoc(Child) == "GrandParent.\n\nParameters:\n foo\n bar"
""" Include special method option"""
@add_metaclass(DocInheritMeta(style=style, include_special_methods=True))
class Parent3(object):
def __init__(self):
""""""
pass
class Kid3(Parent3):
def __init__(self):
"""kid"""
pass
def test_special_method3():
# __init__ docstring should inherit from Parent3
assert isinstance(Kid3().__init__, MethodType)
assert getdoc(Kid3.__init__) == "valid"
@pytest.mark.parametrize(
"section_content,expected",
(
("foo", [("foo", "")]),
("foo : str\n Foo.", [("foo", " : str\n Foo.")]),
("foo\nbar", [("foo", ""), ("bar", "")]),
("foo : str\n Foo.\nbar", [("foo", " : str\n Foo."), ("bar", "")]),
(
"foo : str\n Foo.\nbar : int\n Bar.",
[("foo", " : str\n Foo."), ("bar", " : int\n Bar.")],
),
),
)
def test_regex(section_content, expected):
assert _RE_PATTERN_ITEMS.findall(section_content) == expected