Skip to content

Instantly share code, notes, and snippets.

@karpanGit
Created January 9, 2022 15:43
Show Gist options
  • Save karpanGit/286fb3dedd2ccd15324787503697c5ca to your computer and use it in GitHub Desktop.
Save karpanGit/286fb3dedd2ccd15324787503697c5ca to your computer and use it in GitHub Desktop.
python, enforcing type checking
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