Author netbnd
Recipients netbnd
Date 2019-08-13.10:42:58
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1565692979.16.0.678723196671.issue37838@roundup.psfhosted.org>
In-reply-to
Content
When decorating a function and using a forward declaration as type hint, the typing.get_type_hints function does not work anymore, since it cannot find the forward declared name in the namespace. After debugging I think, the typing.get_type_hints function is actually using the namespace of the decorator instead of the decorated function.

When using a normal class type (no forward declaration) everything works fine and also when not using any decorator it works like expected.

As a workaround, one could pass the local namespace to typing.get_type_hints. However in normal usecases this function is used for runtime typechecking in a deep call hierarchy. So one would normally not have access to the right local namespace, only to the function object itself. 

However there is another easy workaround. At least when using the functool.wraps method to create a function decorator. The decorated functions has a "__wrapped__" attribute, which references the original function. When using "typing.get_type_hints(function.__wrapped__)" instead of "typing.get_type_hints(function)", it works like expected. So maybe this could be built in into the get_type_hints method.
History
Date User Action Args
2019-08-13 10:42:59netbndsetrecipients: + netbnd
2019-08-13 10:42:59netbndsetmessageid: <1565692979.16.0.678723196671.issue37838@roundup.psfhosted.org>
2019-08-13 10:42:59netbndlinkissue37838 messages
2019-08-13 10:42:59netbndcreate