|
8 | 8 | import itertools |
9 | 9 | import pickle |
10 | 10 | from string.templatelib import Template, Interpolation |
| 11 | +import types |
11 | 12 | import typing |
12 | 13 | import sys |
13 | 14 | import unittest |
@@ -1862,6 +1863,39 @@ def foo(a: c1_gth, b: c2_gth): |
1862 | 1863 | self.assertNotEqual(hash(c3), hash(c4)) |
1863 | 1864 | self.assertEqual(hash(c3), hash(ForwardRef("int", module=__name__))) |
1864 | 1865 |
|
| 1866 | + def test_forward_equality_and_hash_with_cells(self): |
| 1867 | + """Regression test for GH-143831.""" |
| 1868 | + class A: |
| 1869 | + def one(_) -> C1: |
| 1870 | + """One cell.""" |
| 1871 | + |
| 1872 | + one_f = ForwardRef("C1", owner=one) |
| 1873 | + one_f_ga1 = get_annotations(one, format=Format.FORWARDREF)["return"] |
| 1874 | + one_f_ga2 = get_annotations(one, format=Format.FORWARDREF)["return"] |
| 1875 | + self.assertIsInstance(one_f_ga1.__cell__, types.CellType) |
| 1876 | + self.assertIs(one_f_ga1.__cell__, one_f_ga2.__cell__) |
| 1877 | + |
| 1878 | + def two(_) -> C1 | C2: |
| 1879 | + """Two cells.""" |
| 1880 | + |
| 1881 | + two_f_ga1 = get_annotations(two, format=Format.FORWARDREF)["return"] |
| 1882 | + two_f_ga2 = get_annotations(two, format=Format.FORWARDREF)["return"] |
| 1883 | + self.assertIsNot(two_f_ga1.__cell__, two_f_ga2.__cell__) |
| 1884 | + self.assertIsInstance(two_f_ga1.__cell__, dict) |
| 1885 | + self.assertIsInstance(two_f_ga2.__cell__, dict) |
| 1886 | + |
| 1887 | + type C1 = None |
| 1888 | + type C2 = None |
| 1889 | + |
| 1890 | + self.assertNotEqual(A.one_f, A.one_f_ga1) |
| 1891 | + self.assertNotEqual(hash(A.one_f), hash(A.one_f_ga1)) |
| 1892 | + |
| 1893 | + self.assertEqual(A.one_f_ga1, A.one_f_ga2) |
| 1894 | + self.assertEqual(hash(A.one_f_ga1), hash(A.one_f_ga2)) |
| 1895 | + |
| 1896 | + self.assertEqual(A.two_f_ga1, A.two_f_ga2) |
| 1897 | + self.assertEqual(hash(A.two_f_ga1), hash(A.two_f_ga2)) |
| 1898 | + |
1865 | 1899 | def test_forward_equality_namespace(self): |
1866 | 1900 | def namespace1(): |
1867 | 1901 | a = ForwardRef("A") |
|
0 commit comments