[issue37838] typing.get_type_hints not working with forward-declaration and decorated functions
Netzeband added the comment: Thanks a lot! -- ___ Python tracker <https://bugs.python.org/issue37838> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue37838] typing.get_type_hints not working with forward-declaration and decorated functions
Netzeband added the comment: Hello, I'm very sorry, I was not able to find the time to finish the patch in the quality I wanted to have. So @benedwards14 please feel free to provide a pull request. Best regards, André -- ___ Python tracker <https://bugs.python.org/issue37838> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue37835] typing.get_type_hints wrong namespace for forward-declaration of inner class
Netzeband added the comment: Thanks for your explanations. It sounds, that there is no way to fix this issue officially. Only the meta-class workaround is left, which is - as you highlight - really just a workaround. I anyway always wonder, why functions, which are methods, do not hold a reference to the class, which they belong to. It could help here and second, this would be a good way to differentiate if a function object is a method or not. -- ___ Python tracker <https://bugs.python.org/issue37835> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue37835] typing.get_type_hints wrong namespace for forward-declaration of inner class
Netzeband added the comment: I think I found a better workaround (by accident). I was debugging another issue with get_type_hints for the case that this function is used inside the __new__ method of a metaclass and with methods of the class to define. Here the same issue happens: Forward declared type names are not inside the namespace of the function. However, inside the __new__ method of the metaclass, you already know the name of the class you want to define and you know the class-object to define. With this information it is very easy to add the missing reference to the __globals__ list of all methods found in the namespace. So the workaround is to use a metaclass, which adds the class-name and class-object to the global namespace of the methods of this class. It works also in case of classes, which are inherited, from a class, which uses this metaclass. This leads also to a non-workaround solution: Why is the own class not always inside the __globals__ list of the methods? Is there a reason? Or is this just a missing feature of the python-core? What are you thinking about this solution? Do you see any issues or corner-cases with that? Working code is attached to this ticket (metaclass_workaround.py) -- Added file: https://bugs.python.org/file48562/metaclass_workaround.py ___ Python tracker <https://bugs.python.org/issue37835> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue37835] typing.get_type_hints wrong namespace for forward-declaration of inner class
Netzeband added the comment: I tried my idea with the small example code above. However it does not work like expected: (see zipped example project, attached to this comment) At the moment where the function decorator is applied to the method of the inner class, the local namespace ("locals()") does not contain any inner class. Even not another inner class, define before the corresponding class. So the only way to get it working is to add the __locals__ attribute manually after defining the class, which is even more ugly than my suggested workaround with the function decorator :-( Any further ideas about this? -- Added file: https://bugs.python.org/file48552/get_type_hints_for_inner_classes.zip ___ Python tracker <https://bugs.python.org/issue37835> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue37835] typing.get_type_hints wrong namespace for forward-declaration of inner class
Netzeband added the comment: Thanks for your response. I was also thinking much about it and was not able to find a nice idea how to get this working. The problem is, that we loose the local-namespace information as soon as we leave the context of the function, where the class was defined in. This information is not stored in the function object of the method, we want to get the type hints from. The only open question, I have in this context is: Why can python resolve the reference to class A (also a inner class, but no forward declaration)? Is there any chance to use the same mechanism also for forward declared references? Beside from this question, I will give you my thoughts, how I think this issue could be addressed: - What we need is the local namespace information from the function, where the inner class was defined in. This is not stored from python in the function object of this class (but __global__ is stored). - Maybe any upcoming python version could store this information in __local__ ? So maybe we could clone this ticket to the python core in order to address this? - As long as python is not storing this information, a workaround could be used: We could define a function decorator, which adds the attribute __local__ to a function object. I think about this syntax: class InnerClass(): @store_namespace(locals()) def method() -> 'InnerClass': ... - The get_type_hints function is then checking for the __local__ attribute and if it exits it is passed to the function, which resolves the forward declaration. This workaround is not beautiful, since it requires manual work for those methods (adding the function decorator to those methods). Furthermore - without knowing the internals of the get_type_hints function - this workaround is not straight forward and easy to understand. So it needs to be carefully documented and even then I expect confusing questions on StackOverflow or other communities. However, it is a quite rare case that someone really needs to use inner classes. Normally one could simply put the class in the global namespace. But when this happens and there is really a need for it, than this workaround would make it possible. Furthermore, checking a __local__ attribute of the function object would be a nice preparation for a feature request to the python core, which should store the reference to the local namespace for every function object, without using any decorators. What do you think? -- ___ Python tracker <https://bugs.python.org/issue37835> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue37838] typing.get_type_hints not working with forward-declaration and decorated functions
Netzeband added the comment: Thanks for your feedback. I can create a pull-request. However, I did it never before for the Python-Lib, so I first have to setup the correct environment for that and ensure, that I can run all test-cases. Since I'm on vacation soon, it could take several weeks. If someone wants to solve the issue earlier, please don't hesitate. I will write it in this issue ticket, as soon as I finished the PR. -- ___ Python tracker <https://bugs.python.org/issue37838> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue37838] typing.get_type_hints not working with forward-declaration and decorated functions
New submission from Netzeband : 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 <https://bugs.python.org/issue37838> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue37835] typing.get_type_hints wrong namespace for forward-declaration of inner class
New submission from Netzeband : When evaluating the type-hints of an inner-class (a class defined inside a function scope), the forward declaration does not work correctly. In this case the typing.get_type_hints does not get the right namespace, thus the class-string specified is not found. When using the same syntax for a normal class (defined in global space) it works, or when using another class instead a forward declaration, also everything works. As a workaround one could pass the local namespace (locals()) from the function where the class has been defined in to typing.get_type_hints. However in normal situations the typing.get_type_hints call is deep in the call hierarchy to do some runtime type-checks and at this point only the reference to the function-object is existing and no-one is aware of the fact, that this is just a method defined in a inner class. >From the outside perspective one would expect, that typing.get_type_hints >reacts the same, independent of the type of class. -- components: Library (Lib) files: typing_check.py messages: 349535 nosy: netbnd priority: normal severity: normal status: open title: typing.get_type_hints wrong namespace for forward-declaration of inner class type: behavior versions: Python 3.6, Python 3.7 Added file: https://bugs.python.org/file48539/typing_check.py ___ Python tracker <https://bugs.python.org/issue37835> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com