Issue 36810: Recursive type annotations do not work in documentation tests (original) (raw)

Issue36810

Created on 2019-05-06 07:34 by lovasoa, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_recursive.py lovasoa,2019-05-06 07:34 Example failing file
Messages (4)
msg341489 - (view) Author: Lovasoa (lovasoa) Date: 2019-05-06 07:34
When using documentations tests to test a function that uses typing.get_type_hints, an error is thrown. The error is thrown when trying to get the type hints for a class that was defined in a documentation test. I was able to reproduce it both on current and PEP563 type annotations: https://gist.github.com/lovasoa/74ea62a89f5bf073b0e0c2f222008ae3
msg342123 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2019-05-10 19:42
I think this is related to doc tests being executed in a namespace where the class definition is not available. I am not sure what is the best way here, a workaround is to explicitly pass the namespace, for example this passes: import typing def f(clazz, ns): """ >>> class MyClass: ... my_field: 'MyClass' >>> f(MyClass, globals()) """ typing.get_type_hints(clazz, ns) You can maybe make the ns parameter optional, but it still doesn't look like a good solution.
msg342143 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-05-11 00:11
I don't think there's an actionable bug here.
msg342838 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2019-05-19 08:50
> I don't think there's an actionable bug here. OK, then taking into account there is a decent workaround, I am closing this.
History
Date User Action Args
2022-04-11 14:59:14 admin set github: 80991
2019-05-19 08:50:46 levkivskyi set status: open -> closedresolution: not a bugmessages: + stage: resolved
2019-05-11 00:11:10 gvanrossum set messages: +
2019-05-10 22:31:29 levkivskyi set nosy: + gvanrossum, lukasz.langa
2019-05-10 19:42:10 levkivskyi set nosy: + levkivskyimessages: +
2019-05-06 07:34:51 lovasoa create