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: Infinite recursion with typing.get_type_hints
Type: crash Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: levkivskyi Nosy List: hongweipeng, levkivskyi, vg0377467
Priority: normal Keywords: patch

Created on 2019-08-10 05:26 by vg0377467, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 15493 closed hongweipeng, 2019-08-25 18:43
PR 15559 closed hongweipeng, 2019-08-28 11:08
Messages (3)
msg349330 - (view) Author: Valerio G (vg0377467) Date: 2019-08-10 05:26
I encountered one condition where calling get_type_hints causes infinite recursion when dealing with forward declaration and cyclic types.

Here's an example:

from typing import Union, List, get_type_hints

ValueList = List['Value']
Value = Union[str, ValueList]

class A:
    a: List[Value]

get_type_hints(A, globals(), locals())
This reaches the recursion limit as of 3.8.0b2.

It seems that the combining _GenericAlias with ForwardRef is what triggers this condition:

ForwardRef._evaluate sets __forward_value__ on its first call on a given instance
_GenericAlias tries to compare its args post evaluation
If one of the arguments is a previously evaluated forward reference containing a cycle, then it will infinitely recurse in the hash function when building a frozen set for the comparison.
The above is, of course, a very artificial example, but I can imagine this happening a lot in code with trees or similar structures.
My initial reproduction case was using _eval_type to resolve forward references returned by get_args (side note: it would be nice to have a public function to do that).
msg352400 - (view) Author: hongweipeng (hongweipeng) * Date: 2019-09-14 03:08
This report can be closed, PR #15400 has solved this issue.
msg352401 - (view) Author: hongweipeng (hongweipeng) * Date: 2019-09-14 03:10
Sorry, the link is https://github.com/python/cpython/pull/15400 .
History
Date User Action Args
2022-04-11 14:59:18adminsetgithub: 81987
2019-09-14 05:07:37matrixisesetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-09-14 03:10:38hongweipengsetmessages: + msg352401
2019-09-14 03:08:51hongweipengsetnosy: + hongweipeng
messages: + msg352400
2019-08-28 11:08:12hongweipengsetpull_requests: + pull_request15232
2019-08-25 20:36:46gvanrossumsetnosy: - gvanrossum
2019-08-25 18:43:19hongweipengsetkeywords: + patch
stage: patch review
pull_requests: + pull_request15181
2019-08-10 16:46:23levkivskyisetassignee: levkivskyi
2019-08-10 07:31:54xtreaksetnosy: + gvanrossum, levkivskyi
2019-08-10 05:32:30vg0377467settype: crash
2019-08-10 05:26:57vg0377467create