From d09457b158254620ff1df0467070cb06e61d5961 Mon Sep 17 00:00:00 2001 From: HongWeipeng Date: Mon, 26 Aug 2019 02:41:55 +0800 Subject: [PATCH 1/2] Fix infinite recursion with typing.get_type_hints. --- Lib/typing.py | 7 +++++-- .../next/Library/2019-08-26-02-29-14.bpo-37806.wR83qu.rst | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2019-08-26-02-29-14.bpo-37806.wR83qu.rst diff --git a/Lib/typing.py b/Lib/typing.py index 5f1a0ad3d63743..52553eeb7b9a0a 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -491,7 +491,7 @@ class ForwardRef(_Final, _root=True): __slots__ = ('__forward_arg__', '__forward_code__', '__forward_evaluated__', '__forward_value__', - '__forward_is_argument__') + '__forward_is_argument__', '__hash') def __init__(self, arg, is_argument=True): if not isinstance(arg, str): @@ -505,6 +505,7 @@ def __init__(self, arg, is_argument=True): self.__forward_evaluated__ = False self.__forward_value__ = None self.__forward_is_argument__ = is_argument + self.__hash = None def _evaluate(self, globalns, localns): if not self.__forward_evaluated__ or localns is not globalns: @@ -528,7 +529,9 @@ def __eq__(self, other): self.__forward_value__ == other.__forward_value__) def __hash__(self): - return hash((self.__forward_arg__, self.__forward_value__)) + if self.__hash is None: + self.__hash = hash((self.__forward_arg__, self.__forward_value__)) + return self.__hash def __repr__(self): return f'ForwardRef({self.__forward_arg__!r})' diff --git a/Misc/NEWS.d/next/Library/2019-08-26-02-29-14.bpo-37806.wR83qu.rst b/Misc/NEWS.d/next/Library/2019-08-26-02-29-14.bpo-37806.wR83qu.rst new file mode 100644 index 00000000000000..64a1f0fbf25b42 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-08-26-02-29-14.bpo-37806.wR83qu.rst @@ -0,0 +1,2 @@ +Fix infinite recursion with typing.get_type_hints. +Patch by hongweipeng. From b727c2a114d9ad840876070607f60203791cf557 Mon Sep 17 00:00:00 2001 From: hongweipeng <961365124@qq.com> Date: Mon, 26 Aug 2019 09:09:11 +0800 Subject: [PATCH 2/2] code review --- .../next/Library/2019-08-26-02-29-14.bpo-37806.wR83qu.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2019-08-26-02-29-14.bpo-37806.wR83qu.rst b/Misc/NEWS.d/next/Library/2019-08-26-02-29-14.bpo-37806.wR83qu.rst index 64a1f0fbf25b42..50853efd7ace40 100644 --- a/Misc/NEWS.d/next/Library/2019-08-26-02-29-14.bpo-37806.wR83qu.rst +++ b/Misc/NEWS.d/next/Library/2019-08-26-02-29-14.bpo-37806.wR83qu.rst @@ -1,2 +1 @@ -Fix infinite recursion with typing.get_type_hints. -Patch by hongweipeng. +Fix infinite recursion from :func:`typing.get_type_hints`. Patch by hongweipeng.