Skip to content

Unexpected results in importing mock from autodoc #14333

@RobertoRoos

Description

@RobertoRoos

Describe the bug

The code:

from sphinx.ext.autodoc import mock

Might import either the mock module or the mock() function, depending on what other package have already preceded.

This resembles #14142 , but this does feel like a separate issue.

We are encountering this here sphinx-doc/sphinx-argparse#82, in the sphinx_argparse extension.

We get the error:

Traceback
=========

      File "sphinx-argparse\sphinxarg\ext.py", line 749, in run
        with mock(self.config.autodoc_mock_imports):
             ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    TypeError: 'module' object is not callable

How to Reproduce

We have this in our ext.py:

from sphinx.ext.autodoc import mock

# ...

with mock(...):
    # ...

This succeeds in a Sphinx project with a conf.py like:

extensions = [
    'sphinx.ext.autodoc',
    'sphinxarg.ext',
]

But fails with this conf.py:

extensions = [
    'sphinx.ext.autodoc',
    'sphinx_autodoc_typehints',
    'sphinxarg.ext',
]

Environment Information

Show:
Package                       Version         Editable project location
----------------------------- --------------- ---------------------------------------------------------
accessible-pygments           0.0.5
alabaster                     1.0.0
babel                         2.17.0
beautifulsoup4                4.14.3
certifi                       2025.11.12
charset-normalizer            3.4.4
colorama                      0.4.6
coverage                      7.12.0
docutils                      0.21.2
idna                          3.11
imagesize                     1.4.1
iniconfig                     2.3.0
Jinja2                        3.1.6
librt                         0.7.3
lxml                          6.0.2
lxml-stubs                    0.5.1
MarkupSafe                    3.0.3
mypy                          1.19.0
mypy_extensions               1.1.0
packaging                     25.0
pathspec                      0.12.1
pip                           26.0.1
pluggy                        1.6.0
project                       0.1.0           C:\Users\name\Source\sphinx-argparse\playground\project
Pygments                      2.19.2
pytest                        9.0.2
pytest-cov                    7.0.0
requests                      2.32.5
roman-numerals                3.1.0
roman-numerals-py             3.1.0
ruff                          0.15.5
setuptools                    80.9.0
snowballstemmer               3.0.1
soupsieve                     2.8
Sphinx                        9.1.0
sphinx-argparse               0.5.2           C:\Users\name\Source\sphinx-argparse
sphinx-autodoc-typehints      3.9.7
sphinx-basic-ng               1.0.0b2
sphinx_rtd_theme              3.1.0
sphinxcontrib-applehelp       2.0.0
sphinxcontrib-devhelp         2.0.0
sphinxcontrib-htmlhelp        2.1.0
sphinxcontrib-jquery          4.1
sphinxcontrib-jsmath          1.0.1
sphinxcontrib-qthelp          2.0.0
sphinxcontrib-serializinghtml 2.0.0
types-docutils                0.22.3.20251115
typing_extensions             4.15.0
urllib3                       2.6.0

Sphinx extensions

'sphinx.ext.autodoc',
'sphinx_autodoc_typehints',
'sphinxarg.ext',

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions