Created
November 14, 2016 07:02
-
-
Save math2001/4be59857d1802db986e62970f1daf917 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def check_type_hinter(function): | |
""" | |
Make sure you follow what you said. | |
How to use: | |
@check_type_hinter | |
def length(arg: str) -> int: | |
return len(arg) | |
length('hello') | |
length(5) # TypeError | |
@cth | |
def error() -> bool: | |
return 'an error' | |
error() # TypeError | |
By math2001 | |
""" | |
def class_name(el): | |
return el.__class__.__name__ | |
def caller(*args, **kwargs): | |
annots = function.__annotations__ | |
i = -1 | |
for arg_name, required_type in annots.items(): | |
if arg_name == 'return': | |
continue | |
else: | |
i += 1 | |
if i >= len(args): | |
break | |
if not isinstance(args[i], required_type): | |
raise TypeError("The argument '{}' should be a '{}' " | |
"('{}' given)" | |
.format(arg_name, required_type.__name__, | |
class_name(args[i]))) | |
result = function(*args, **kwargs) | |
if (annots.get('return', False) and | |
not isinstance(result, annots['return'])): | |
raise TypeError("This function does not return what it should " | |
"('{}' instead of '{}')".format(class_name(result), | |
annots['return'] | |
.__name__)) | |
return result | |
caller.__annotations__ = function.__annotations__ | |
return caller | |
def cth(*args, **kwargs): | |
# for the lazy ones (like me) | |
return check_type_hinter(*args, **kwargs) | |
class SomethingCool: | |
def __str__(self): | |
return "Python's awesome! Let's make it better. ;)" | |
@cth | |
def hello(name: str='you', end=''): | |
msg = 'Hello {}!{}'.format(name, end) | |
print(msg) | |
print(hello()) | |
from check_for_call import check_for_use | |
@check_for_use() | |
def bonjour(): | |
print('bonjour') | |
bonjour() | |
check_for_use(True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment