[Types-sig] Framework for an experimental type system
Paul Prescod
paulp@ActiveState.com
Tue, 13 Mar 2001 04:42:16 -0800
The fastest way to get to a working type system is to get to an
experimental type system and play with it. The fastest way to get to an
experimental type system is to stay as close as possible to Python's
existing syntax. So I'm going to use Tim's idea as elaborated by Tim.
Thanks Tim and Tim!
And for those who are not named Tim and would like credit, I'll point
out that this is not the first time we've seen this idea. Now that we've
shifted goals it seems more workable than it did when we were very
concerned about optimization.
Here is a proposed experimental strategy:
from __experimental__.typecheck import *
def open(filename, mode="r", buffering=-1):
assertTypes(filename=String,
mode=String,
buffering=Integer,
_RC=Integer})
...
This model allows us to build the type-system right this minute. At
first, it only helps err, not doc or anything else but that's okay. It's
a basis for experimentation.
_RC is a magic constant which declares the return type of this method.
If we *require* the assertTypes function to be the first thing in the
method after the (optional) docstring then we can find it predictably
and use it for doc, just as if the declarations were right in the open
function.
The types come from "typecheck" and they all implement an
__implementedby__ method.
Also, it should be possible to assert conformance to a type with an
__interfaces__ attribute. In other words, Tim's Type objects and
Michel's Interface objects are the same thing. Michel happens to have a
syntactic trick that is very nice for declaring OO interfaces but it was
always the understanding that it comes down to a runtime check.
If a type declaration points to a class (not an object), then we should
use instanceof under the covers, not class.__implementedby__.
class Person:
...
def hire(employee):
assertTypes(employee=Person)
One downside about Tim's model is that a Type object that inherits from
multiple other type objects will have to manually delegate to each of
their __implementedby__ methods (barring __getattr__ tricks). I think
that maybe the typeAssert function should walk up the inheritance tree
testing ALL __implementedby__ methods on base classes.
--
Python:
Programming the way
Guido
indented it.
- (originated with Skip Montanaro?)