Skip to content

gh-113360: Fix doctest failure when __test__ is set to falsy value #113389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ def _find(self, tests, obj, name, module, source_lines, globs, seen):

# Look for tests in a module's __test__ dictionary.
if inspect.ismodule(obj) and self._recurse:
for valname, val in getattr(obj, '__test__', {}).items():
for valname, val in (getattr(obj, '__test__', None) or {}).items():
if not isinstance(valname, str):
raise ValueError("DocTestFinder.find: __test__ keys "
"must be strings: %r" %
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,24 @@ def basics(): r"""
2 some_module.__test__.d
1 some_module.sample_func

When ``__test__`` is falsy, this value should be ignored:

>>> import types
>>> m = types.ModuleType('falsy_magic_test')
>>> m.__dict__.update({'__test__': None})

>>> finder = doctest.DocTestFinder()
>>> # Use module=test.test_doctest, to prevent doctest from
>>> # ignoring the objects since they weren't defined in m.
>>> import test.test_doctest
>>> finder.find(m, module=test.test_doctest)
[]

>>> m.__dict__.update({'__test__': False})
>>> finder.find(m, module=test.test_doctest)
[]


Duplicate Removal
~~~~~~~~~~~~~~~~~
If a single object is listed twice (under different names), then tests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix doctest failure when ``__test__`` magic var is set to a falsy value.