Skip to content

bpo-37806:Fix infinite recursion with typing.get_type_hints. #15493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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})'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix infinite recursion from :func:`typing.get_type_hints`. Patch by hongweipeng.