Skip to content

Instantly share code, notes, and snippets.

@minus7
Created May 8, 2016 14:47
Show Gist options
  • Select an option

  • Save minus7/01b8a84984d1b99241b80d3ff36a3e2c to your computer and use it in GitHub Desktop.

Select an option

Save minus7/01b8a84984d1b99241b80d3ff36a3e2c to your computer and use it in GitHub Desktop.

Revisions

  1. minus7 created this gist May 8, 2016.
    17 changes: 17 additions & 0 deletions type_check.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    #!/usr/bin/env python3
    # vim: set noexpandtab:
    from functools import wraps
    from inspect import signature

    def type_checked(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
    sig = signature(func)
    bound = sig.bind(*args, **kwargs)
    for k,v in bound.arguments.items():
    ann = sig.parameters[k].annotation
    if ann and not isinstance(v, ann):
    raise ValueError("Argument {} should be of type {}, is {}"
    .format(k, ann, type(v)))
    return func(*bound.args, **bound.kwargs)
    return wrapper