Skip to content

Commit

Permalink
Move split_full_qualified_name() to ext.autosummary.generate
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Jul 28, 2023
1 parent 58585a7 commit d779a1c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
35 changes: 33 additions & 2 deletions sphinx/ext/autosummary/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from __future__ import annotations

import argparse
import importlib
import inspect
import locale
import os
Expand Down Expand Up @@ -44,7 +45,7 @@
from sphinx.locale import __
from sphinx.pycode import ModuleAnalyzer, PycodeError
from sphinx.registry import SphinxComponentRegistry
from sphinx.util import logging, rst, split_full_qualified_name
from sphinx.util import logging, rst
from sphinx.util.inspect import getall, safe_getattr
from sphinx.util.osutil import ensuredir
from sphinx.util.template import SphinxTemplateLoader
Expand Down Expand Up @@ -147,6 +148,36 @@ def render(self, template_name: str, context: dict) -> str:
return template.render(context)


def _split_full_qualified_name(name: str) -> tuple[str | None, str]:
"""Split full qualified name to a pair of modname and qualname.
A qualname is an abbreviation for "Qualified name" introduced at PEP-3155
(https://peps.python.org/pep-3155/). It is a dotted path name
from the module top-level.
A "full" qualified name means a string containing both module name and
qualified name.
.. note:: This function actually imports the module to check its existence.
Therefore you need to mock 3rd party modules if needed before
calling this function.
"""
parts = name.split('.')
for i, _part in enumerate(parts, 1):
try:
modname = ".".join(parts[:i])
importlib.import_module(modname)
except ImportError:
if parts[:i - 1]:
return ".".join(parts[:i - 1]), ".".join(parts[i - 1:])
else:
return None, ".".join(parts)
except IndexError:
pass

return name, ""


# -- Generating output ---------------------------------------------------------


Expand Down Expand Up @@ -292,7 +323,7 @@ def generate_autosummary_content(name: str, obj: Any, parent: Any,
_get_members(doc, app, obj, {'attribute', 'property'})

if modname is None or qualname is None:
modname, qualname = split_full_qualified_name(name)
modname, qualname = _split_full_qualified_name(name)

if doc.objtype in ('method', 'attribute', 'property'):
ns['class'] = qualname.rsplit(".", 1)[0]
Expand Down
30 changes: 0 additions & 30 deletions sphinx/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,36 +265,6 @@ def import_object(objname: str, source: str | None = None) -> Any:
raise ExtensionError('Could not import %s' % objname, exc) from exc


def split_full_qualified_name(name: str) -> tuple[str | None, str]:
"""Split full qualified name to a pair of modname and qualname.
A qualname is an abbreviation for "Qualified name" introduced at PEP-3155
(https://peps.python.org/pep-3155/). It is a dotted path name
from the module top-level.
A "full" qualified name means a string containing both module name and
qualified name.
.. note:: This function actually imports the module to check its existence.
Therefore you need to mock 3rd party modules if needed before
calling this function.
"""
parts = name.split('.')
for i, _part in enumerate(parts, 1):
try:
modname = ".".join(parts[:i])
import_module(modname)
except ImportError:
if parts[:i - 1]:
return ".".join(parts[:i - 1]), ".".join(parts[i - 1:])
else:
return None, ".".join(parts)
except IndexError:
pass

return name, ""


def encode_uri(uri: str) -> str:
split = list(urlsplit(uri))
split[1] = split[1].encode('idna').decode('ascii')
Expand Down

0 comments on commit d779a1c

Please sign in to comment.