Message349542
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. |
|
Date |
User |
Action |
Args |
2019-08-13 10:42:59 | netbnd | set | recipients:
+ netbnd |
2019-08-13 10:42:59 | netbnd | set | messageid: <1565692979.16.0.678723196671.issue37838@roundup.psfhosted.org> |
2019-08-13 10:42:59 | netbnd | link | issue37838 messages |
2019-08-13 10:42:59 | netbnd | create | |
|