|
65 | 65 | ReadOnly, |
66 | 66 | Required, |
67 | 67 | Self, |
| 68 | + Sentinel, |
68 | 69 | Set, |
69 | 70 | Tuple, |
70 | 71 | Type, |
@@ -9096,5 +9097,44 @@ def test_invalid_special_forms(self): |
9096 | 9097 | self.assertIs(evaluate_forward_ref(typing.ForwardRef("ClassVar", is_argument=False), globals=vars(typing)), ClassVar) |
9097 | 9098 |
|
9098 | 9099 |
|
| 9100 | +class TestSentinels(BaseTestCase): |
| 9101 | + def test_sentinel_no_repr(self): |
| 9102 | + sentinel_no_repr = Sentinel('sentinel_no_repr') |
| 9103 | + |
| 9104 | + self.assertEqual(sentinel_no_repr._name, 'sentinel_no_repr') |
| 9105 | + self.assertEqual(repr(sentinel_no_repr), '<sentinel_no_repr>') |
| 9106 | + |
| 9107 | + def test_sentinel_explicit_repr(self): |
| 9108 | + sentinel_explicit_repr = Sentinel('sentinel_explicit_repr', repr='explicit_repr') |
| 9109 | + |
| 9110 | + self.assertEqual(repr(sentinel_explicit_repr), 'explicit_repr') |
| 9111 | + |
| 9112 | + @skipIf(sys.version_info < (3, 10), reason='New unions not available in 3.9') |
| 9113 | + def test_sentinel_type_expression_union(self): |
| 9114 | + sentinel = Sentinel('sentinel') |
| 9115 | + |
| 9116 | + def func1(a: int | sentinel = sentinel): pass |
| 9117 | + def func2(a: sentinel | int = sentinel): pass |
| 9118 | + |
| 9119 | + self.assertEqual(func1.__annotations__['a'], Union[int, sentinel]) |
| 9120 | + self.assertEqual(func2.__annotations__['a'], Union[sentinel, int]) |
| 9121 | + |
| 9122 | + def test_sentinel_not_callable(self): |
| 9123 | + sentinel = Sentinel('sentinel') |
| 9124 | + with self.assertRaisesRegex( |
| 9125 | + TypeError, |
| 9126 | + "'Sentinel' object is not callable" |
| 9127 | + ): |
| 9128 | + sentinel() |
| 9129 | + |
| 9130 | + def test_sentinel_not_picklable(self): |
| 9131 | + sentinel = Sentinel('sentinel') |
| 9132 | + with self.assertRaisesRegex( |
| 9133 | + TypeError, |
| 9134 | + "Cannot pickle 'Sentinel' object" |
| 9135 | + ): |
| 9136 | + pickle.dumps(sentinel) |
| 9137 | + |
| 9138 | + |
9099 | 9139 | if __name__ == '__main__': |
9100 | 9140 | main() |
0 commit comments