Welcome to TypeWrap’s documentation!

Contents:

created by Austin Poor

https://travis-ci.com/a-poor/typewrap.svg?branch=master https://img.shields.io/pypi/v/typewrap https://img.shields.io/pypi/pyversions/typewrap https://codecov.io/gh/a-poor/typewrap/branch/master/graph/badge.svg Documentation Status

A super tiny python package for function typechecking.

Requirements

  • python>=3.6

Installation

Installing with pip:

$ pip install typewrap

Usage

exception typewrap.TypeCheckError

Bases: TypeError

Subclass of TypeError for type checking input or output exceptions.

typewrap.checkInputs(f: Callable) → Callable

Function input type-checking decorator.

Wraps a function with argument type annotations. When function is called, checks input arguments against annotated types.

Parameters

f – Function with argument type hints to check

Raises

TypeCheckError – If not isinstance(argValue,argType)

Examples:

>>> @checkInputs
>>> def add(a: int, b: int
...     return a + b
>>> add(1, 2)   # Works
>>> add(1, 2.0) # Raises TypeCheckError
typewrap.checkOutputs(f: Callable) → Callable

Function output type-checking decorator.

Wraps a function with argument type annotations. When function is called, checks output value type against annotated type.

Parameters

f – Function with return type hint to check

Raises

TypeCheckError – If not isinstance(returnValue,returnType)

Examples:

>>> @checkOutputs
>>> def add(a, b) -> int:
...     return a + b
>>> add(1, 2)   # Works

>>> @checkOutputs
>>> def add(a, b) -> str:
...     return a + b
>>> add(1, 2)   # Raises TypeCheckError

Indices and tables