Created
January 9, 2022 15:43
-
-
Save karpanGit/286fb3dedd2ccd15324787503697c5ca to your computer and use it in GitHub Desktop.
python, enforcing type checking
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 f(a: int, b: str, c: float): | |
import inspect | |
args = inspect.getfullargspec(f).args | |
annotations = inspect.getfullargspec(f).annotations | |
# annotations = f.__annotations__ | |
# print(type(locals()), locals()) | |
for x in args: | |
print(x, '->', | |
'arg is', type(locals()[x]), ',', | |
'annotation is', annotations[x], | |
'/', (type(locals()[x])) is annotations[x]) | |
f(1, 'foo', 3) | |
# prints | |
# a -> arg is <class 'int'> , annotation is <class 'int'> / True | |
# b -> arg is <class 'str'> , annotation is <class 'str'> / True | |
# c -> arg is <class 'int'> , annotation is <class 'float'> / False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment