Created
November 26, 2019 14:18
-
-
Save astrofrog/0310c1f66151ca1f6e9b6854973f332c 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
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