from
Sylvain Thénault <sylvain.thenault at logilab dot fr>
to
David G. Wonnacott <davew at cs dot haverford dot edu>
cc
Sylvain Thenault <syt at logilab dot fr>
subject
Re: type checker for Pylint?
date
On Wednesday 08 June à 20:52, David G. Wonnacott wrote:2005/06/10 18:57
> We are considering adopting Python for our introductory computer
> science courses, but are alarmed by the idea of having to personally
> help students track down any number of bugs that would have been
> caught by C++'s type checker. (Not that we mind personally helping
> students, but it is a waste of our time to do something that can be
> automated...)
Just some thoughts... Python is a great language to introduce students
to computer science. You may get feedback from others using the mailing
list of the python educational SIG (http://python.org/sigs/edu-sig/), if
you're not already aware about it. And I'm really not sure that the lake
for a type checker is a penalty for you or your students. "Dynamic"
typing also leads to a different way to program, and with some minimum
good programming habits like automated testing, you don't even think
about your old type checker. But this is another topic, maybe more about
that later... Just to say that I sincerely hope python will be your
choice :)
> I am hoping to find either (a) a type checker that relies on
> assertions about types to find errors in a way similar to C++, if one
> adopts the coding standard that one must declare variable types with
> assertions, or better yet (b) a type checker based on something like
> the Hindley-Milner type inference algorithm, which would make use of
> assertions about types but not require them everywhere, and thus
> detect inconsistencies without requiring a huge number of type
> declarations (often none at all, if we make use of type information
> about basic operations and libraries).
>
> Do you know if anyone has added either (a) or (b) as an option in
> Pylint?
Hum, not really. There is another pylint project [1] which aims to be a
static type checker, but the last time I tried it it didn't seemed
really usable, and there is no recent development activity (i'm aware of
at least). You may also be interested in the pypy project [2], which has
a run time type infererer.
[1] http://www.dotfunk.com/projects/pylint
http://mozart.chat.net/~jeske/Projects/PyLint/
[2] http://codespeak.net/pypy
> If not, can you tell be about any documentation for how to
> write new "checkers" (or whatever the right term is) and provisions
> for accepting contributions to Pylint?
checker is the right term. Unfortunatly there is not a lot of
documentation about writing new checker (you're the first one asking for
it actually ;). You can find some simple examples in the examples
directory of the distribution (custom.py and custom_raw.py). I'll try to
quickly explain the essential here.
First, there is two kinds of checker :
* raw checkers, which are analysing each module as a raw file stream
* ast checkers, which are working on an ast representation of the module
The ast representation used is an extension of the one provided with the
standard python distribution in the compiler package [3]. The extension
adds additional information and methods on the tree nodes to ease
navigation and code introspection, and it's a part of the logilab'c
common library [4] (the astng subpackage).
An AST checker is a visitor, and should implements
visit_<lowered class name>
leave_<lowered class name>
methods for the nodes it's interested in. Checkers are ordered by
priority. For each module, pylint's engine:
1. give the module source file as a stream to raw checkers
2. get an ast representation for the module
3. make a depth first descent of the tree, calling visit_<> on each AST
checker when entering a node, and living_<> on the back traversal
Notice that the source code is probably the best source of
documentation, it should be clear and well documented. Don't hesitate to
ask for any information, I would be really happy to have a type checker
in pylint so I'm willing to help ;) Of course I would happily include it
in the distribution if you're agreeing the GPL terms.
[3] http://python.org/doc/current/lib/module-compiler.html
[4] http://www.logilab.org/projects/common/
PS: I'm ccing the python-projects list which hosts pylint related
discussion.
regards,
--
Sylvain Thénault LOGILAB, Paris (France).
http://www.logilab.com http://www.logilab.fr http://www.logilab.org
