[Q] Static type checker for Python

Skip Montanaro skip at mojam.com
Wed Jun 30 23:02:22 EDT 1999


> Can I make static type checker for Python?
> or Is there any previous work about this?

In theory, you could do some type inference and generate some warning
messages, but since you don't declare variables to be of a particular
type in Python, it's unlikely it would be very useful.  The same
variable can point to many different types at runtime.

For example, the following code may be just what the author intended:

    tmp = 1.7 * math.sin(a)
    tmp = `tmp`
    print tmp
    tmp = len(tmp)

After the first statement you might *presume* that tmp holds a floating
point number, but there's no guarantee, since you don't know that
math.sin returns a float.  You can infer that tmp holds a string after
the second statement because its builtin to the language that ``
converts what it encloses to a string (though the timbot will probably
chime in with a case where I'm wrong about that <wink>).  After the last
statement you no longer know what tmp holds because you can't be sure
that len() returns an int.

The topic of static type declarations has consumed more than its fair
share of heat and light on the newsgroup and at Python conferences.  For
one recent example, check out:

   
http://www.foretec.com/python/workshops/1998-11/proceedings/papers/masse/masse.html
    http://www.foretec.com/python/workshops/1998-11/dd-rmasse-sum.html
    http://www.python.org/~rmasse/presentations/python-types/

You can scan the comp.lang.python archives for "static types" using the
form at

    http://www.python.org/search/

to browse around the previous discussions about this topic on the
newsgroup.

Also, it's worth taking a look at Guido's most recent "Python Futures"
talk to get an idea where he things Python might be headed:

   
http://www.foretec.com/python/workshops/1998-11/proceedings/guido/index.html

Note, however, that the above talk was given last November.  Things have
probably changed a bit since then.

<hungarian-notation-anyone-l'yrs?>

-- 
Skip Montanaro	| http://www.mojam.com/
skip at mojam.com  | http://www.musi-cal.com/~skip/
518-372-5583




More information about the Python-list mailing list