Sage autodoc extension#
This is sphinx.ext.autodoc
extension modified for Sage objects.
The original header of sphinx.ext.autodoc
:
Extension to create automatic documentation from code docstrings.
Automatically insert docstrings for functions, classes or whole modules into the doctree, thus avoiding duplication between docstrings and documentation for those who like elaborate docstrings.
This module is currently based on sphinx.ext.autodoc
from Sphinx version
7.2.6. Compare (do diff) with the upstream source file
sphinx/ext/autodoc/__init__.py.
In the source file of this module, major modifications are delimited by a pair of comment dividers. To lessen maintenance burden, we aim at reducing the modifications.
AUTHORS:
? (before 2011): initial version derived from sphinx.ext.autodoc
Johan S. R. Nielsen: support for _sage_argspec_
Simon King (2011-04): use sageinspect; include public cython attributes only in the documentation if they have a docstring
Kwankyu Lee (2018-12-26, 2022-11-08): rebased on the latest sphinx.ext.autodoc
Kwankyu Lee (2024-02-14): rebased on Sphinx 7.2.6
- class sage_docbuild.ext.sage_autodoc.AttributeDocumenter(directive: DocumenterBridge, name: str, indent: str = '')[source]#
Bases:
GenericAliasMixin
,SlotsMixin
,RuntimeInstanceAttributeMixin
,UninitializedInstanceAttributeMixin
,NonDataDescriptorMixin
,DocstringStripSignatureMixin
,ClassLevelDocumenter
Specialized Documenter subclass for attributes.
- member_order = 60#
order if autodoc_member_order is set to ‘groupwise’
- objtype = 'attribute'#
name by which the directive is called (auto…) and the default generated directive name
- option_spec: OptionSpec = {'annotation': <function annotation_option>, 'no-index': <function bool_option>, 'no-value': <function bool_option>, 'noindex': <function bool_option>}#
- priority = 10#
priority if multiple documenters return True from can_document_member
- class sage_docbuild.ext.sage_autodoc.ClassDocumenter(*args: Any)[source]#
Bases:
DocstringSignatureMixin
,ModuleLevelDocumenter
Specialized Documenter subclass for classes.
- member_order = 20#
order if autodoc_member_order is set to ‘groupwise’
- objtype = 'class'#
name by which the directive is called (auto…) and the default generated directive name
- option_spec: OptionSpec = {'class-doc-from': <function class_doc_from_option>, 'exclude-members': <function exclude_members_option>, 'inherited-members': <function inherited_members_option>, 'member-order': <function member_order_option>, 'members': <function members_option>, 'no-index': <function bool_option>, 'noindex': <function bool_option>, 'private-members': <function members_option>, 'show-inheritance': <function bool_option>, 'special-members': <function members_option>, 'undoc-members': <function bool_option>}#
- priority = 15#
priority if multiple documenters return True from can_document_member
- class sage_docbuild.ext.sage_autodoc.ClassLevelDocumenter(directive: DocumenterBridge, name: str, indent: str = '')[source]#
Bases:
Documenter
Specialized Documenter subclass for objects on class level (methods, attributes).
- class sage_docbuild.ext.sage_autodoc.DataDocumenter(directive: DocumenterBridge, name: str, indent: str = '')[source]#
Bases:
GenericAliasMixin
,UninitializedGlobalVariableMixin
,ModuleLevelDocumenter
Specialized Documenter subclass for data items.
- member_order = 40#
order if autodoc_member_order is set to ‘groupwise’
- objtype = 'data'#
name by which the directive is called (auto…) and the default generated directive name
- option_spec: OptionSpec = {'annotation': <function annotation_option>, 'no-index': <function bool_option>, 'no-value': <function bool_option>, 'noindex': <function bool_option>}#
- priority = -10#
priority if multiple documenters return True from can_document_member
- class sage_docbuild.ext.sage_autodoc.DataDocumenterMixinBase[source]#
Bases:
object
- config: Config#
- env: BuildEnvironment#
- modname: str#
- object: Any#
- objpath: list[str]#
- parent: Any#
- class sage_docbuild.ext.sage_autodoc.DecoratorDocumenter(directive: DocumenterBridge, name: str, indent: str = '')[source]#
Bases:
FunctionDocumenter
Specialized Documenter subclass for decorator functions.
- objtype = 'decorator'#
name by which the directive is called (auto…) and the default generated directive name
- priority = -1#
priority if multiple documenters return True from can_document_member
- class sage_docbuild.ext.sage_autodoc.DocstringSignatureMixin[source]#
Bases:
object
Mixin for FunctionDocumenter and MethodDocumenter to provide the feature of reading the signature from the docstring.
- class sage_docbuild.ext.sage_autodoc.DocstringStripSignatureMixin[source]#
Bases:
DocstringSignatureMixin
Mixin for AttributeDocumenter to provide the feature of stripping any function signature from the docstring.
- class sage_docbuild.ext.sage_autodoc.Documenter(directive: DocumenterBridge, name: str, indent: str = '')[source]#
Bases:
object
A Documenter knows how to autodocument a single object type. When registered with the AutoDirective, it will be used to document objects of that type when needed by autodoc.
Its objtype attribute selects what auto directive it is assigned to (the directive name is ‘auto’ + objtype), and what directive it generates by default, though that can be overridden by an attribute called directivetype.
A Documenter has an option_spec that works like a docutils directive’s; in fact, it will be used to parse an auto directive’s options that matches the Documenter.
- classmethod can_document_member(member, membername, isattr, parent)[source]#
Called to see if a member can be documented by this Documenter.
- content_indent = ' '#
indentation by which to indent the directive content
- document_members(all_members=False)[source]#
Generate reST for member documentation.
If all_members is True, document all members, else those given by self.options.members.
- property documenters: dict[str, type[Documenter]]#
Returns registered Documenter classes
- filter_members(members, want_all)[source]#
Filter the given member list.
Members are skipped if
they are private (except if given explicitly or the private-members option is set)
they are special methods (except if given explicitly or the special-members option is set)
they are undocumented (except if the undoc-members option is set)
The user can override the skipping decision by connecting to the
autodoc-skip-member
event.
- format_args(**kwargs)[source]#
Format the argument signature of self.object.
Should return None if the object does not have a signature.
- format_name()[source]#
Format the name of self.object.
This normally should be something that can be parsed by the generated directive, but doesn’t need to be (Sphinx will display it unparsed then).
- format_signature(**kwargs)[source]#
Format the signature (arguments and return annotation) of the object.
Let the user process it via the
autodoc-process-signature
event.
- generate(more_content=None, real_modname=None, check_module=False, all_members=False)[source]#
Generate reST for the object given by self.name, and possibly for its members.
If more_content is given, include that content. If real_modname is given, use that module name to find attribute docs. If check_module is True, only generate if the object is defined in the module name it is imported from. If all_members is True, document all members.
- get_doc()[source]#
Decode and return lines of the docstring(s) for the object.
When it returns None, autodoc-process-docstring will not be called for this object.
- get_object_members(want_all)[source]#
Return \((members_check_module, members)\) where \(members\) is a list of \((membername, member)\) pairs of the members of self.object.
If want_all is True, return all members. Else, only return those members given by self.options.members (which may also be None).
- get_real_modname()[source]#
Get the real module name of an object to document.
It can differ from the name of the module through which the object was imported.
- import_object(raiseerror=False)[source]#
Import the object given by self.modname and self.objpath and set it as self.object.
Returns True if successful, False if an error occurred.
- member_order = 0#
order if autodoc_member_order is set to ‘groupwise’
- objtype = 'object'#
name by which the directive is called (auto…) and the default generated directive name
- option_spec: OptionSpec = {'no-index': <function bool_option>, 'noindex': <function bool_option>}#
- parse_name()[source]#
Determine what module to import and what attribute to document.
Returns True and sets self.modname, self.objpath, self.fullname, self.args and self.retann if parsing and resolving was successful.
- priority = 0#
priority if multiple documenters return True from can_document_member
- resolve_name(modname, parents, path, base)[source]#
Resolve the module and name of the object to document given by the arguments and the current module/class.
Must return a pair of the module name and a chain of attributes; for example, it would return
('zipfile', ['ZipFile', 'open'])
for thezipfile.ZipFile.open
method.
- titles_allowed = True#
true if the generated content may contain titles
- class sage_docbuild.ext.sage_autodoc.ExceptionDocumenter(*args: Any)[source]#
Bases:
ClassDocumenter
Specialized ClassDocumenter subclass for exceptions.
- member_order = 10#
order if autodoc_member_order is set to ‘groupwise’
- objtype = 'exception'#
name by which the directive is called (auto…) and the default generated directive name
- priority = 20#
priority if multiple documenters return True from can_document_member
- class sage_docbuild.ext.sage_autodoc.FunctionDocumenter(directive: DocumenterBridge, name: str, indent: str = '')[source]#
Bases:
DocstringSignatureMixin
,ModuleLevelDocumenter
Specialized Documenter subclass for functions.
- annotate_to_first_argument(func, typ)[source]#
Annotate type hint to the first argument of function if needed.
- member_order = 30#
order if autodoc_member_order is set to ‘groupwise’
- merge_default_value(actual, overload)[source]#
Merge default values of actual implementation to the overload variants.
- objtype = 'function'#
name by which the directive is called (auto…) and the default generated directive name
- class sage_docbuild.ext.sage_autodoc.GenericAliasMixin[source]#
Bases:
DataDocumenterMixinBase
Mixin for DataDocumenter and AttributeDocumenter to provide the feature for supporting GenericAliases.
- sage_docbuild.ext.sage_autodoc.INSTANCEATTR = <object object>#
The base class of the class hierarchy.
When called, it accepts no arguments and returns a new featureless instance that has no instance attributes and cannot be given any.
- class sage_docbuild.ext.sage_autodoc.MethodDocumenter(directive: DocumenterBridge, name: str, indent: str = '')[source]#
Bases:
DocstringSignatureMixin
,ClassLevelDocumenter
Specialized Documenter subclass for methods (normal, static and class).
- annotate_to_first_argument(func, typ)[source]#
Annotate type hint to the first argument of function if needed.
- directivetype = 'method'#
- member_order = 50#
order if autodoc_member_order is set to ‘groupwise’
- merge_default_value(actual, overload)[source]#
Merge default values of actual implementation to the overload variants.
- objtype = 'method'#
name by which the directive is called (auto…) and the default generated directive name
- priority = 1#
priority if multiple documenters return True from can_document_member
- class sage_docbuild.ext.sage_autodoc.ModuleDocumenter(*args: Any)[source]#
Bases:
Documenter
Specialized Documenter subclass for modules.
- content_indent = ''#
indentation by which to indent the directive content
- objtype = 'module'#
name by which the directive is called (auto…) and the default generated directive name
- option_spec: OptionSpec = {'deprecated': <function bool_option>, 'exclude-members': <function exclude_members_option>, 'ignore-module-all': <function bool_option>, 'imported-members': <function bool_option>, 'inherited-members': <function inherited_members_option>, 'member-order': <function member_order_option>, 'members': <function members_option>, 'no-index': <function bool_option>, 'no-value': <function bool_option>, 'noindex': <function bool_option>, 'platform': <function identity>, 'private-members': <function members_option>, 'show-inheritance': <function bool_option>, 'special-members': <function members_option>, 'synopsis': <function identity>, 'undoc-members': <function bool_option>}#
- class sage_docbuild.ext.sage_autodoc.ModuleLevelDocumenter(directive: DocumenterBridge, name: str, indent: str = '')[source]#
Bases:
Documenter
Specialized Documenter subclass for objects on module level (functions, classes, data/constants).
- class sage_docbuild.ext.sage_autodoc.NonDataDescriptorMixin[source]#
Bases:
DataDocumenterMixinBase
Mixin for AttributeDocumenter to provide the feature for supporting non data-descriptors.
Note
This mix-in must be inherited after other mix-ins. Otherwise, docstring and :value: header will be suppressed unexpectedly.
- class sage_docbuild.ext.sage_autodoc.ObjectMember(name: str, obj: Any, *, docstring: str | None = None, class_: Any | None = None, skipped: bool = False)[source]#
Bases:
object
A member of object.
This is used for the result of \(Documenter.get_module_members()\) to represent each member of the object.
Note
An instance of this class behaves as a tuple of (name, object) for compatibility to old Sphinx. The behavior will be dropped in the future. Therefore extensions should not use the tuple interface.
- class sage_docbuild.ext.sage_autodoc.Options[source]#
Bases:
dict
A dict/attribute hybrid that returns None on nonexisting keys.
- class sage_docbuild.ext.sage_autodoc.PropertyDocumenter(directive: DocumenterBridge, name: str, indent: str = '')[source]#
Bases:
DocstringStripSignatureMixin
,ClassLevelDocumenter
Specialized Documenter subclass for properties.
- import_object(raiseerror=False)[source]#
Check the exisitence of uninitialized instance attribute when failed to import the attribute.
- member_order = 60#
order if autodoc_member_order is set to ‘groupwise’
- objtype = 'property'#
name by which the directive is called (auto…) and the default generated directive name
- priority = 11#
priority if multiple documenters return True from can_document_member
- class sage_docbuild.ext.sage_autodoc.RuntimeInstanceAttributeMixin[source]#
Bases:
DataDocumenterMixinBase
Mixin for AttributeDocumenter to provide the feature for supporting runtime instance attributes (that are defined in __init__() methods with doc-comments).
Example:
- class Foo:
- def __init__(self):
self.attr = None #: This is a target of this mix-in.
- RUNTIME_INSTANCE_ATTRIBUTE#
- import_object(raiseerror=False)[source]#
Check the existence of runtime instance attribute after failing to import the attribute.
- is_runtime_instance_attribute(parent)[source]#
Check the subject is an attribute defined in __init__().
- class sage_docbuild.ext.sage_autodoc.SlotsMixin[source]#
Bases:
DataDocumenterMixinBase
Mixin for AttributeDocumenter to provide the feature for supporting __slots__.
- class sage_docbuild.ext.sage_autodoc.UninitializedGlobalVariableMixin[source]#
Bases:
DataDocumenterMixinBase
Mixin for DataDocumenter to provide the feature for supporting uninitialized (type annotation only) global variables.
- class sage_docbuild.ext.sage_autodoc.UninitializedInstanceAttributeMixin[source]#
Bases:
DataDocumenterMixinBase
Mixin for AttributeDocumenter to provide the feature for supporting uninitialized instance attributes (PEP-526 styled, annotation only attributes).
Example:
- class Foo:
attr: int #: This is a target of this mix-in.
- import_object(raiseerror=False)[source]#
Check the exisitence of uninitialized instance attribute when failed to import the attribute.
- sage_docbuild.ext.sage_autodoc.autodoc_attrgetter(app, obj, name, *defargs)[source]#
Alternative getattr() for types
- sage_docbuild.ext.sage_autodoc.between(marker, what=None, keepempty=False, exclude=False)[source]#
Return a listener that either keeps, or if exclude is True excludes, lines between lines that match the marker regular expression. If no line matches, the resulting docstring would be empty, so no change will be made unless keepempty is true.
If what is a sequence of strings, only docstrings of a type in what will be processed.
- sage_docbuild.ext.sage_autodoc.bool_option(arg)[source]#
Used to convert flag options to auto directives. (Instead of directives.flag(), which returns None).
- sage_docbuild.ext.sage_autodoc.class_doc_from_option(arg)[source]#
Used to convert the :class-doc-from: option to autoclass directives.
- sage_docbuild.ext.sage_autodoc.cut_lines(pre, post=0, what=None)[source]#
Return a listener that removes the first pre and last post lines of every docstring. If what is a sequence of strings, only docstrings of a type in what will be processed.
Use like this (e.g. in the
setup()
function ofconf.py
):from sphinx.ext.autodoc import cut_lines app.connect('autodoc-process-docstring', cut_lines(4, what=['module']))
This can (and should) be used in place of
automodule_skip_lines
.
- sage_docbuild.ext.sage_autodoc.exclude_members_option(arg)[source]#
Used to convert the :exclude-members: option.
- sage_docbuild.ext.sage_autodoc.inherited_members_option(arg)[source]#
Used to convert the :inherited-members: option to auto directives.
- sage_docbuild.ext.sage_autodoc.member_order_option(arg)[source]#
Used to convert the :member-order: option to auto directives.
- sage_docbuild.ext.sage_autodoc.members_option(arg)[source]#
Used to convert the :members: option to auto directives.
- sage_docbuild.ext.sage_autodoc.merge_members_option(options)[source]#
Merge :private-members: and :special-members: options to the :members: option.
- sage_docbuild.ext.sage_autodoc.py_ext_sig_re = re.compile('^ ([\\w.]+::)? # explicit module name\n ([\\w.]+\\.)? # module and/or class name(s)\n (\\w+) \\s* # thing name\n (?: \\[\\s*(.*)\\s*])? , re.VERBOSE)#
extended signature RE: with explicit module name separated by “::”