[issue37838] typing.get_type_hints not working with forward-declaration and decorated functions

Netzeband report at bugs.python.org
Tue Aug 13 06:42:59 EDT 2019


New submission from Netzeband <andre at netzeband.eu>:

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.

----------
components: Library (Lib)
files: typing_check_wrapped.zip
messages: 349542
nosy: netbnd
priority: normal
severity: normal
status: open
title: typing.get_type_hints not working with forward-declaration and decorated functions
type: behavior
versions: Python 3.6, Python 3.7
Added file: https://bugs.python.org/file48540/typing_check_wrapped.zip

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37838>
_______________________________________


More information about the Python-bugs-list mailing list