8000 gh-128661: Fix `typing.evaluate_forward_ref` not showing deprecataion · sobolevn/cpython@120915a · GitHub
[go: up one dir, main page]

Skip to content

Commit 120915a

Browse files
committed
pythongh-128661: Fix typing.evaluate_forward_ref not showing deprecataion
1 parent a1284e9 commit 120915a

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

Lib/test/test_typing.py

Lines changed: 46 additions & 0 deletions
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
@@ -7254,6 +7255,51 @@ class C(Generic[T]): pass
72547255
self.assertEqual(get_args(Unpack[tuple[Unpack[Ts]]]), (tuple[Unpack[Ts]],))
72557256

72567257

7258+
class EvaluateForwardRefTests(BaseTestCase):
7259+
def test_evaluate_forward_ref(self):
7260+
int_ref = ForwardRef('int')
7261+
missing = ForwardRef('missing')
7262+
self.assertIs(
7263+
typing.evaluate_forward_ref(int_ref, type_params=()),
7264+
int,
7265+
)
7266+
self.assertIs(
7267+
typing.evaluate_forward_ref(
7268+
int_ref, type_params=(), format=annotationlib.Format.FORWARDREF,
7269+
),
7270+
int,
7271+
)
7272+
self.assertIs(
7273+
typing.evaluate_forward_ref(
7274+
missing, type_params=(), format=annotationlib.Format.FORWARDREF,
7275+
),
7276+
missing,
7277+
)
7278+
self.assertEqual(
7279+
typing.evaluate_forward_ref(
7280+
int_ref, type_params=(), format=annotationlib.Format.STRING,
7281+
),
7282+
'int',
7283+
)
7284+
7285+
def test_evaluate_forward_ref_no_type_params(self):
7286+
ref = ForwardRef('int')
7287+
with self.assertWarnsRegex(
7288+
DeprecationWarning,
7289+
(
7290+
"Failing to pass a value to the 'type_params' parameter "
7291+
"of 'typing.evaluate_forward_ref' is deprecated, "
7292+
"as it leads to incorrect behaviour"
7293+
),
7294+
):
7295+
typing.evaluate_forward_ref(ref)
7296+
7297+
# No warnings when `type_params` is passed:
7298+
with warnings.catch_warnings(record=True) as w:
7299+
typing.evaluate_forward_ref(ref, type_params=())
7300+
self.assertEqual(w, [])
7301+
7302+
72577303
class CollectionsAbcTests(BaseTestCase):
72587304

72597305
def test_hashable(self):

Lib/typing.py

Lines changed: 1 addition & 1 deletion
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
):

0 commit comments

Comments
 (0)
0