gh-128661: Fix typing.evaluate_forward_ref not showing deprecation (#128663)

gh-128661: Fix `typing.evaluate_forward_ref` not showing deprecataion
This commit is contained in:
sobolevn 2025-01-09 18:15:13 +03:00 committed by GitHub
parent 43ac9f5059
commit b725297cee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 49 additions and 1 deletions

View File

@ -45,6 +45,7 @@ import abc
import textwrap
import typing
import weakref
import warnings
import types
from test.support import captured_stderr, cpython_only, infinite_recursion, requires_docstrings, import_helper, run_code
@ -7273,6 +7274,51 @@ class GetUtilitiesTestCase(TestCase):
self.assertEqual(get_args(Unpack[tuple[Unpack[Ts]]]), (tuple[Unpack[Ts]],))
class EvaluateForwardRefTests(BaseTestCase):
def test_evaluate_forward_ref(self):
int_ref = ForwardRef('int')
missing = ForwardRef('missing')
self.assertIs(
typing.evaluate_forward_ref(int_ref, type_params=()),
int,
)
self.assertIs(
typing.evaluate_forward_ref(
int_ref, type_params=(), format=annotationlib.Format.FORWARDREF,
),
int,
)
self.assertIs(
typing.evaluate_forward_ref(
missing, type_params=(), format=annotationlib.Format.FORWARDREF,
),
missing,
)
self.assertEqual(
typing.evaluate_forward_ref(
int_ref, type_params=(), format=annotationlib.Format.STRING,
),
'int',
)
def test_evaluate_forward_ref_no_type_params(self):
ref = ForwardRef('int')
with self.assertWarnsRegex(
DeprecationWarning,
(
"Failing to pass a value to the 'type_params' parameter "
"of 'typing.evaluate_forward_ref' is deprecated, "
"as it leads to incorrect behaviour"
),
):
typing.evaluate_forward_ref(ref)
# No warnings when `type_params` is passed:
with warnings.catch_warnings(record=True) as w:
typing.evaluate_forward_ref(ref, type_params=())
self.assertEqual(w, [])
class CollectionsAbcTests(BaseTestCase):
def test_hashable(self):

View File

@ -1024,7 +1024,7 @@ def evaluate_forward_ref(
owner=None,
globals=None,
locals=None,
type_params=None,
type_params=_sentinel,
format=annotationlib.Format.VALUE,
_recursive_guard=frozenset(),
):

View File

@ -0,0 +1,2 @@
Fixes :func:`typing.evaluate_forward_ref` not showing deprecation when
``type_params`` arg is not passed.