This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: python -m doctest script_file_with_no_py_extension produces confusing NameErrors
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: r.david.murray, towb
Priority: normal Keywords:

Created on 2016-07-04 09:54 by towb, last changed 2022-04-11 14:58 by admin.

Messages (3)
msg269781 - (view) Author: (towb) Date: 2016-07-04 09:54
Command line tools usually use the shebang and don't have a .py extension. This breaks the `python -m doctest` shortcut for testmod().

Getting it to work is probably ugly, but there should be a useful message. Currently it's just a NameError for every function, which is very confusing.
msg269790 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-07-04 16:32
This is not a bug, it is a feature.  python -m doctest can be used to run tests from files that are *not* python files.

I wouldn't call tracebacks with name errors as a silent failure :)

As you say, there is no easy way to use doctest to run doctests in a script file that is not a .py file, since it can't be imported, so I'm not sure there is anything that can be done here.  Even trying to guess that it is a python file and issuing an error message or even a warning is problematic from a backward compatibility standpoint, since there may be people with files that look like python files that are *meant* to be run as doctests.

I'm guessing script-with-testable-docstrings isn't *common*, though I can easily imagine myself writing one (using subprocess to re-exec the script).  What I actually did, though, was have a command line switch that runs the doctests by calling testmod from within the script, which is how doctest is designed to work.  

So, we can see what other people think about the idea of looking for a shebang line and issuing an error message.  Or maybe a warning...or an error plus a command line switch to force it to process the script anyway?
msg269799 - (view) Author: (towb) Date: 2016-07-04 17:12
Improving the documentation would also be nice, and easier. -m is mentioned in two places, first only as a shortcut for testmod, later with an explanation on name extensions and testfile. At least I didn't get that far.
History
Date User Action Args
2022-04-11 14:58:33adminsetgithub: 71634
2016-07-04 17:12:00towbsetmessages: + msg269799
2016-07-04 16:32:24r.david.murraysetnosy: + r.david.murray
title: python -m doctest file_with_no_py_extension # silently fails -> python -m doctest script_file_with_no_py_extension produces confusing NameErrors
messages: + msg269790

versions: + Python 3.6, - Python 3.5
2016-07-04 09:54:32towbcreate