[Types-sig] type-safe declaration

Guido van Rossum guido@CNRI.Reston.VA.US
Tue, 14 Dec 1999 11:42:08 -0500


[still Paul Prescod]
> Under my plan, you would need a static declaration on YOUR code. I mean
> if your code can NEVER be right (e.g. range( "abc" ) ) then maybe a
> smart checker could report that. Java actually requires this of
> implementors. But if your code COULD be right (which is much more often
> the case in Python) then it should wait until runtime to check:
> 
> a=callSomeUnTypedFunction()
> range( a )

If the type checker can prove that callSomeUnTypedFunction() can
return non-integer types as well as integers I think I'd be happy to
get a warning here (as long as we're in lint mode).  It's much more
likely that the programmer didn't realize this possibility, than that
she somehow had tweaked the environment or the arguments so that
callSomeUnTypedFunction() would never return a non-int at this
particular call site, or that she would be catching the resulting
TypeError later.

Aside: I also believe that a static typechecker can easily know 99% of
all try-except statements that are currently on the call stack.
Try-except statements with a variable (that isn't a simple alias) in
the exception name slot are extremely rare, in my experience.  Of
course a lint-style checker should also warn about (1) all unqualified
except clauses, and (2) "wide" try clauses -- that is, try clauses
around lots of code that could raise the exception that is being
caught.  Bot of these are caused by sloppy coding much more frequently
than they are a necessity in the program.

--Guido van Rossum (home page: http://www.python.org/~guido/)