Friday, August 14, 2009

Python, a little too permisive?

Also known as, why static typing can rock.

So today I was tracking down a subtle bug. It is a long standing one that we have been having trouble with and things were finally quiet enough to really start digging in. Here is the type of problem it ended up being.

Say you have a bit of python that looks like this:

ids = {foo,bar,baz,moop}
for id in ids:
fxn(id)

with fuction doing something like:

def fxn(id)
"""
@param id: id of thing we are working with
@type id: str or unicode
"""
somedict[id] = do_stuff

Now, this will all work find untill you do something like:

names = {foo,bar,baz,moo}
for name in names:
fxn(id)

Now, what just happened here? At some point someone went and edited the loop and changed a variable name in one place, but not another. 'id' in python is a build in function (gives you the memory address of the parameter), so no error is raised for unknown variable.

However, once we get inside fxn, there is nothing making sure that 'id' is, in fact, a string. Instead it is happily using it as the index for the dictionary.

The result you get is, instead of:

foo: foostuff
bar: barstuff
baz: bazstuff
moop: moopstuff

you get

: moopstuff

and of course any further use of the dictionary will be missing 3 elements and the 4th one is misnamed so you can not access it anyway.

the two basic problems here:

(1) Permissive overloading by wildly different types: id the function gets replaced by id the string and no errors (or even warning) are raised.
(2) Permissive passing of arbitrary data to functions: nothing stops to warn the user that the function expecting a string just got a function instead.

Now, of course there are ways to protect against this,... for instance what I did was:

assert(isinstance(id,str) or isinstance(id,unicode))

Which essentially reimplementation strong typing.

So yeah.... python's permissiveness is a really powerful tool, but wow can it catch you as a project ages and is maintained. That tends to be the problem with loose and agile technologies/methodologies.. they are great for throwaway stuff, but they become increasingly difficult to maintain and keep 'correct' as they age. This is a class of bug that even C/C++/ObjC would choke on and not let buy. And Ada probably would break your fingers to boot.

3 comments:

nef said...

This amoung other superfolus reasons, are why I've always been a fan of static ty[e sustems.

I can understand the benefit of dynamic type systems, but reall feel like the cons. out weigh the pros. :/

Nathan Weyer said...

To be fair, you can do some really powerful things with such loose typing. For instance, systems like the template/concept/STL in C++ are unnecessary, the capability flows directly from the language (rather then hacked on additional syntax).

Dynamic typing make generic programming almost trivial.

Anonymous said...

Hi!
You may probably be very interested to know how one can make real money on investments.
There is no initial capital needed.
You may commense to get income with a sum that usually is spent
for daily food, that's 20-100 dollars.
I have been participating in one company's work for several years,
and I'll be glad to let you know my secrets at my blog.

Please visit blog and send me private message to get the info.

P.S. I make 1000-2000 per daily now.

http://theinvestblog.com [url=http://theinvestblog.com]Online Investment Blog[/url]