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 8.0.2. 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

  • François Bissey (2024-08-24): rebased on Sphinx 8.0.2

  • François Bissey (2024-09-10): Tweaks to support python 3.9 (and older sphinx) as well

  • François Bissey (2024-11-12): rebased on Sphinx 8.1.3 (while trying to keep python 3.9 compatibility)

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.

add_content(more_content)[source]
add_directive_header(sig)[source]
classmethod can_document_member(member, membername, isattr, parent)[source]
document_members(all_members=False)[source]
get_attribute_comment(parent, attrname)[source]
get_doc()[source]
get_real_modname()[source]
import_object(raiseerror=False)[source]
static is_function_or_method(obj)[source]
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: ClassVar[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

should_suppress_value_header()[source]
update_annotations(parent)[source]

Update __annotations__ to support type_comment and so on.

class sage_docbuild.ext.sage_autodoc.ClassDocumenter(*args: Any)[source]

Bases: DocstringSignatureMixin, ModuleLevelDocumenter

Specialized Documenter subclass for classes.

add_content(more_content)[source]
add_directive_header(sig)[source]
classmethod can_document_member(member, membername, isattr, parent)[source]
document_members(all_members=False)[source]
format_args(**kwargs)[source]
format_signature(**kwargs)[source]
generate(more_content=None, real_modname=None, check_module=False, all_members=False)[source]
get_canonical_fullname()[source]
get_doc()[source]
get_object_members(want_all)[source]
get_overloaded_signatures()[source]
get_variable_comment()[source]
import_object(raiseerror=False)[source]
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: ClassVar[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).

resolve_name(modname, parents, path, base)[source]
class sage_docbuild.ext.sage_autodoc.DataDocumenter(directive: DocumenterBridge, name: str, indent: str = '')[source]

Bases: GenericAliasMixin, UninitializedGlobalVariableMixin, ModuleLevelDocumenter

Specialized Documenter subclass for data items.

add_content(more_content)[source]
add_directive_header(sig)[source]
classmethod can_document_member(member, membername, isattr, parent)[source]
document_members(all_members=False)[source]
get_doc()[source]
get_module_comment(attrname)[source]
get_real_modname()[source]
import_object(raiseerror=False)[source]
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: ClassVar[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

should_suppress_value_header()[source]
update_annotations(parent)[source]

Update __annotations__ to support type_comment and so on.

class sage_docbuild.ext.sage_autodoc.DataDocumenterMixinBase[source]

Bases: object

config: Config
env: BuildEnvironment
modname: str
object: Any
objpath: list[str]
parent: Any
should_suppress_directive_header()[source]

Check directive header should be suppressed.

should_suppress_value_header()[source]

Check :value: header should be suppressed.

update_content(more_content)[source]

Update docstring, for example with TypeVar variance.

class sage_docbuild.ext.sage_autodoc.DecoratorDocumenter(directive: DocumenterBridge, name: str, indent: str = '')[source]

Bases: FunctionDocumenter

Specialized Documenter subclass for decorator functions.

format_args(**kwargs)[source]
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.

format_signature(**kwargs)[source]
get_doc()[source]
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.

format_signature(**kwargs)[source]
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.

add_content(more_content)[source]

Add content from docstrings, attribute documentation and user.

add_directive_header(sig)[source]

Add the directive header and options to the generated content.

add_line(line, source, *lineno)[source]

Append one line of generated reST to the output.

classmethod can_document_member(member, membername, isattr, parent)[source]

Called to see if a member can be documented by this Documenter.

check_module()[source]

Check if self.object is really defined in the module given by self.modname.

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_attr(obj, name, *defargs)[source]

getattr() override for types such as Zope interfaces.

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.

get_sourcename()[source]
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: ClassVar[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

process_doc(docstrings)[source]

Let the user process the docstrings before adding them.

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 the zipfile.ZipFile.open method.

sort_members(documenters, order)[source]

Sort the given member list.

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.

classmethod can_document_member(member, membername, isattr, parent)[source]
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.

add_directive_header(sig)[source]
annotate_to_first_argument(func, typ)[source]

Annotate type hint to the first argument of function if needed.

classmethod can_document_member(member, membername, isattr, parent)[source]
document_members(all_members=False)[source]
format_args(**kwargs)[source]
format_signature(**kwargs)[source]
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.

should_suppress_directive_header()[source]
update_content(more_content)[source]
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).

add_directive_header(sig)[source]
annotate_to_first_argument(func, typ)[source]

Annotate type hint to the first argument of function if needed.

classmethod can_document_member(member, membername, isattr, parent)[source]
directivetype = 'method'
document_members(all_members=False)[source]
format_args(**kwargs)[source]
get_doc()[source]
import_object(raiseerror=False)[source]
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.

add_content(more_content)[source]
add_directive_header(sig)[source]
classmethod can_document_member(member, membername, isattr, parent)[source]
content_indent = ''

indentation by which to indent the directive content

get_module_members()[source]

Get members of target module.

get_object_members(want_all)[source]
import_object(raiseerror=False)[source]
objtype = 'module'

name by which the directive is called (auto…) and the default generated directive name

option_spec: ClassVar[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>}
parse_name()[source]
resolve_name(modname, parents, path, base)[source]
sort_members(documenters, order)[source]
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).

resolve_name(modname, parents, path, base)[source]
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.

get_doc()[source]
import_object(raiseerror=False)[source]
should_suppress_value_header()[source]
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.

class sage_docbuild.ext.sage_autodoc.Options[source]

Bases: dict[str, Any]

A dict/attribute hybrid that returns None on nonexisting keys.

copy()[source]
class sage_docbuild.ext.sage_autodoc.PropertyDocumenter(directive: DocumenterBridge, name: str, indent: str = '')[source]

Bases: DocstringStripSignatureMixin, ClassLevelDocumenter

Specialized Documenter subclass for properties.

add_directive_header(sig)[source]
classmethod can_document_member(member, membername, isattr, parent)[source]
document_members(all_members=False)[source]
format_args(**kwargs)[source]
get_real_modname()[source]
import_object(raiseerror=False)[source]

Check the existence 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
get_doc()[source]
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__().

is_runtime_instance_attribute_not_commented(parent)[source]

Check the subject is an attribute defined in __init__() without comment.

should_suppress_value_header()[source]
class sage_docbuild.ext.sage_autodoc.SlotsMixin[source]

Bases: DataDocumenterMixinBase

Mixin for AttributeDocumenter to provide the feature for supporting __slots__.

get_doc()[source]
import_object(raiseerror=False)[source]
isslotsattribute()[source]

Check the subject is an attribute in __slots__.

should_suppress_value_header()[source]
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.

get_doc()[source]
import_object(raiseerror=False)[source]
should_suppress_value_header()[source]
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.

get_doc()[source]
import_object(raiseerror=False)[source]

Check the existence of uninitialized instance attribute when failed to import the attribute.

is_uninitialized_instance_attribute(parent)[source]

Check the subject is an annotation only attribute.

should_suppress_value_header()[source]
sage_docbuild.ext.sage_autodoc.annotation_option(arg)[source]
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 of conf.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.getdoc(obj, *args, **kwargs)[source]
sage_docbuild.ext.sage_autodoc.identity(x)[source]
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 “::”

sage_docbuild.ext.sage_autodoc.setup(app)[source]