Coder Social home page Coder Social logo

typecheck.py's Introduction

Copyright 2013 Constantin Kulikov

Author: Constantin Kulikov (Bad_ptr) [email protected]
Date: 2013/07/09 21:28:06
License: GPL either version 2 or any later version
Keywords: python 3, typecheck, lib
X-URL: https://github.com/Bad-ptr/typecheck.py

typecheck.py

Intro

The core of this module is rec_type_check function.
You can extend it by defining new subclasses to type_spec class (and then using this subclasses in type signature).
The one thing I'm unshure is how correct the dict handling code couse dict is not ordered collection.
It was tested with Python 3.3.1

Usage notes

Level of error reporting is controlled via __typecheck_error_level variable:
NOTHING = -1
WARNING = 0
EXCEPTION = 1
Which could be set by set_typecheck_error_level function.

Example

from typecheck import (typecheck,accepts,returns, ts_num, set_typecheck_error_level
                       , NOTHING, WARNING, EXCEPTION)

@accepts(ts_num(int, min_num=3,max_num=3),("asd",tuple,list,[int,1]),bool)
def fun(a,b,c,d,test=False):
    print("test =", test)
    return a,b,c,d

fun(1,2,3,("asd",(),[],[2,1]),True)
# test = True
# (1, 2, 3, ('asd', (), [], [2, 1]))

fun(1,2,3,("asd",(),[],[3,1]))
# test = False
# (1, 2, 3, ('asd', (), [], [3, 1]))

returns(1,2,3,(str,(),[ts_num(int)],[1,1]))(fun)

set_typecheck_error_level(EXCEPTION) # EXCEPTION == 1

fun(1,2,3,("asd",(),[],[1,1]),True)
# 'fun' method returns ((1, 2, 3, ('asd', (), [ts_num((<class 'int'>,), min_num=1, max_num=-1)], [1, 1]))),
# but result is (((1, 2, 3, ('asd', (), [], [1, 1])),))
# TypeStack:
#  ts_num not matched: min_num=1, max_num=-1; but actual_num=0 (<class 'int'>,) to ()
# Type not matched: (ts_num((<class 'int'>,), min_num=1, max_num=-1),) to ()
# Type not matched: ([ts_num((<class 'int'>,), min_num=1, max_num=-1)], [1, 1]) to ([], [1, 1])
# Type not matched: (('asd', (), [ts_num((<class 'int'>,), min_num=1, max_num=-1)], [1, 1]),) to (('asd',(), [], [1, 1]),)
#
# Traceback ...

@typecheck()
def foo(a:int,b:bool, *vargs:((1,2),)) -> 'ret_string':
    return 'ret_string'

foo(0,False,(1,2))
# 'ret_string'

typecheck.py's People

Contributors

bad-ptr avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.