sphinx/doc/development/tutorials/examples/autodoc_intenum.py
Chris Sewell 3bedde26a9
🔧 Ruff format python files within docs folder (#12139)
Remove `docs` files from the `exclude` list, and also add the `preview = false` format config, which is shown to reduce diffs to the current code base
2024-03-19 15:23:57 +01:00

58 lines
1.7 KiB
Python

from __future__ import annotations
from enum import IntEnum
from typing import TYPE_CHECKING, Any
from sphinx.ext.autodoc import ClassDocumenter, bool_option
if TYPE_CHECKING:
from docutils.statemachine import StringList
from sphinx.application import Sphinx
class IntEnumDocumenter(ClassDocumenter):
objtype = 'intenum'
directivetype = ClassDocumenter.objtype
priority = 10 + ClassDocumenter.priority
option_spec = dict(ClassDocumenter.option_spec)
option_spec['hex'] = bool_option
@classmethod
def can_document_member(
cls, member: Any, membername: str, isattr: bool, parent: Any
) -> bool:
try:
return issubclass(member, IntEnum)
except TypeError:
return False
def add_directive_header(self, sig: str) -> None:
super().add_directive_header(sig)
self.add_line(' :final:', self.get_sourcename())
def add_content(
self,
more_content: StringList | None,
no_docstring: bool = False,
) -> None:
super().add_content(more_content, no_docstring)
source_name = self.get_sourcename()
enum_object: IntEnum = self.object
use_hex = self.options.hex
self.add_line('', source_name)
for the_member_name, enum_member in enum_object.__members__.items():
the_member_value = enum_member.value
if use_hex:
the_member_value = hex(the_member_value)
self.add_line(f'**{the_member_name}**: {the_member_value}', source_name)
self.add_line('', source_name)
def setup(app: Sphinx) -> None:
app.setup_extension('sphinx.ext.autodoc') # Require autodoc extension
app.add_autodocumenter(IntEnumDocumenter)