|
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
|
@@ -7152,6 +7153,25 @@ class C:
|
7152 | 7153 | self.assertEqual(get_type_hints(C, format=annotationlib.Format.STRING),
|
7153 | 7154 | {'x': 'undefined'})
|
7154 | 7155 |
|
| 7156 | + def test_get_type_hints_format_function(self): |
| 7157 | + def func(x: undefined) -> undefined: ... |
| 7158 | + |
| 7159 | + # VALUE |
| 7160 | + with self.assertRaises(NameError): |
| 7161 | + get_type_hints(func) |
| 7162 | + with self.assertRaises(NameError): |
| 7163 | + get_type_hints(func, format=annotationlib.Format.VALUE) |
| 7164 | + |
| 7165 | + # FORWARDREF |
| 7166 | + self.assertEqual( |
| 7167 | + get_type_hints(func, format=annotationlib.Format.FORWARDREF), |
| 7168 | + {'x': ForwardRef('undefined'), 'return': ForwardRef('undefined')}, |
| 7169 | + ) |
| 7170 | + |
| 7171 | + # STRING |
| 7172 | + self.assertEqual(get_type_hints(func, format=annotationlib.Format.STRING), |
| 7173 | + {'x': 'undefined', 'return': 'undefined'}) |
| 7174 | + |
7155 | 7175 |
|
7156 | 7176 | class GetUtilitiesTestCase(TestCase):
|
7157 | 7177 | def test_get_origin(self):
|
@@ -7254,6 +7274,51 @@ class C(Generic[T]): pass
|
7254 | 7274 | self.assertEqual(get_args(Unpack[tuple[Unpack[Ts]]]), (tuple[Unpack[Ts]],))
|
7255 | 7275 |
|
7256 | 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 | + |
7257 | 7322 | class CollectionsAbcTests(BaseTestCase):
|
7258 | 7323 |
|
7259 | 7324 | def test_hashable(self):
|
|
0 commit comments