mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
refactor: autodoc: Add UninitializedGlobalVariableMixin
To make DataDocumenter simple, this divide uninitialized global variable feature from DataDocumenter as UninitializedGlobalVariableMixin.
This commit is contained in:
@@ -1702,6 +1702,9 @@ class ExceptionDocumenter(ClassDocumenter):
|
|||||||
|
|
||||||
class DataDocumenterMixinBase:
|
class DataDocumenterMixinBase:
|
||||||
# define types of instance variables
|
# define types of instance variables
|
||||||
|
config = None # type: Config
|
||||||
|
env = None # type: BuildEnvironment
|
||||||
|
modname = None # type: str
|
||||||
parent = None # type: Any
|
parent = None # type: Any
|
||||||
object = None # type: Any
|
object = None # type: Any
|
||||||
objpath = None # type: List[str]
|
objpath = None # type: List[str]
|
||||||
@@ -1710,6 +1713,10 @@ class DataDocumenterMixinBase:
|
|||||||
"""Check directive header should be suppressed."""
|
"""Check directive header should be suppressed."""
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def should_suppress_value_header(self) -> bool:
|
||||||
|
"""Check :value: header should be suppressed."""
|
||||||
|
return False
|
||||||
|
|
||||||
def update_content(self, more_content: StringList) -> None:
|
def update_content(self, more_content: StringList) -> None:
|
||||||
"""Update docstring for the NewType object."""
|
"""Update docstring for the NewType object."""
|
||||||
pass
|
pass
|
||||||
@@ -1774,25 +1781,15 @@ class TypeVarMixin(DataDocumenterMixinBase):
|
|||||||
super().update_content(more_content)
|
super().update_content(more_content)
|
||||||
|
|
||||||
|
|
||||||
class DataDocumenter(NewTypeMixin, TypeVarMixin, ModuleLevelDocumenter):
|
class UninitializedGlobalVariableMixin(DataDocumenterMixinBase):
|
||||||
"""
|
"""
|
||||||
Specialized Documenter subclass for data items.
|
Mixin for DataDocumenter to provide the feature for supporting uninitialized
|
||||||
|
(type annotation only) global variables.
|
||||||
"""
|
"""
|
||||||
objtype = 'data'
|
|
||||||
member_order = 40
|
|
||||||
priority = -10
|
|
||||||
option_spec = dict(ModuleLevelDocumenter.option_spec)
|
|
||||||
option_spec["annotation"] = annotation_option
|
|
||||||
option_spec["no-value"] = bool_option
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def can_document_member(cls, member: Any, membername: str, isattr: bool, parent: Any
|
|
||||||
) -> bool:
|
|
||||||
return isinstance(parent, ModuleDocumenter) and isattr
|
|
||||||
|
|
||||||
def import_object(self, raiseerror: bool = False) -> bool:
|
def import_object(self, raiseerror: bool = False) -> bool:
|
||||||
try:
|
try:
|
||||||
return super().import_object(raiseerror=True)
|
return super().import_object(raiseerror=True) # type: ignore
|
||||||
except ImportError as exc:
|
except ImportError as exc:
|
||||||
# annotation only instance variable (PEP-526)
|
# annotation only instance variable (PEP-526)
|
||||||
try:
|
try:
|
||||||
@@ -1812,6 +1809,36 @@ class DataDocumenter(NewTypeMixin, TypeVarMixin, ModuleLevelDocumenter):
|
|||||||
self.env.note_reread()
|
self.env.note_reread()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def should_suppress_value_header(self) -> bool:
|
||||||
|
return (self.object == UNINITIALIZED_ATTR or
|
||||||
|
super().should_suppress_value_header())
|
||||||
|
|
||||||
|
def add_content(self, more_content: Optional[StringList], no_docstring: bool = False
|
||||||
|
) -> None:
|
||||||
|
if self.object is UNINITIALIZED_ATTR:
|
||||||
|
# suppress docstring of the value
|
||||||
|
super().add_content(more_content, no_docstring=True) # type: ignore
|
||||||
|
else:
|
||||||
|
super().add_content(more_content, no_docstring=no_docstring) # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
class DataDocumenter(NewTypeMixin, TypeVarMixin, UninitializedGlobalVariableMixin,
|
||||||
|
ModuleLevelDocumenter):
|
||||||
|
"""
|
||||||
|
Specialized Documenter subclass for data items.
|
||||||
|
"""
|
||||||
|
objtype = 'data'
|
||||||
|
member_order = 40
|
||||||
|
priority = -10
|
||||||
|
option_spec = dict(ModuleLevelDocumenter.option_spec)
|
||||||
|
option_spec["annotation"] = annotation_option
|
||||||
|
option_spec["no-value"] = bool_option
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def can_document_member(cls, member: Any, membername: str, isattr: bool, parent: Any
|
||||||
|
) -> bool:
|
||||||
|
return isinstance(parent, ModuleDocumenter) and isattr
|
||||||
|
|
||||||
def add_directive_header(self, sig: str) -> None:
|
def add_directive_header(self, sig: str) -> None:
|
||||||
super().add_directive_header(sig)
|
super().add_directive_header(sig)
|
||||||
sourcename = self.get_sourcename()
|
sourcename = self.get_sourcename()
|
||||||
@@ -1833,7 +1860,7 @@ class DataDocumenter(NewTypeMixin, TypeVarMixin, ModuleLevelDocumenter):
|
|||||||
sourcename)
|
sourcename)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if self.object is UNINITIALIZED_ATTR or self.options.no_value:
|
if self.options.no_value or self.should_suppress_value_header():
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
objrepr = object_description(self.object)
|
objrepr = object_description(self.object)
|
||||||
@@ -1850,15 +1877,11 @@ class DataDocumenter(NewTypeMixin, TypeVarMixin, ModuleLevelDocumenter):
|
|||||||
|
|
||||||
def add_content(self, more_content: Optional[StringList], no_docstring: bool = False
|
def add_content(self, more_content: Optional[StringList], no_docstring: bool = False
|
||||||
) -> None:
|
) -> None:
|
||||||
if self.object is UNINITIALIZED_ATTR:
|
if not more_content:
|
||||||
# suppress docstring of the value
|
more_content = StringList()
|
||||||
super().add_content(more_content, no_docstring=True)
|
|
||||||
else:
|
|
||||||
if not more_content:
|
|
||||||
more_content = StringList()
|
|
||||||
|
|
||||||
self.update_content(more_content)
|
self.update_content(more_content)
|
||||||
super().add_content(more_content, no_docstring=no_docstring)
|
super().add_content(more_content, no_docstring=no_docstring)
|
||||||
|
|
||||||
|
|
||||||
class DataDeclarationDocumenter(DataDocumenter):
|
class DataDeclarationDocumenter(DataDocumenter):
|
||||||
|
|||||||
Reference in New Issue
Block a user