[soc2008-general] Adding Static Type Checking to Python While Retaining Dynamic Features

Ryan Freckleton ryan.freckleton at gmail.com
Tue Mar 25 22:34:57 CET 2008


Hi Tim,

You're correct, I was planning using PEP 3107 function annotations to
implement gradual typing, and to use the typecheck library as
inspiration for semantics and syntax.

Thanks,
Ryan

On Mon, Mar 24, 2008 at 10:34 PM, Tim Ansell <mithro at mithis.com> wrote:
> Hello Ryan,
>
>  Just in case you have not already looked at it, I would recommend the
>  following links,
>   http://oakwinter.com/code/typecheck/
>   http://www.python.org/dev/peps/pep-3107/
>
>  It seems like you may have already found this from your application but
>  I thought I would mention it just in case.
>
>  Tim 'Mithro' Ansell
>
>
>
>  On Mon, 2008-03-24 at 16:15 -0600, Ryan Freckleton wrote:
>  > Hello everyone,
>  >
>  > Based on some feedback I got from the #gsoc-python channel on IRC, I'm
>  > going to elaborate on my proposal of add gradual typing to Python.
>  >
>  > Gradual typing allows you to do static type checking on only parts of
>  > a program. A gradual type checker considers undeclared types as
>  > compatible with all other types. This allows programs to be quickly
>  > "sketched out" in Python and type annotations may be added later if
>  > static type checking is necessary. It is also fully backwards
>  > compatible with python programs that do not have static type
>  > information. A program using gradual typing can consist of any
>  > combination of functions that are fully type annotated, partially type
>  > annotated, or have no type annotations at all.
>  >
>  > Here's an example that would be considered type correct. The type
>  > checker would alert the user that not all type information is
>  > available statically.
>  >
>  > def gcd(a, b):
>  >     while a:
>  >         a, b = b%a, a
>  >     return b
>  >
>  > def randint() -> int:
>  >     return random.randint(1,100)
>  >
>  > print gcd(randint(), anUnTypedFunction())
>  >
>  >
>  > The next example would considered statically type correct,
>  >
>  > def gcd(a: int, b: int) -> int:
>  >
>  >     while a:
>  >         a, b = b%a, a
>  >     return b
>  >
>  > def randint() -> int:
>  >     return random.randint(1,100)
>  >
>  > print gcd(11, radint())
>  >
>  > This example would generate a static type check error before runtime,
>  > since the return type of hello is not compatible with int.
>  >
>  > def gcd(a: int, b: int) -> int:
>  >
>  >     while a:
>  >         a, b = b%a, a
>  >     return b
>  >
>  > def hello() -> str:
>  >     return "Hello!
>  >
>  > print gcd(11, hello())
>  >
>  > Gradual type checking is a way to add static type safety to python
>  > without giving up any of its dynamic features. The system is based on
>  > several papers available at: http://ece-www.colorado.edu/~siek/
>  >
>  > I look forward to your feedback.
>  >
>  > Thanks,
>
>



-- 
=====
--Ryan E. Freckleton


More information about the soc2008-general mailing list