Skip to content

Commit b725297

Browse files
authored
gh-128661: Fix typing.evaluate_forward_ref not showing deprecation (#128663)
gh-128661: Fix `typing.evaluate_forward_ref` not showing deprecataion
1 parent 43ac9f5 commit b725297

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

Lib/test/test_typing.py

+46
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import textwrap
4646
import typing
4747
import weakref
48+
import warnings
4849
import types
4950

5051
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
72737274
self.assertEqual(get_args(Unpack[tuple[Unpack[Ts]]]), (tuple[Unpack[Ts]],))
72747275

72757276

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+
72767322
class CollectionsAbcTests(BaseTestCase):
72777323

72787324
def test_hashable(self):

Lib/typing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ def evaluate_forward_ref(
10241024
owner=None,
10251025
globals=None,
10261026
locals=None,
1027-
type_params=None,
1027+
type_params=_sentinel,
10281028
format=annotationlib.Format.VALUE,
10291029
_recursive_guard=frozenset(),
10301030
):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixes :func:`typing.evaluate_forward_ref` not showing deprecation when
2+
``type_params`` arg is not passed.

0 commit comments

Comments
 (0)