8000 GitHub - jacopofar/runtime_typecheck: Checks that a value satisfies a Python 3.6 type
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Dec 13, 2019. It is now read-only.

jacopofar/runtime_typecheck

Repository files navigation

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.

About

Checks that a value satisfies a Python 3.6 type

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages

0