Mailing List Archive

[Q] Static type checker for Python
hello.
Probabilly this is dummy question 'cause I'm a new user of Python.

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

if impossible, why?
what language feature should be changed to be possible?

thanks in advance.
[Q] Static type checker for Python [ In reply to ]
> 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@mojam.com | http://www.musi-cal.com/~skip/
518-372-5583
[Q] Static type checker for Python [ In reply to ]
From: Skip Montanaro <skip@mojam.com>

> 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.h
tml
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@mojam.com | http://www.musi-cal.com/~skip/
518-372-5583
[Q] Static type checker for Python [ In reply to ]
From: neelk@brick.cswv.com (Neel Krishnaswami)

In article <377ADAD7.EA105C50@mojam.com>,
Skip Montanaro <skip@mojam.com> wrote:
>> 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.

I'm about to go off on a tangent, so first I want to warn the original
poster that there aren't too many actual problems with dynamic typing
-- people have written huge and complex apps with no problems using
Smalltalk and Lisp, for example.

Now, it is possible to teach Python to check the argument types of
functions at runtime, and I've written a little module called
TypeChecker to do just that. It's a wrapper class with callable
instances, and the instance checks the declaration you make in a
docstring against the actual types and classes of the arguments.
For example:

def fact(n):
"""foo(n :: LongType) <-- here's the type declaration; it can be
either a type from the types module or a
class name.
A factorial that only takes longs as arguments."""
f = 1L
for i in range(1L, n+1):
f = f * i
return f

f = TypeChecker(fact)

>>> fact(3)
6L

>>> f(3L)
6L

>>>f(3)
Traceback (innermost last):
File "<stdin>", line 1, in ?
File "/usr/tmp/python-6-237", line 25, in __call__
File "/usr/tmp/python-6-237", line 118, in check_args
TypeError: Argument 1 of fact wrong type, <type 'int'>, declared <type 'long
int'>

What it's doing is parsing the type declaration in the doc string to
generate a list of types and classes, and then the __call__ method of
the instance is comparing the declared types against the runtime
arguments it receives before running the function. I've always wanted
to abuse docstrings, and now I've gotten my chance. :) Though this is
a fairly mild abuse, considering that it doesn't block you from
putting other things into the docstring.

Obviously, this is not terribly useful. (It doesn't handle subclasses
correctly, f'ex, and can't yet be used on class methods.) But it is
interesting to see what Python static types might look like. (The
answer to my eyes is "fairly ugly;" real lexical scoping and
type/class unification seem to me to be prerequisites before static
typing will behave gracefully in Python.)

The actual code for TypeChecker is a little too long to put in the
post, so here's a URL:

http://www.sff.net/people/neelk/free-software/TypeChecker.py

Neel
[Q] Static type checker for Python [ In reply to ]
In article <377ADAD7.EA105C50@mojam.com>,
Skip Montanaro <skip@mojam.com> wrote:
>> 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.

I'm about to go off on a tangent, so first I want to warn the original
poster that there aren't too many actual problems with dynamic typing
-- people have written huge and complex apps with no problems using
Smalltalk and Lisp, for example.

Now, it is possible to teach Python to check the argument types of
functions at runtime, and I've written a little module called
TypeChecker to do just that. It's a wrapper class with callable
instances, and the instance checks the declaration you make in a
docstring against the actual types and classes of the arguments.
For example:

def fact(n):
"""foo(n :: LongType) <-- here's the type declaration; it can be
either a type from the types module or a
class name.
A factorial that only takes longs as arguments."""
f = 1L
for i in range(1L, n+1):
f = f * i
return f

f = TypeChecker(fact)

>>> fact(3)
6L

>>> f(3L)
6L

>>>f(3)
Traceback (innermost last):
File "<stdin>", line 1, in ?
File "/usr/tmp/python-6-237", line 25, in __call__
File "/usr/tmp/python-6-237", line 118, in check_args
TypeError: Argument 1 of fact wrong type, <type 'int'>, declared <type 'long int'>

What it's doing is parsing the type declaration in the doc string to
generate a list of types and classes, and then the __call__ method of
the instance is comparing the declared types against the runtime
arguments it receives before running the function. I've always wanted
to abuse docstrings, and now I've gotten my chance. :) Though this is
a fairly mild abuse, considering that it doesn't block you from
putting other things into the docstring.

Obviously, this is not terribly useful. (It doesn't handle subclasses
correctly, f'ex, and can't yet be used on class methods.) But it is
interesting to see what Python static types might look like. (The
answer to my eyes is "fairly ugly;" real lexical scoping and
type/class unification seem to me to be prerequisites before static
typing will behave gracefully in Python.)

The actual code for TypeChecker is a little too long to put in the
post, so here's a URL:

http://www.sff.net/people/neelk/free-software/TypeChecker.py

Neel