Skip to content

Instantly share code, notes, and snippets.

@astrofrog
Created November 26, 2019 14:18
Show Gist options
  • Save astrofrog/0310c1f66151ca1f6e9b6854973f332c to your computer and use it in GitHub Desktop.
Save astrofrog/0310c1f66151ca1f6e9b6854973f332c to your computer and use it in GitHub Desktop.
import gc
import sys
import inspect
from types import FunctionType
from typeguard import check_type
def annotation_checker(frame, event, arg):
try:
func = [obj for obj in gc.get_referrers(frame.f_code) if isinstance(obj, FunctionType)][0]
except IndexError: # Not a function
return
if event == 'return':
check_type(func.__name__, arg, func.__annotations__['return'])
sys.setprofile(annotation_checker)
def test() -> int:
return 'a'
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment