Adding static typing to Python

Nick Mathewson QnickQm at alum.mit.edu
Tue Feb 19 10:44:09 EST 2002


In article <24c39b2c.0202181531.187fad4c at posting.google.com>, 
        Alexander Jerusalem wrote:
> Has anyone heard of plans to add static type checking to Python?

Someone else has mentioned an old proposal of Guido's for adding
optional type declarations in order to help the compiler catch errors.

Making a real static type system is harder than you'd think.
Consider this function:

def fn(lst,item):
   for i in lst:
     if i == item:
        return repr(i)

What is the type of fn?  You could call it:

     (list[int], int) -> str

But then you'd lose the polymorphism of the function.  To a first
approximation, you might say:

     (list[A], A) -> str

But keep in mind that not any A will work...

     (list[A], A) -> str  s.t. A has __eq__(A) and __repr__() -> str

And that x==y is sometimes okay even if x.__class__ != y.__class__...

     (list[A], B) -> str  s.t. A has __eq__(B) and __repr__() -> str

But there's nothing that says you can't use a tuple or a string or a UserList 
instead of a list...


     (sequence[A], B) -> str  s.t. A has __eq__(B) and __repr__() -> str

And you aren't using _all_ of the sequence protocol: just iteration...

     (A, B) -> str   s.t. A has __iter()__ -> C 
                     s.t. C has next() -> D (or raises StopIteration)
                     s.t. D has __eq__(B) and repr() -> str

IMNSHO, some kind type inference is the only tractable answer.

And-what-if-repr-doesn't-return-a-string?-ly yrs,

-- 
 Nick Mathewson    <Q nick Q m at alum dot mit dot edu>
                      Remove Q's to respond.  No spam.



More information about the Python-list mailing list