@@ -129,6 +129,14 @@ def member_order_option(arg: Any) -> Optional[str]:
129129 raise ValueError (__ ('invalid value for member-order option: %s' ) % arg )
130130
131131
132+ def class_doc_from_option (arg : Any ) -> Optional [str ]:
133+ """Used to convert the :class-doc-from: option to autoclass directives."""
134+ if arg in ('both' , 'class' , 'init' ):
135+ return arg
136+ else :
137+ raise ValueError (__ ('invalid value for class-doc-from option: %s' ) % arg )
138+
139+
132140SUPPRESS = object ()
133141
134142
@@ -1320,12 +1328,12 @@ def format_signature(self, **kwargs: Any) -> str:
13201328 if typ is object :
13211329 pass # default implementation. skipped.
13221330 else :
1323- self .annotate_to_first_argument (func , typ )
1324-
1325- documenter = FunctionDocumenter (self .directive , '' )
1326- documenter .object = func
1327- documenter .objpath = [None ]
1328- sigs .append (documenter .format_signature ())
1331+ dispatchfunc = self .annotate_to_first_argument (func , typ )
1332+ if dispatchfunc :
1333+ documenter = FunctionDocumenter (self .directive , '' )
1334+ documenter .object = dispatchfunc
1335+ documenter .objpath = [None ]
1336+ sigs .append (documenter .format_signature ())
13291337 if overloaded :
13301338 actual = inspect .signature (self .object ,
13311339 type_aliases = self .config .autodoc_type_aliases )
@@ -1350,28 +1358,34 @@ def merge_default_value(self, actual: Signature, overload: Signature) -> Signatu
13501358
13511359 return overload .replace (parameters = parameters )
13521360
1353- def annotate_to_first_argument (self , func : Callable , typ : Type ) -> None :
1361+ def annotate_to_first_argument (self , func : Callable , typ : Type ) -> Optional [ Callable ] :
13541362 """Annotate type hint to the first argument of function if needed."""
13551363 try :
13561364 sig = inspect .signature (func , type_aliases = self .config .autodoc_type_aliases )
13571365 except TypeError as exc :
13581366 logger .warning (__ ("Failed to get a function signature for %s: %s" ),
13591367 self .fullname , exc )
1360- return
1368+ return None
13611369 except ValueError :
1362- return
1370+ return None
13631371
13641372 if len (sig .parameters ) == 0 :
1365- return
1373+ return None
1374+
1375+ def dummy ():
1376+ pass
13661377
13671378 params = list (sig .parameters .values ())
13681379 if params [0 ].annotation is Parameter .empty :
13691380 params [0 ] = params [0 ].replace (annotation = typ )
13701381 try :
1371- func .__signature__ = sig .replace (parameters = params ) # type: ignore
1382+ dummy .__signature__ = sig .replace (parameters = params ) # type: ignore
1383+ return dummy
13721384 except (AttributeError , TypeError ):
13731385 # failed to update signature (ex. built-in or extension types)
1374- return
1386+ return None
1387+ else :
1388+ return None
13751389
13761390
13771391class DecoratorDocumenter (FunctionDocumenter ):
@@ -1417,6 +1431,7 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
14171431 'show-inheritance' : bool_option , 'member-order' : member_order_option ,
14181432 'exclude-members' : exclude_members_option ,
14191433 'private-members' : members_option , 'special-members' : members_option ,
1434+ 'class-doc-from' : class_doc_from_option ,
14201435 }
14211436
14221437 _signature_class : Any = None
@@ -1651,7 +1666,7 @@ def get_doc(self, ignore: int = None) -> Optional[List[List[str]]]:
16511666 if lines is not None :
16521667 return lines
16531668
1654- content = self .config .autoclass_content
1669+ classdoc_from = self .options . get ( 'class-doc-from' , self . config .autoclass_content )
16551670
16561671 docstrings = []
16571672 attrdocstring = self .get_attr (self .object , '__doc__' , None )
@@ -1660,7 +1675,7 @@ def get_doc(self, ignore: int = None) -> Optional[List[List[str]]]:
16601675
16611676 # for classes, what the "docstring" is can be controlled via a
16621677 # config value; the default is only the class docstring
1663- if content in ('both' , 'init' ):
1678+ if classdoc_from in ('both' , 'init' ):
16641679 __init__ = self .get_attr (self .object , '__init__' , None )
16651680 initdocstring = getdoc (__init__ , self .get_attr ,
16661681 self .config .autodoc_inherit_docstrings ,
@@ -1682,7 +1697,7 @@ def get_doc(self, ignore: int = None) -> Optional[List[List[str]]]:
16821697 initdocstring .strip () == object .__new__ .__doc__ )): # for !pypy
16831698 initdocstring = None
16841699 if initdocstring :
1685- if content == 'init' :
1700+ if classdoc_from == 'init' :
16861701 docstrings = [initdocstring ]
16871702 else :
16881703 docstrings .append (initdocstring )
@@ -2109,13 +2124,13 @@ def format_signature(self, **kwargs: Any) -> str:
21092124 if typ is object :
21102125 pass # default implementation. skipped.
21112126 else :
2112- self .annotate_to_first_argument (func , typ )
2113-
2114- documenter = MethodDocumenter (self .directive , '' )
2115- documenter .parent = self .parent
2116- documenter .object = func
2117- documenter .objpath = [None ]
2118- sigs .append (documenter .format_signature ())
2127+ dispatchmeth = self .annotate_to_first_argument (func , typ )
2128+ if dispatchmeth :
2129+ documenter = MethodDocumenter (self .directive , '' )
2130+ documenter .parent = self .parent
2131+ documenter .object = dispatchmeth
2132+ documenter .objpath = [None ]
2133+ sigs .append (documenter .format_signature ())
21192134 if overloaded :
21202135 if inspect .isstaticmethod (self .object , cls = self .parent , name = self .object_name ):
21212136 actual = inspect .signature (self .object , bound_method = False ,
@@ -2149,27 +2164,34 @@ def merge_default_value(self, actual: Signature, overload: Signature) -> Signatu
21492164
21502165 return overload .replace (parameters = parameters )
21512166
2152- def annotate_to_first_argument (self , func : Callable , typ : Type ) -> None :
2167+ def annotate_to_first_argument (self , func : Callable , typ : Type ) -> Optional [ Callable ] :
21532168 """Annotate type hint to the first argument of function if needed."""
21542169 try :
21552170 sig = inspect .signature (func , type_aliases = self .config .autodoc_type_aliases )
21562171 except TypeError as exc :
21572172 logger .warning (__ ("Failed to get a method signature for %s: %s" ),
21582173 self .fullname , exc )
2159- return
2174+ return None
21602175 except ValueError :
2161- return
2176+ return None
2177+
21622178 if len (sig .parameters ) == 1 :
2163- return
2179+ return None
2180+
2181+ def dummy ():
2182+ pass
21642183
21652184 params = list (sig .parameters .values ())
21662185 if params [1 ].annotation is Parameter .empty :
21672186 params [1 ] = params [1 ].replace (annotation = typ )
21682187 try :
2169- func .__signature__ = sig .replace (parameters = params ) # type: ignore
2188+ dummy .__signature__ = sig .replace (parameters = params ) # type: ignore
2189+ return dummy
21702190 except (AttributeError , TypeError ):
21712191 # failed to update signature (ex. built-in or extension types)
2172- return
2192+ return None
2193+ else :
2194+ return None
21732195
21742196
21752197class NonDataDescriptorMixin (DataDocumenterMixinBase ):
0 commit comments