How to detect typos in Python programs

Peter Hansen peter at engcorp.com
Fri Jul 25 11:15:03 EDT 2003


Manish Jethani wrote:
> 
> Is there a way to detect typos in a Python program, before
> actually having to run it.  Let's say I have a function like this:
> 
>   def server_closed_connection():
>     session.abost()
> 
> Here, abort() is actually misspelt.  The only time my program
> follows this path is when the server disconnects from its
> end--and that's like once in 100 sessions.  So sometimes I
> release the program, people start using it, and then someone
> reports this typo after 4-5 days of the release (though it's
> trivial to fix manually at the user's end, or I can give a patch).
> 
> How can we detect these kinds of errors at development time?
> It's not practical for me to have a test script that can make
> the program go through all (most) the possible code paths.

You have no good alternative.  Why do you say it's impractical
to actually test your software before it's shipped?  Isn't it
more impractical to rely on your users to test the software,
thinking it should work?

Unit testing in Python is *really* easy.  I can't think of any
reason not to do it as the best way of catching problems like you
show above.  If you resist :-), however, you might find PyChecker
will help.  I'm not sure if it can do anything in the above case
yet, however.

-Peter




More information about the Python-list mailing list