doctest suggestion

Tim Hochberg tim.hochberg at ieee.org
Sat Sep 29 01:19:43 EDT 2001


Right behind avoiding land wars in Asia is some advice about not arguing
with Tim Peters, but why be prudent.

> > 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).

Yes, but sometimes one deliberately exposes imported names, and then things
break even thouigh one's not being sloppy. Back when I was using "import Y
as _Y" I had particular problems with "__init__.py" files where I sometimes
pulled in several functions or classes from different modules.

> 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.

I've decided not to cater to import * users. I've banned import-* from code
and what made it painless enough for me do was "import Numeric as np". My
advice to users of "from X import *" is generally "So don't do that." Hmmm.
Sounds familiar somehow.

> 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.

I automate running doctest by walking all the packages and running doctest
everywhere. isprivate isn't powerful enough to help me here since it only
works on names. The second solution sounds like it might work -- I'll look
into it.

[Snip]
> Right; and the doctest docs point this out.

Yeah, I knew that...

[Snap]

> 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.

Fair enough. I'll either look into the Jython side, or put together a
solution based on pruning the dictionary -- either way you won't have to
listen to me whining again for a while....

-tim






More information about the Python-list mailing list