Skip to main content

Intuitive and minimalistic type checking for Python objects

Project description

Getting Started

The first snippet below checks that an object is a list of tuples, each tuple with an int and a string. The second snippet shows how to decorate a function

activate_checktype() # by default, checktype is deactivated

o = [(1, 'hello'), (2, 'world')
checktype(o, '[(int, str)..]')


@check('str, {str:int} -> [int..]')
def myfunction(a1, a2):
    return [1,2,3]

myfunction("say", {"hello":1})

Introduction

Python is strongly, dynamically typed. This is fine, but at times when objects get too complicated, it can be a good thing to check for types.

Checktype allows simple checking of Python object formats. It has a very intuitive type system that mimics the way you define your own Python objects.

It can be particularily helpful when refactoring programs. Its performance has not been optimized and it is intended as a development-time type checking utility (disabled by default, and should probably be activated in tests only).

Installation

You can install checktype with pip:

pip install checktype

Usage examples:

from checktype import checktype, activate_checktype, check

# 'activates' checktype. You need to explicitly call this,
# else it will do nothing (by default, to save cpu time).
# It is common to specify it in tests
activate_checktype()

# checktype only provides a single function, that takes two arguments.
# the first one is the python object you want to test;
# the second one is an 'object specification' (spec), that mimics the way
# objects are represented in Python. This spec is a string representation of
# a python object. For example, to check that 12 is an int or 'hello' is a str:
checktype(12, 'int')
checktype('hello', 'str')

# A list of exactly 3 floats is specified as [float,float,float]
checktype([1.0,2.0,3.0], '[float,float,float]')

# To match a variable-length list of ints, use [int..]
checktype([1,2,3], '[int..]')

# To only check that it is a list (but not its content), use []
checktype([1, "deux"], '[]')

# And so on for tuples,
checktype((1,2,3), '(int,int,int)')
checktype((1,2,3), '(int..)')
checktype((1, "deux"), '()')
# dictionnaries,
checktype({11:2, 12: 3},  '{int:int}')
checktype({11:2, 12: 3},  '{}')
# and sets.
checktype({11, 2},  '{int}')

# Use the ? wildcard if you don't want to check for a specific part
checktype({11:2, 12: "a", 13:(3,4)},  '{int:?}')

# Further examples
checktype({11: (2,3), 12: (4,"5")}, '{ int: (int,?)}')
checktype([(2, "asdf"),(-12, "asfwe"),(1,"")], "[(int,str)..]")
checktype([(1, 'hello', 2.0)], "[(int, str, float)]")

# Decorator examples
@check("str.. -> int")
def fn_1(a1, a2):
    return 1

fn_1("say", "hello")


@check("str, {str:int} -> [int..]")
def fn_3(a1, a2):
    return [1,2,3]

fn_3("say", {"hello":1})

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

checktype-1.1.4.tar.gz (4.6 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page