1212import sphinx
1313from sphinx .config import ENUM
1414from sphinx .ext .autodoc ._directive import AutodocDirective
15- from sphinx .ext .autodoc ._directive_options import (
15+ from sphinx .ext .autodoc ._event_listeners import between , cut_lines
16+ from sphinx .ext .autodoc ._legacy_class_based ._directive_options import (
17+ Options ,
1618 annotation_option ,
1719 bool_option ,
1820 class_doc_from_option ,
2325 members_option ,
2426 merge_members_option ,
2527)
26- from sphinx .ext .autodoc ._dynamic ._member_finder import ObjectMember , special_member_re
27- from sphinx .ext .autodoc ._event_listeners import between , cut_lines
28- from sphinx .ext .autodoc ._names import py_ext_sig_re
29- from sphinx .ext .autodoc ._sentinels import ALL , EMPTY , SUPPRESS , UNINITIALIZED_ATTR
30- from sphinx .ext .autodoc ._sentinels import INSTANCE_ATTR as INSTANCEATTR
31- from sphinx .ext .autodoc ._sentinels import SLOTS_ATTR as SLOTSATTR
28+ from sphinx .ext .autodoc ._legacy_class_based ._documenters import (
29+ AttributeDocumenter ,
30+ ClassDocumenter ,
31+ ClassLevelDocumenter ,
32+ DataDocumenter ,
33+ DataDocumenterMixinBase ,
34+ DecoratorDocumenter ,
35+ DocstringSignatureMixin ,
36+ DocstringStripSignatureMixin ,
37+ Documenter ,
38+ ExceptionDocumenter ,
39+ FunctionDocumenter ,
40+ GenericAliasMixin ,
41+ MethodDocumenter ,
42+ ModuleDocumenter ,
43+ ModuleLevelDocumenter ,
44+ NonDataDescriptorMixin ,
45+ ObjectMember ,
46+ PropertyDocumenter ,
47+ RuntimeInstanceAttributeMixin ,
48+ SlotsMixin ,
49+ UninitializedGlobalVariableMixin ,
50+ UninitializedInstanceAttributeMixin ,
51+ autodoc_attrgetter ,
52+ py_ext_sig_re ,
53+ special_member_re ,
54+ )
55+ from sphinx .ext .autodoc ._legacy_class_based ._sentinels import (
56+ ALL ,
57+ EMPTY ,
58+ INSTANCEATTR ,
59+ SLOTSATTR ,
60+ SUPPRESS ,
61+ UNINITIALIZED_ATTR ,
62+ )
3263from sphinx .ext .autodoc .typehints import _merge_typehints
3364
3465if TYPE_CHECKING :
3566 from sphinx .application import Sphinx
67+ from sphinx .config import Config
3668 from sphinx .ext .autodoc ._property_types import _AutodocObjType
3769 from sphinx .util .typing import ExtensionMetadata
3870
3971__all__ = (
4072 # Useful event listener factories for autodoc-process-docstring
4173 'cut_lines' ,
4274 'between' ,
75+ # Documenters
76+ 'AttributeDocumenter' ,
77+ 'ClassDocumenter' ,
78+ 'DataDocumenter' ,
79+ 'DecoratorDocumenter' ,
80+ 'ExceptionDocumenter' ,
81+ 'FunctionDocumenter' ,
82+ 'MethodDocumenter' ,
83+ 'ModuleDocumenter' ,
84+ 'PropertyDocumenter' ,
4385 # This class is only used in ``sphinx.ext.autodoc.directive``,
4486 # but we export it here for compatibility.
4587 # See: https://github.com/sphinx-doc/sphinx/issues/4538
46- # 'Options',
88+ 'Options' ,
4789 # Option spec functions.
4890 # Exported for compatibility.
4991 'annotation_option' ,
68110 'ObjectMember' ,
69111 'py_ext_sig_re' ,
70112 'special_member_re' ,
113+ 'ModuleLevelDocumenter' ,
114+ 'ClassLevelDocumenter' ,
115+ 'DocstringSignatureMixin' ,
116+ 'DocstringStripSignatureMixin' ,
117+ 'DataDocumenterMixinBase' ,
118+ 'GenericAliasMixin' ,
119+ 'UninitializedGlobalVariableMixin' ,
120+ 'NonDataDescriptorMixin' ,
121+ 'SlotsMixin' ,
122+ 'RuntimeInstanceAttributeMixin' ,
123+ 'UninitializedInstanceAttributeMixin' ,
124+ 'autodoc_attrgetter' ,
125+ 'Documenter' ,
71126)
72127
73128
74129def setup (app : Sphinx ) -> ExtensionMetadata :
75- obj_type : _AutodocObjType
76- for obj_type in (
77- 'module' ,
78- 'class' ,
79- 'exception' ,
80- 'function' ,
81- 'decorator' ,
82- 'method' ,
83- 'property' ,
84- 'attribute' ,
85- 'data' ,
86- 'type' ,
87- ):
88- # register the automodule, autoclass, etc. directives
89- app .add_directive (f'auto{ obj_type } ' , AutodocDirective )
90-
91130 app .add_config_value (
92131 'autoclass_content' ,
93132 'class' ,
@@ -142,6 +181,9 @@ def setup(app: Sphinx) -> ExtensionMetadata:
142181 app .add_config_value (
143182 'autodoc_use_type_comments' , True , 'env' , types = frozenset ({bool })
144183 )
184+ app .add_config_value (
185+ 'autodoc_use_legacy_class_based' , False , 'env' , types = frozenset ({bool })
186+ )
145187
146188 app .add_event ('autodoc-before-process-signature' )
147189 app .add_event ('autodoc-process-docstring' )
@@ -151,7 +193,50 @@ def setup(app: Sphinx) -> ExtensionMetadata:
151193
152194 app .connect ('object-description-transform' , _merge_typehints )
153195
196+ app .connect ('config-inited' , _register_directives )
197+
154198 return {
155199 'version' : sphinx .__display_version__ ,
156200 'parallel_read_safe' : True ,
157201 }
202+
203+
204+ def _register_directives (app : Sphinx , config : Config ) -> None :
205+ if not config .autodoc_use_legacy_class_based :
206+ obj_type : _AutodocObjType
207+ for obj_type in (
208+ 'module' ,
209+ 'class' ,
210+ 'exception' ,
211+ 'function' ,
212+ 'decorator' ,
213+ 'method' ,
214+ 'property' ,
215+ 'attribute' ,
216+ 'data' ,
217+ 'type' ,
218+ ):
219+ # register the automodule, autoclass, etc. directives
220+ app .add_directive (f'auto{ obj_type } ' , AutodocDirective )
221+ else :
222+ from sphinx .ext .autodoc .preserve_defaults import update_defvalue
223+ from sphinx .ext .autodoc .type_comment import (
224+ update_annotations_using_type_comments ,
225+ )
226+ from sphinx .ext .autodoc .typehints import record_typehints
227+
228+ app .add_autodocumenter (ModuleDocumenter )
229+ app .add_autodocumenter (ClassDocumenter )
230+ app .add_autodocumenter (ExceptionDocumenter )
231+ app .add_autodocumenter (DataDocumenter )
232+ app .add_autodocumenter (FunctionDocumenter )
233+ app .add_autodocumenter (DecoratorDocumenter )
234+ app .add_autodocumenter (MethodDocumenter )
235+ app .add_autodocumenter (AttributeDocumenter )
236+ app .add_autodocumenter (PropertyDocumenter )
237+
238+ app .connect ('autodoc-before-process-signature' , update_defvalue )
239+ app .connect (
240+ 'autodoc-before-process-signature' , update_annotations_using_type_comments
241+ )
242+ app .connect ('autodoc-process-signature' , record_typehints )
0 commit comments