Skip to content

Commit

Permalink
pythongh-128661: Fix typing.evaluate_forward_ref not showing deprec…
Browse files Browse the repository at this point in the history
…ataion
  • Loading branch information
sobolevn committed Jan 9, 2025
1 parent a1284e9 commit 120915a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
46 changes: 46 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
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
Expand Down Expand Up @@ -7254,6 +7255,51 @@ class C(Generic[T]): pass
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):
Expand Down
2 changes: 1 addition & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
):
Expand Down

0 comments on commit 120915a

Please sign in to comment.