1. Gradual
  2. Untitled project
  3. py-gradual

Overview

HTTPS SSH

Gradual Typing for Python

This package provides the runtime type-checking semantics of Gradual Typing for Python. Get a short introduction to gradual typing here.

Installing

This package only works on Python 3. To install gradual on your system, you must first get the Python 3 version of pip-3.2:

sudo apt-get install python3-pip

Now, you can easily install the gradual package via:

pip install gradual

If you wish to install it to your base system, then the above command would require root. If you already have gradual and wish to upgrade the existing package then you can use the --upgrade flag:

pip install --upgrade gradual

Usage

Once you have installed the package, fire up the Python 3 interpreter and just do:

from gradual import *

@typed
def calculate_total(a:int, b:int) -> int:
    return a + b//100

If you would like to avoid the from gradual import * syntax, you can manually import all the names such as:

from gradual import typed, obj, func

Any class or function marked with typedon decorator will produce a typed class or a typed function. Consider the following example:

from gradual import *

@typed
class DogInterface(object):

    def wag_tail()->str:
        return 'woff woff'


@typed
def use_dog(dog: DogInterface):
    print(dog.wag_tail())


class Cat(object):
    pass


c = Cat()
c.wag_tail = lambda: 'meow'
use_dog(c)