[Types-sig] Type Annotations Progress report

Paul Prescod paulp@ActiveState.com
Thu, 15 Mar 2001 08:14:45 -0800


Before I documented everything I wanted to do a real-world test. So I
added type annotations to the os module. I even added a new type that
was local to that module for handling P_WAIT, P_NOWAIT. I'd appreciate
if people would at least take a cursory peek at the os module, if not
the implementation.

OS: www.prescod.net/pytypes/os.py
Implementation: http://www.prescod.net/pytypes/typecheck.py

I'm going to release it with one big known limitation. The stack traces
it produces are very useful if you know how to read them but somewhat
ugly compared to what they will be eventually. I'm trying to figure out
how to fix that...

Remember that this is experimental, transitional, prototype, type
annotation system. The syntax is not ideal. Nevertheless, I think it is
quite simple, easy to use and immediately useful. Try making a small
program that calls some of the "os" functions. Tell me if any of them
fail when they should not.

For instance, this is what Python *used* to do when you made a mistake
in os.environ:

C:\data\research\pytypes\peps\src>python bad.py
Traceback (most recent call last):
  File "bad.py", line 2, in ?
    os.getenv(5)
  File "c:\python20\lib\os.py", line 321, in getenv
    return environ.get(key, default)
  File "c:\python20\lib\os.py", line 298, in get
    return self.data.get(key.upper(), failobj)
AttributeError: 'int' object has no attribute 'upper'

This is what it does NOW:

C:\data\research\pytypes\peps\src>python bad.py
Traceback (most recent call last):
  File "bad.py", line 2, in ?
    os.getenv(5)
  File ".\os.py", line 437, in getenv
    __paramcheck__()
  File ".\typecheck.py", line 167, in __paramcheck__
    raise InterfaceError(name, obj, type)
typecheck.InterfaceError: Parameter 'key', expected Unicode or 8-bit
string.
Instead it got '5' (<type 'int'>)

That's already an improvement! Instead of getting an error about a
method I've never heard of on an object I may never of heard of on a
random line of os.py, I got an obvious parameter error with a name from
the parameter list and the erroneous value I passed it.

This is what it should do when I have cleaned everything up:

Traceback (most recent call last):
  File "bad.py", line 2, in ?
    os.getenv(5)
  File ".\os.py", line 433, in getenv
    def getenv(key, default=None):
InterfaceError: Parameter 'key', expected Unicode or 8-bit string, got
'5'
Instead it got '5' (<type 'int'>)

If anybody knows how I can control the traceback associated with the
exception better (especially to eliminate one or more levels of
traceback), I'd appreciate it. One trick is to rewrite in C. I'd like
something less drastic while I'm still experimenting. :)

Thanks for all of the advice I got from people!
-- 
Take a recipe. Leave a recipe.  
Python Cookbook!  http://www.activestate.com/pythoncookbook