Skip to content

Document replacement for sphinx.util.import_object or un-deprecate #13083

@mgeier

Description

@mgeier

Describe the bug

According to https://www.sphinx-doc.org/en/master/extdev/deprecated.html, sphinx.util.import_object is deprecated and importlib.import_module is suggested as alternative.

However, sphinx.util.import_object, does much more:

def import_object(object_name: str, /, source: str = '') -> Any:
"""Import python object by qualname."""
obj_path = object_name.split('.')
module_name = obj_path.pop(0)
try:
obj = import_module(module_name)
for name in obj_path:
module_name += '.' + name
try:
obj = getattr(obj, name)
except AttributeError:
obj = import_module(module_name)
except (AttributeError, ImportError) as exc:
if source:
msg = f'Could not import {object_name} (needed for {source})'
raise ExtensionError(msg, exc) from exc
msg = f'Could not import {object_name}'
raise ExtensionError(msg, exc) from exc
return obj

Am I supposed to copy all that code into my extension?

Or is there another (shorter) replacement?

Either way, this information should be added to the docs.

How to Reproduce

  • Use sphinx.util.import_object(). This will give a warning:

    sphinx.deprecation.RemovedInSphinx10Warning: 'sphinx.util.import_object' is deprecated. Check CHANGES for Sphinx API modifications.
    
  • Look for CHANGES -> not found

  • try CHANGES.rst -> the relevant content isn't there

  • try doc/changes/8.1.rst -> importlib.import_module is suggested

  • use importlib.import_module -> doesn't work, because it provides only a small part of original functionality

Environment Information

-

Sphinx extensions

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions