Skip to content

Instantly share code, notes, and snippets.

@dvl
Created February 28, 2015 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvl/6ab65771e34ce40519f1 to your computer and use it in GitHub Desktop.
Save dvl/6ab65771e34ce40519f1 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import functools
def ensure_types(f):
@functools.wraps(f)
def wrapper(*args: list, **kwargs: dict):
if f.__annotations__:
for arg in args:
if not isinstance(arg, f.__annotations__[arg]):
raise TypeError('Argument "{0}" should be of type {1}'.format(arg, f.__annotations__[arg]))
f(*args, **kwargs)
return wrapper
@ensure_types
def test(a: str, b: str, c: str) -> str:
return a, b, c
if __name__ == '__main__':
print(dir(test('a', 'b', 'c')))
print(dir(test(1, 2, 3)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment