Skip to content

Commit 90c5fd0

Browse files
committed
Don't subclass autodoc_traits, since we need to override too much
1 parent 7c4002b commit 90c5fd0

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

docs/extensions/serverprocess_documenter.py

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
1-
from autodoc_traits import ConfigurableDocumenter, TraitDocumenter
1+
"""
2+
A modified version of https://github.com/jupyterhub/autodoc-traits/tree/1.2.2
3+
for documenting trait fields that are used to configure another object, but
4+
where the traitlet cannot be set directly.
5+
6+
This is used to generate the Server Process options documentation:
7+
https://github.com/jupyterhub/jupyter-server-proxy/blob/main/docs/source/server-process.md
8+
"""
9+
210
from sphinx.application import Sphinx
3-
from sphinx.ext.autodoc import SUPPRESS, ClassDocumenter, ObjectMember
11+
from sphinx.ext.autodoc import (
12+
SUPPRESS,
13+
AttributeDocumenter,
14+
ClassDocumenter,
15+
ObjectMember,
16+
)
417
from sphinx.util.typing import ExtensionMetadata
5-
from traitlets import Undefined
18+
from traitlets import MetaHasTraits, TraitType, Undefined
19+
620

21+
class ServerProcessConfigurableDocumenter(ClassDocumenter):
22+
"""
23+
A modified version of autodoc_traits.ConfigurableDocumenter that only documents
24+
the traits in this class, not the inherited traits.
25+
https://github.com/jupyterhub/autodoc-traits/blob/1.2.2/autodoc_traits.py#L20-L122
26+
"""
727

8-
class ServerProcessConfigurableDocumenter(ConfigurableDocumenter):
928
objtype = "serverprocessconfigurable"
1029
directivetype = "class"
11-
priority = 100
30+
priority = 100 # higher priority than ClassDocumenter's 10
31+
32+
@classmethod
33+
def can_document_member(cls, member, membername, isattr, parent):
34+
return isinstance(member, MetaHasTraits)
1235

1336
def get_object_members(self, want_all):
1437
"""
@@ -21,20 +44,27 @@ def get_object_members(self, want_all):
2144
def should_suppress_directive_header():
2245
return True
2346

24-
# Skip over autodoc_traits otherwise it'll prepend c.ServerProcess
25-
# to the annotation
2647
def add_directive_header(self, sig):
2748
print(f"{sig=}")
2849
self.options.annotation = SUPPRESS
29-
super(ClassDocumenter, self).add_directive_header(sig)
50+
super().add_directive_header(sig)
51+
3052

53+
class ServerProcessTraitDocumenter(AttributeDocumenter):
54+
"""
55+
A modified version of autodoc_traits.TraitDocumenter that omits the c.ClassName prefix
56+
https://github.com/jupyterhub/autodoc-traits/blob/1.2.2/autodoc_traits.py#L125-L203
57+
"""
3158

32-
class ServerProcessTraitDocumenter(TraitDocumenter):
3359
objtype = "serverprocesstrait"
3460
directivetype = "attribute"
3561
priority = 100 # AttributeDocumenter has 10
3662
member_order = 0 # AttributeDocumenter has 60
3763

64+
@classmethod
65+
def can_document_member(cls, member, membername, isattr, parent):
66+
return isinstance(member, TraitType)
67+
3868
def add_directive_header(self, sig):
3969
default_value = self.object.default_value
4070
if default_value is Undefined:
@@ -44,13 +74,11 @@ def add_directive_header(self, sig):
4474

4575
traitlets_type = self.object.__class__.__name__
4676
self.options.annotation = f"{traitlets_type}({default_value})"
47-
# Skip over autodoc_traits otherwise it'll prepend c.ServerProcess
48-
# to the annotation
49-
super(TraitDocumenter, self).add_directive_header(sig)
77+
super().add_directive_header(sig)
5078

5179

5280
def setup(app: Sphinx) -> ExtensionMetadata:
53-
app.setup_extension("autodoc_traits")
81+
app.setup_extension("sphinx.ext.autodoc")
5482
app.add_autodocumenter(ServerProcessConfigurableDocumenter)
5583
app.add_autodocumenter(ServerProcessTraitDocumenter)
5684
return {

0 commit comments

Comments
 (0)