@@ -401,6 +401,7 @@ class Context(dict):
401
401
If you don't pass your own `Context` instance to `Module` constructor,
402
402
a global context object will be used.
403
403
"""
404
+ __pdoc__ ['Context.__init__' ] = False
404
405
405
406
406
407
_global_context = Context ()
@@ -981,7 +982,8 @@ def _link_inheritance(self):
981
982
'use `__pdoc__[key] = False` '
982
983
'(key: {!r}, module: {!r}).' .format (name , self .name ))
983
984
984
- if name not in self .doc and refname not in self ._context :
985
+ if (not name .endswith ('.__init__' ) and
986
+ name not in self .doc and refname not in self ._context ):
985
987
warn ('__pdoc__-overriden key {!r} does not exist '
986
988
'in module {!r}' .format (name , self .name ))
987
989
@@ -1073,6 +1075,10 @@ def find_ident(self, name: str) -> Doc:
1073
1075
`External` is returned populated with the given identifier.
1074
1076
"""
1075
1077
_name = name .rstrip ('()' ) # Function specified with parentheses
1078
+
1079
+ if _name .endswith ('.__init__' ): # Ref to class' init is ref to class itself
1080
+ _name = _name [:- len ('.__init__' )]
1081
+
1076
1082
return (self .doc .get (_name ) or
1077
1083
self ._context .get (_name ) or
1078
1084
self ._context .get (self .name + '.' + _name ) or
@@ -1127,6 +1133,11 @@ class Class(Doc):
1127
1133
1128
1134
def __init__ (self , name , module , obj , * , docstring = None ):
1129
1135
assert isinstance (obj , type )
1136
+ if docstring is None :
1137
+ init_doc = inspect .getdoc (obj .__init__ ) or ''
1138
+ if init_doc == object .__init__ .__doc__ :
1139
+ init_doc = ''
1140
+ docstring = ((inspect .getdoc (obj ) or '' ) + '\n \n ' + init_doc ).strip ()
1130
1141
super ().__init__ (name , module , obj , docstring = docstring )
1131
1142
1132
1143
self .doc = {}
@@ -1138,8 +1149,7 @@ def __init__(self, name, module, obj, *, docstring=None):
1138
1149
for name , obj in inspect .getmembers (self .obj )
1139
1150
# Filter only *own* members. The rest are inherited
1140
1151
# in Class._fill_inheritance()
1141
- if (name in self .obj .__dict__ and
1142
- (_is_public (name ) or name == '__init__' ))]
1152
+ if name in self .obj .__dict__ and _is_public (name )]
1143
1153
index = list (self .obj .__dict__ ).index
1144
1154
public_objs .sort (key = lambda i : index (i [0 ]))
1145
1155
@@ -1214,6 +1224,22 @@ def subclasses(self) -> List['Class']:
1214
1224
return [self .module .find_class (c )
1215
1225
for c in type .__subclasses__ (self .obj )]
1216
1226
1227
+ def params (self , * , annotate = False ) -> List ['str' ]:
1228
+ """
1229
+ Return a list of formatted parameters accepted by the
1230
+ class constructor (method `__init__`). See `pdoc.Function.params`.
1231
+ """
1232
+ name = self .name + '.__init__'
1233
+ qualname = self .qualname + '.__init__'
1234
+ refname = self .refname + '.__init__'
1235
+ exclusions = getattr (self .module .obj , "__pdoc__" , {})
1236
+ if name in exclusions or qualname in exclusions or refname in exclusions :
1237
+ return []
1238
+
1239
+ params = Function ._params (self .obj .__init__ , annotate = annotate )
1240
+ params = params [1 :] if params [0 ] == 'self' else params
1241
+ return params
1242
+
1217
1243
def _filter_doc_objs (self , type : Type [T ], include_inherited = True ,
1218
1244
filter_func : Callable [[T ], bool ] = lambda x : True ,
1219
1245
sort = True ) -> List [T ]:
@@ -1319,7 +1345,7 @@ class Function(Doc):
1319
1345
1320
1346
def __init__ (self , name , module , obj , * , cls : Class = None , method = False ):
1321
1347
"""
1322
- Same as `pdoc.Doc.__init__ `, except `obj` must be a
1348
+ Same as `pdoc.Doc`, except `obj` must be a
1323
1349
Python function object. The docstring is gathered automatically.
1324
1350
1325
1351
`cls` should be set when this is a method or a static function
@@ -1392,8 +1418,12 @@ def params(self, *, annotate: bool = False) -> List[str]:
1392
1418
1393
1419
[PEP 484]: https://www.python.org/dev/peps/pep-0484/
1394
1420
"""
1421
+ return self ._params (self .obj , annotate = annotate )
1422
+
1423
+ @staticmethod
1424
+ def _params (func_obj , annotate = False ):
1395
1425
try :
1396
- signature = inspect .signature (inspect .unwrap (self . obj ))
1426
+ signature = inspect .signature (inspect .unwrap (func_obj ))
1397
1427
except ValueError :
1398
1428
# I guess this is for C builtin functions?
1399
1429
return ["..." ]
@@ -1436,10 +1466,6 @@ def __repr__(self):
1436
1466
1437
1467
return params
1438
1468
1439
- def __lt__ (self , other ):
1440
- # Push __init__ to the top.
1441
- return self .name == '__init__' or super ().__lt__ (other )
1442
-
1443
1469
@property
1444
1470
def refname (self ):
1445
1471
return (self .cls .refname if self .cls else self .module .refname ) + '.' + self .name
@@ -1455,7 +1481,7 @@ class Variable(Doc):
1455
1481
def __init__ (self , name , module , docstring , * ,
1456
1482
obj = None , cls : Class = None , instance_var = False ):
1457
1483
"""
1458
- Same as `pdoc.Doc.__init__ `, except `cls` should be provided
1484
+ Same as `pdoc.Doc`, except `cls` should be provided
1459
1485
as a `pdoc.Class` object when this is a class or instance
1460
1486
variable.
1461
1487
"""
0 commit comments