Last active
September 22, 2022 18:33
-
-
Save cemoody/4cfa6801b3bd9ebec023485cf1fe33ba to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from typing import get_type_hints | |
def cast(argname, value, hints): | |
""" Only cast arguments if type hints are available for them. | |
""" | |
if argname in hints: | |
expected_type = hints[argname] | |
if not issubclass(type(value), expected_type): | |
# Will throw type error if argument cannot be cast | |
value = expected_type(value) | |
return value | |
def typecast(function): | |
""" Cast the argument and keyword argument to | |
the type annotations when they are provided. | |
""" | |
def type_caster(*args, **kwargs): | |
hints = get_type_hints(function) | |
# This accesses function positional argument names | |
arg_names = function.__code__.co_varnames | |
args_casted = [cast(argname, val, hints) | |
for (argname, val) in zip(arg_names, args)] | |
kwargs_casted = {argname: cast(argname, val, hints) | |
for (argname, val) in kwargs.items()} | |
result = function(*args_casted, **kwargs_casted) | |
if 'return' in hints: | |
result = cast('return', result, hints) | |
return result | |
return type_checker | |
if __name__ == '__main__': | |
@typecast | |
def func(x: str, y, z: bool=True): | |
return (x, y, z) | |
# We give the func an int but expect a str since types will be casted | |
x, y, z = func(3, 4) | |
assert type(x) is str | |
assert type(y) is int | |
assert type(z) is bool | |
x, y, z = func(3, 4, z=1) | |
assert type(z) is bool |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment