Skip to content

Commit 16e6308

Browse files
committed
Split out _parse_name()
1 parent e000134 commit 16e6308

File tree

1 file changed

+88
-67
lines changed

1 file changed

+88
-67
lines changed

sphinx/ext/autodoc/importer.py

Lines changed: 88 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -770,77 +770,15 @@ def _load_object_by_name(
770770
get_attr: _AttrGetter,
771771
) -> tuple[_ItemProperties, str | None, str | None, ModuleType | None, Any] | None:
772772
"""Import and load the object given by *name*."""
773-
from sphinx.ext.autodoc._documenters import py_ext_sig_re
774-
775-
# Parse the definition in *name*.
776-
# autodoc directives for classes and functions can contain a signature,
777-
# which overrides the autogenerated one.
778-
matched = py_ext_sig_re.match(name)
779-
if matched is None:
780-
logger.warning(
781-
__('invalid signature for auto%s (%r)'),
782-
objtype,
783-
name,
784-
type='autodoc',
785-
)
786-
# need a module to import
787-
logger.warning(
788-
__(
789-
"don't know which module to import for autodocumenting "
790-
'%r (try placing a "module" or "currentmodule" directive '
791-
'in the document, or giving an explicit module name)'
792-
),
793-
name,
794-
type='autodoc',
795-
)
796-
return None
797-
798-
explicit_modname, path, base, _tp_list, args, retann = matched.groups()
799-
800-
# Support explicit module and class name separation via ``::``
801-
if explicit_modname is not None:
802-
module_name = explicit_modname.removesuffix('::')
803-
parents = path.rstrip('.').split('.') if path else ()
804-
else:
805-
module_name = None
806-
parents = ()
807-
808-
resolved = _resolve_name(
773+
parsed = _parse_name(
774+
name=name,
809775
objtype=objtype,
810-
module_name=module_name,
811-
path=path,
812-
base=base,
813-
parents=parents,
814776
current_document=current_document,
815-
ref_context_py_module=env.ref_context.get('py:module'),
816-
ref_context_py_class=env.ref_context.get('py:class', ''),
777+
env=env,
817778
)
818-
if resolved is None:
819-
msg = 'must be implemented in subclasses'
820-
raise NotImplementedError(msg)
821-
module_name, parts = resolved
822-
823-
if objtype == 'module' and args:
824-
msg = __("signature arguments given for automodule: '%s'")
825-
logger.warning(msg, name, type='autodoc')
826-
return None
827-
if objtype == 'module' and retann:
828-
msg = __("return annotation given for automodule: '%s'")
829-
logger.warning(msg, name, type='autodoc')
830-
return None
831-
832-
if not module_name:
833-
# Could not resolve a module to import
834-
logger.warning(
835-
__(
836-
"don't know which module to import for autodocumenting "
837-
'%r (try placing a "module" or "currentmodule" directive '
838-
'in the document, or giving an explicit module name)'
839-
),
840-
name,
841-
type='autodoc',
842-
)
779+
if parsed is None:
843780
return None
781+
module_name, parts, args, retann = parsed
844782

845783
# Import the module and get the object to document
846784
try:
@@ -1052,6 +990,89 @@ def _load_object_by_name(
1052990
return props, args, retann, module, parent
1053991

1054992

993+
def _parse_name(
994+
*,
995+
name: str,
996+
objtype: _AutodocObjType,
997+
current_document: _CurrentDocument,
998+
env: BuildEnvironment,
999+
) -> tuple[str, tuple[str, ...], str | None, str | None] | None:
1000+
"""Parse *name* into module name, path, arguments, and return annotation."""
1001+
from sphinx.ext.autodoc._documenters import py_ext_sig_re
1002+
1003+
# Parse the definition in *name*.
1004+
# autodoc directives for classes and functions can contain a signature,
1005+
# which overrides the autogenerated one.
1006+
matched = py_ext_sig_re.match(name)
1007+
if matched is None:
1008+
logger.warning(
1009+
__('invalid signature for auto%s (%r)'),
1010+
objtype,
1011+
name,
1012+
type='autodoc',
1013+
)
1014+
# need a module to import
1015+
logger.warning(
1016+
__(
1017+
"don't know which module to import for autodocumenting "
1018+
'%r (try placing a "module" or "currentmodule" directive '
1019+
'in the document, or giving an explicit module name)'
1020+
),
1021+
name,
1022+
type='autodoc',
1023+
)
1024+
return None
1025+
1026+
explicit_modname, path, base, _tp_list, args, retann = matched.groups()
1027+
1028+
# Support explicit module and class name separation via ``::``
1029+
if explicit_modname is not None:
1030+
module_name = explicit_modname.removesuffix('::')
1031+
parents = path.rstrip('.').split('.') if path else ()
1032+
else:
1033+
module_name = None
1034+
parents = ()
1035+
1036+
resolved = _resolve_name(
1037+
objtype=objtype,
1038+
module_name=module_name,
1039+
path=path,
1040+
base=base,
1041+
parents=parents,
1042+
current_document=current_document,
1043+
ref_context_py_module=env.ref_context.get('py:module'),
1044+
ref_context_py_class=env.ref_context.get('py:class', ''),
1045+
)
1046+
if resolved is None:
1047+
msg = 'must be implemented in subclasses'
1048+
raise NotImplementedError(msg)
1049+
module_name, parts = resolved
1050+
1051+
if objtype == 'module' and args:
1052+
msg = __("signature arguments given for automodule: '%s'")
1053+
logger.warning(msg, name, type='autodoc')
1054+
return None
1055+
if objtype == 'module' and retann:
1056+
msg = __("return annotation given for automodule: '%s'")
1057+
logger.warning(msg, name, type='autodoc')
1058+
return None
1059+
1060+
if not module_name:
1061+
# Could not resolve a module to import
1062+
logger.warning(
1063+
__(
1064+
"don't know which module to import for autodocumenting "
1065+
'%r (try placing a "module" or "currentmodule" directive '
1066+
'in the document, or giving an explicit module name)'
1067+
),
1068+
name,
1069+
type='autodoc',
1070+
)
1071+
return None
1072+
1073+
return module_name, parts, args, retann
1074+
1075+
10551076
def _resolve_name(
10561077
*,
10571078
objtype: str,

0 commit comments

Comments
 (0)