checking for mis-spelled variable names / function names

John Machin sjmachin at lexicon.net
Tue Nov 25 19:37:33 EST 2008


On Nov 26, 10:16 am, News123 <news... at free.fr> wrote:
> Hi,
>
> Let's imagine following code
>
> def specialfunc():
>         print "very special function"
>
> name= getuserinput()
> if name == 'one_name_out_of_a_million':
>         print "Hey your name '%s' is really rare" % namee
>         specialfunk()
>
> my python script could survive thousands of runs before falling into
> the mis-spelled code section. ('namee' instead of 'name' and
> 'specialfunck()' instead of 'specialfunc()'
> I know that good designers should always test their code and have
> complete code coverage, before releasing their beasts into the wild, but
> in my experience this is not always what happens.
>
> I fell already over quite of my own sins, but also over typoes of other
> python authors.
>
> Is there any way in python to check for mis-spelled variable / function
> names?
>
> In perl for example 'use strict;' would detect bad variable names,
> though it wouldn't detect calls to undeclared functions.
>
> I am aware, that there is absolutely valid (and useful) python code with
>  undefined functions / variable names.
>
> However for the scripts that I write I would prefer to identify as many
> typoes as possibe already when trying to run the script the first (and
> not the millionth) time.
>
> Do you have ideas suggestions?
> Are there any 'lint' like tools trying to analyze python code for
> potential stupidities?
>
> If yes, what would be 'the' way to add these tools / modules at )least
> during the development cycle) to the scripts.
>

C:\junk>type typo.py
def specialfunc():
        print "very special function"

### name= getuserinput() ### won't even compile; gets NameError
def get_check_name(name):
    if name == 'one_name_out_of_a_million':
        print "Hey your name '%s' is really rare" % namee
        specialfunk()

C:\junk>pychecker typo.py

C:\junk>c:\python25\python.exe c:\python25\Lib\site-packages\pychecker
\checker.py typo.py
Processing typo...

Warnings...

typo.py:7: No global (namee) found
typo.py:8: No global (specialfunk) found




More information about the Python-list mailing list