sphinx-doc / sphinx-doc/sphinx

doctests has no access to the module globals (c.f. running with python -m doctest)

Open
#6,590 11 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

extensions:doctest type:bug type:docs
Dominant language
Python
Stars
8k
Forks
2.6k
PR merge metrics
No merged PRs in 30d

Description

Lets take this simple example:

$ cat docs/conf.py 
import a

extensions = ['sphinx.ext.doctest', 'sphinx.ext.autodoc']
$ cat docs/contents.rst 
.. automodule:: a
    :members:
$ cat a/__init__.py 
def foo():
    """bar

    >>> foo()
    1
    """
    return 1

Testing with the plain doctest module works:

$ python3 -m doctest -v a/__init__.py 
Trying:
    foo()
Expecting:
    1
ok
1 items had no tests:
    __init__
1 items passed all tests:
   1 tests in __init__.foo
1 tests in 2 items.
1 passed and 0 failed.
Test passed.

py.test --doctest-modules works in the same way, it follows same execution context convention.

Yet, apparently doctest code has no access to the module globals (e.g. foo):

$ python3 -m sphinx -b doctest docs/ builds/
Running Sphinx v1.8.4
loading pickled environment... done
building [mo]: targets for 0 po files that are out of date
building [doctest]: targets for 1 source files that are out of date
updating environment: 0 added, 0 changed, 0 removed
looking for now-outdated files... none found
running tests...

Document: contents
------------------
**********************************************************************
File "../a/__init__.py", line ?, in default
Failed example:
    foo()
Exception raised:
    Traceback (most recent call last):
      File "/usr/lib/python3.7/doctest.py", line 1329, in __run
        compileflags, 1), test.globs)
      File "<doctest default[0]>", line 1, in <module>
        foo()
    NameError: name 'foo' is not defined
**********************************************************************
1 items had failures:
   1 of   1 in default
1 tests in 1 items.
0 passed and 1 failed.
***Test Failed*** 1 failures.

Doctest summary
===============
    1 test
    1 failure in tests
    0 failures in setup code
    0 failures in cleanup code
build finished with problems.

Sounds like a bug for me.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with docs/conf.py, docs/contents.rst, and a/init.py using python -m doctest and the Sphinx doctest build command. Start at the sphinx.ext.doctest entry point and compare its execution context with Python's doctest module; done means foo() resolves and the doctest build passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.