[Types-sig] Proposed type declaration syntax

Paul Prescod paulp@ActiveState.com
Wed, 14 Mar 2001 10:08:25 -0800

While we are experimenting, I want to use a type declaration syntax that
does not interfere with standard Python syntax. I'd also like it to be
discoverable both at runtime (for ERR) and compile time (for DOC).

def func(module, integer, func, stream, tuple, string, unknown):
    __types__ = {"module: A module to foobar": 	types.ModuleType,
                  "integer: An integer to baz":	Int,
                  "func: A callable object":  	Callable,
                  "stream: A readable stream":  ReadStream,
                  "tuple: A sequence": 	 	Sequence,
                  "string: A string":		String,
		  "unknown: Some weird type":   None,
                  "__returns__: File handle no":Int}
    ... some long calculation...
    return __assertrc(42)

Note that you can *declare types* for purposes of DOC without *asserting
types* for purposes of ERR. Those who like DOC but hate ERR have a

I'll post the code that implements this later today and then start
working on a PEP for the Zwiki and sourceforge.

I could use function attributes for this but I chose not to for the
following reasons (which I will document in the PEP):

 * function attributes are implied to be sort of more dynamic in that
they can be assigned from anywhere

 * function attributes must go AFTER the code which is a big problem for
long functions. If anything, I would be more inclined to move the type
declaration BEFORE the function definition, not after the suite

I also considered a more verbose but explicit format like
assertTypes(__types__, locals()) but the __assertrc got too ugly.

    Programming the way
    indented it.
       - (originated with Skip Montanaro?)