This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Recursive type annotations do not work in documentation tests
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, levkivskyi, lovasoa, lukasz.langa
Priority: normal Keywords:

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:14adminsetgithub: 80991
2019-05-19 08:50:46levkivskyisetstatus: open -> closed
resolution: not a bug
messages: + msg342838

stage: resolved
2019-05-11 00:11:10gvanrossumsetmessages: + msg342143
2019-05-10 22:31:29levkivskyisetnosy: + gvanrossum, lukasz.langa
2019-05-10 19:42:10levkivskyisetnosy: + levkivskyi
messages: + msg342123
2019-05-06 07:34:51lovasoacreate