bpo-37953: Fix ForwardRef hash and equality checks (GH-15400)

Ideally if we stick a ForwardRef in a dictionary we would like to reliably be able to get it out again.

https://bugs.python.org/issue37953
(cherry picked from commit e082e7cbe4)

Co-authored-by: plokmijnuhby <39633434+plokmijnuhby@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2019-09-13 13:00:39 -07:00 committed by GitHub
parent cd85200f6d
commit e91edfed42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 123 additions and 3 deletions

View file

@ -524,11 +524,13 @@ class ForwardRef(_Final, _root=True):
def __eq__(self, other):
if not isinstance(other, ForwardRef):
return NotImplemented
return (self.__forward_arg__ == other.__forward_arg__ and
self.__forward_value__ == other.__forward_value__)
if self.__forward_evaluated__ and other.__forward_evaluated__:
return (self.__forward_arg__ == other.__forward_arg__ and
self.__forward_value__ == other.__forward_value__)
return self.__forward_arg__ == other.__forward_arg__
def __hash__(self):
return hash((self.__forward_arg__, self.__forward_value__))
return hash(self.__forward_arg__)
def __repr__(self):
return f'ForwardRef({self.__forward_arg__!r})'