|
45 | 45 | import textwrap
|
46 | 46 | import typing
|
47 | 47 | import weakref
|
| 48 | +import warnings |
48 | 49 | import types
|
49 | 50 |
|
50 | 51 | from test.support import captured_stderr, cpython_only, infinite_recursion, requires_docstrings, import_helper, run_code
|
@@ -7273,6 +7274,51 @@ class C(Generic[T]): pass
|
7273 | 7274 | self.assertEqual(get_args(Unpack[tuple[Unpack[Ts]]]), (tuple[Unpack[Ts]],))
|
7274 | 7275 |
|
7275 | 7276 |
|
| 7277 | +class EvaluateForwardRefTests(BaseTestCase): |
| 7278 | + def test_evaluate_forward_ref(self): |
| 7279 | + int_ref = ForwardRef('int') |
| 7280 | + missing = ForwardRef('missing') |
| 7281 | + self.assertIs( |
| 7282 | + typing.evaluate_forward_ref(int_ref, type_params=()), |
| 7283 | + int, |
| 7284 | + ) |
| 7285 | + self.assertIs( |
| 7286 | + typing.evaluate_forward_ref( |
| 7287 | + int_ref, type_params=(), format=annotationlib.Format.FORWARDREF, |
| 7288 | + ), |
| 7289 | + int, |
| 7290 | + ) |
| 7291 | + self.assertIs( |
| 7292 | + typing.evaluate_forward_ref( |
| 7293 | + missing, type_params=(), format=annotationlib.Format.FORWARDREF, |
| 7294 | + ), |
| 7295 | + missing, |
| 7296 | + ) |
| 7297 | + self.assertEqual( |
| 7298 | + typing.evaluate_forward_ref( |
| 7299 | + int_ref, type_params=(), format=annotationlib.Format.STRING, |
| 7300 | + ), |
| 7301 | + 'int', |
| 7302 | + ) |
| 7303 | + |
| 7304 | + def test_evaluate_forward_ref_no_type_params(self): |
| 7305 | + ref = ForwardRef('int') |
| 7306 | + with self.assertWarnsRegex( |
| 7307 | + DeprecationWarning, |
| 7308 | + ( |
| 7309 | + "Failing to pass a value to the 'type_params' parameter " |
| 7310 | + "of 'typing.evaluate_forward_ref' is deprecated, " |
| 7311 | + "as it leads to incorrect behaviour" |
| 7312 | + ), |
| 7313 | + ): |
| 7314 | + typing.evaluate_forward_ref(ref) |
| 7315 | + |
| 7316 | + # No warnings when `type_params` is passed: |
| 7317 | + with warnings.catch_warnings(record=True) as w: |
| 7318 | + typing.evaluate_forward_ref(ref, type_params=()) |
| 7319 | + self.assertEqual(w, []) |
| 7320 | + |
| 7321 | + |
7276 | 7322 | class CollectionsAbcTests(BaseTestCase):
|
7277 | 7323 |
|
7278 | 7324 | def test_hashable(self):
|
|
0 commit comments