doctest suggestion

Tim Peters tim.one at home.com
Fri Sep 28 23:33:25 EDT 2001


[Tim Hochberg]
> ...
> I like doctest a lot and use it for all of my unit testing.

You will have a long and happy life <wink>.

> However, I frequently run into problems when I import a class or
> function from a module using "from X import Y".

So don't do that.  I don't.  Because the import of a name also implicitly
*exports* the name to users of "import *", that's sloppy practice and
doctest deliberately didn't cater to it (read the doctest docs:  they're
quite explicit about this).  In recent Pythons "from X import Y as _Y" is a
one-liner way to rename Y to use a private name, and then doctest ignores
it, and you also don't screw import-* users.  Other possibilities include
passing your own isprivate function to doctest, or passing a module dict
pruned of names you don't want to see tested.  So there are multiple ways to
worm around it already, but they force users of sloppy imports to suffer
some consequences.

> Y's docstring gets run in the current environment and frequently the
> doctests fail because they rely on some aspects of Xs environment.

Right; and the doctest docs point this out.

> It seemed like it would be useful to skip tests in docstrings on
> objects imported from another module. I threw together a proof of
> concept of this which is attached below; this version ignores the
> docstrings on imported functions and classes.
>
> So here's the suggestion: I would really like to see this functionality
> included in doctest in some way. I'm willing to clean up the changes to
> my code and submit a patch if that's appropriate. However, first I
> wanted solicit opinions as to whether this approach is a good one or if
> there's a more elegant way to go about it:

I've always found it more profitable to be careful from the start about
which names my modules expose.  However, I'd accept a SourceForge patch
*provided that* it also leaves doctest working correctly under Jython.
doctest plays very mild introspection games, because Jython doesn't support
the same set of internal __xxx__ magic attributes CPython supports.  I don't
know whether your use of __xxx__ tricks works under Jython too; if it does,
cool, else it's a show-stopper.  I don't have the motivation (since I don't
do what you're doing) to dig into this myself.  But if someone else does,
incl. enough motivation to verify Jython behavior, and to change the docs
and the doctest tests, happy to play along.  The last person who tried to
address this, about two years ago, didn't find a way that also worked under
Jython.

respective-the-privacy-of-privates-but-that's-all-ly y'rs  - tim





More information about the Python-list mailing list