static type checking / interface definitions for Python?

Bill Janssen (janssen@parc.xerox.com)
Thu, 22 Sep 1994 13:25:00 PDT

I'm wondering if anyone has done a design for optional static typing in Python?

I'm thinking of this in several levels. (I'm not wedded to or even very
enthusiastic about the syntax I suggest in this note.)

Level one would introduce optional addition interface description files
(IDFs) for modules. For a module named foo, there might be an IDF
called foo.pi. It would contain a description of the symbols of that
module. For example, the module dbm might have the file dbm.pi:

open :function((filename :string, rwmode :string, protection_mode
:cardinal), :mapping(:string, :string))
error :exception

symbols could be omitted from the IDF at will, and descriptions of
individual symbols might be partial, so the IDF for dbm might just be:

open :function((:string, , protection_mode :cardinal), :mapping)

This would serve solely as documentation to begin with. I have no idea
what the correct type syntax might be, or how to deal with complex
constructed types, etc.

The second level would add type inference to the Python byte-compiler,
in which by looking at the IDFs the compiler would attempt to find type
mis-matches, where it could statically determine actual type
restrictions on values. This could be combined with optional run-time
type checking.

The third level would change the syntax of .py files by allowing
optional declarations of local and global attributes, so that errors
like the following could be caught:

def foo (a :string, b):
import math
declare v1 :seq
...
v1 = 3 # oops -- assign number to a seq
...
foo2 (math.log10(a))
...

Surely this kind of mechanism would allow us to build larger, more
correct programs, in Python.

Bill