[Q] Static type checker for Python
Neel Krishnaswami
Neel.Krishnaswami at p98.f112.n480.z2.fidonet.org
Thu Jul 1 12:17:48 EDT 1999
More information about the Python-list mailing list
Thu Jul 1 12:17:48 EDT 1999
- Previous message (by thread): TopSort in Python?
- Next message (by thread): [Q] Static type checker for Python
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
From: neelk at brick.cswv.com (Neel Krishnaswami) In article <377ADAD7.EA105C50 at mojam.com>, Skip Montanaro <skip at mojam.com> wrote: >> Can I make static type checker for Python? >> or Is there any previous work about this? > >In theory, you could do some type inference and generate some warning >messages, but since you don't declare variables to be of a particular >type in Python, it's unlikely it would be very useful. The same >variable can point to many different types at runtime. I'm about to go off on a tangent, so first I want to warn the original poster that there aren't too many actual problems with dynamic typing -- people have written huge and complex apps with no problems using Smalltalk and Lisp, for example. Now, it is possible to teach Python to check the argument types of functions at runtime, and I've written a little module called TypeChecker to do just that. It's a wrapper class with callable instances, and the instance checks the declaration you make in a docstring against the actual types and classes of the arguments. For example: def fact(n): """foo(n :: LongType) <-- here's the type declaration; it can be either a type from the types module or a class name. A factorial that only takes longs as arguments.""" f = 1L for i in range(1L, n+1): f = f * i return f f = TypeChecker(fact) >>> fact(3) 6L >>> f(3L) 6L >>>f(3) Traceback (innermost last): File "<stdin>", line 1, in ? File "/usr/tmp/python-6-237", line 25, in __call__ File "/usr/tmp/python-6-237", line 118, in check_args TypeError: Argument 1 of fact wrong type, <type 'int'>, declared <type 'long int'> What it's doing is parsing the type declaration in the doc string to generate a list of types and classes, and then the __call__ method of the instance is comparing the declared types against the runtime arguments it receives before running the function. I've always wanted to abuse docstrings, and now I've gotten my chance. :) Though this is a fairly mild abuse, considering that it doesn't block you from putting other things into the docstring. Obviously, this is not terribly useful. (It doesn't handle subclasses correctly, f'ex, and can't yet be used on class methods.) But it is interesting to see what Python static types might look like. (The answer to my eyes is "fairly ugly;" real lexical scoping and type/class unification seem to me to be prerequisites before static typing will behave gracefully in Python.) The actual code for TypeChecker is a little too long to put in the post, so here's a URL: http://www.sff.net/people/neelk/free-software/TypeChecker.py Neel
- Previous message (by thread): TopSort in Python?
- Next message (by thread): [Q] Static type checker for Python
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list