Git Product home page Git Product logo

runtime_typecheck's Introduction

Travis CI badge pypy version badge

runtime typecheck

NOTE: this was fun to implement and I learned a lot about Python type metadata, but if you are looking for a runtime type checker look at `Pydantic<https://pydantic-docs.helpmanual.io/>`_

Checks that a value satisfies a Python 3.6 type at runtime

This code uses Python 3.6 type hints and the typing package to provide a simple runtime type check.

The check can be done explicitly calling the check_type function or with the @check_args function decorator which raises an exception when the argument passed to the function do not match the type hints

Example:

from runtime_typechecker import check_type, check_args
from typing import Tuple
assert check_type((1, 67), Tuple[int, int])
assert not check_type((1, "new york"), Tuple[int, int])

@check_args
def dummy_fun(a: int = 0, b: str = '', c: Tuple[int, str] = (0, '')) -> int:
    return a + len(b) + c[0] + len(c[1]) + 7
# this will throw a TypeException with the list of the issues. The decorated function is not even called in this case
dummy_fun(a='1', c=(1,42))
# this will work
dummy_fun(a=3)

Currently not all of the components of Python 3.6 typing module are supported, more coming.

runtime_typecheck's People

Contributors

acifani avatar jacopofar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.