forked from mitmproxy/pdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.py
308 lines (208 loc) · 4.77 KB
/
misc.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
import abc
from functools import lru_cache
from typing import Generic
from typing import TypeVar
import demo_long
from pdoc._compat import cached_property
# https://github.com/mitmproxy/pdoc/issues/226
class Descriptor:
def __init__(self, func):
self.__doc__ = func.__doc__
def __get__(self, instance, owner):
return self if instance is None else getattr(instance, "_x", 0)
def __set__(self, instance, value):
instance._x = value
class Issue226:
@Descriptor
def size(self):
"""This is the size"""
# Testing function and object default values
def default_func():
pass
default_obj = object()
var_with_default_obj = default_obj
"""this shouldn't render the object address"""
var_with_default_func = default_func
"""this just render like a normal function"""
def func_with_defaults(
a=default_obj,
b=default_func
):
"""this shouldn't render object or function addresses"""
pass
# Testing classmethod links in code
class ClassmethodLink:
"""
You can either do
>>> ClassmethodLink.bar()
42
or
```python
ClassmethodLink.bar()
```
neither will be linked.
"""
@classmethod
def bar(cls):
return 42
# Testing generic bases
T = TypeVar("T")
class GenericParent(Generic[T]):
pass
class NonGenericChild(GenericParent[str]):
pass
# Testing docstring inheritance
class Base:
def __init__(self):
"""init"""
super().__init__()
def foo(self):
"""foo"""
pass
@classmethod
def bar(cls):
"""bar"""
pass
@staticmethod
def baz():
"""baz"""
pass
@property
def qux(self):
"""qux"""
return
# This is not supported by inspect.getdoc yet.
@cached_property
def quux(self):
"""quux"""
return
quuux: int = 42
"""quuux"""
class Child(Base):
def __init__(self):
super().__init__()
def foo(self):
pass
@classmethod
def bar(cls):
pass
@staticmethod
def baz():
pass
@property
def qux(self):
return
@cached_property
def quux(self):
return
quuux: int = 42
# Testing a proper __module__, but no useful __qualname__ attribute.
bad_qualname = demo_long.DataDemo.__init__
# Testing that an attribute that is only annotated does not trigger a "submodule not found" warning.
only_annotated: int
# Testing that Exceptions render properly
class CustomException(RuntimeError):
"""custom exception type"""
# Testing that a private class in __all__ is displayed
class _Private:
"""private class"""
pass
def _do(self):
"""private method"""
# Testing a class attribute that is a lambda (which generates quirky sources)
class LambdaAttr:
# not really supported, but also shouldn't crash.
attr = lambda x: 42 # noqa
# Testing different docstring annotations
# fmt: off
def foo():
"""no indents"""
def bar():
"""no
indents"""
def baz():
"""one
indent"""
def qux():
"""
two
indents
"""
class Indented:
def foo(self):
"""no indents"""
def bar(self):
"""no
indents"""
def baz(self):
"""one
indent"""
def qux(self):
"""
two
indents
"""
@lru_cache()
def foo_decorated(self):
"""no indents"""
@lru_cache()
# comment
def foo_commented(self):
"""no indents"""
@lru_cache()
def bar_decorated(self):
"""no
indents"""
@lru_cache()
def baz_decorated(self):
"""one
indent"""
@lru_cache()
def qux_decorated(self):
"""
two
indents
"""
@lru_cache(
maxsize=42
)
def quux_decorated(self):
"""multi-line decorator, https://github.com/mitmproxy/pdoc/issues/246"""
def _protected_decorator(f):
return f
@_protected_decorator
def fun_with_protected_decorator():
"""This function has a protected decorator (name starting with a single `_`)."""
class UnhashableDataDescriptor:
def __get__(self):
pass
__hash__ = None # type: ignore
unhashable = UnhashableDataDescriptor()
class AbstractClass(metaclass=abc.ABCMeta):
"""This class shouldn't show a constructor as it's abstract."""
@abc.abstractmethod
def foo(self):
pass
__all__ = [ # noqa
"Issue226",
"var_with_default_obj",
"var_with_default_func",
"func_with_defaults",
"ClassmethodLink",
"GenericParent",
"NonGenericChild",
"Child",
"bad_qualname",
"only_annotated",
"CustomException",
"_Private",
"LambdaAttr",
"foo",
"bar",
"baz",
"qux",
"Indented",
"fun_with_protected_decorator",
"unhashable",
"AbstractClass",
]