Skip to content

Commit 5e80fee

Browse files
gh-129463: Remove two attributes from ForwardRef equality (#132283)
1 parent 884df11 commit 5e80fee

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

Lib/annotationlib.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,6 @@ def __eq__(self, other):
225225
# because dictionaries are not hashable.
226226
and self.__globals__ is other.__globals__
227227
and self.__forward_is_class__ == other.__forward_is_class__
228-
and self.__code__ == other.__code__
229-
and self.__ast_node__ == other.__ast_node__
230228
and self.__cell__ == other.__cell__
231229
and self.__owner__ == other.__owner__
232230
)
@@ -237,8 +235,6 @@ def __hash__(self):
237235
self.__forward_module__,
238236
id(self.__globals__), # dictionaries are not hashable, so hash by identity
239237
self.__forward_is_class__,
240-
self.__code__,
241-
self.__ast_node__,
242238
self.__cell__,
243239
self.__owner__,
244240
))

Lib/test/test_typing.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6539,6 +6539,13 @@ def test_or(self):
65396539
self.assertEqual(X | "x", Union[X, "x"])
65406540
self.assertEqual("x" | X, Union["x", X])
65416541

6542+
def test_multiple_ways_to_create(self):
6543+
X1 = Union["X"]
6544+
self.assertIsInstance(X1, ForwardRef)
6545+
X2 = ForwardRef("X")
6546+
self.assertIsInstance(X2, ForwardRef)
6547+
self.assertEqual(X1, X2)
6548+
65426549

65436550
class InternalsTests(BaseTestCase):
65446551
def test_deprecation_for_no_type_params_passed_to__evaluate(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Comparison of :class:`annotationlib.ForwardRef` objects no longer uses the
2+
internal ``__code__`` and ``__ast_node__`` attributes, which are used as
3+
caches.

0 commit comments

Comments
 (0)