summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsobolevn <mail@sobolevn.me>2025-01-09 15:15:13 (GMT)
committerGitHub <noreply@github.com>2025-01-09 15:15:13 (GMT)
commitb725297cee9e5608b709f3c7291d974c97f68fff (patch)
tree076bb35a295445be234705b0e0d037b366a0c907
parent43ac9f505903ba806aa6a5d93e6a67beb04bebc4 (diff)
downloadcpython-b725297cee9e5608b709f3c7291d974c97f68fff.zip
cpython-b725297cee9e5608b709f3c7291d974c97f68fff.tar.gz
cpython-b725297cee9e5608b709f3c7291d974c97f68fff.tar.bz2
gh-128661: Fix `typing.evaluate_forward_ref` not showing deprecation (#128663)
gh-128661: Fix `typing.evaluate_forward_ref` not showing deprecataion
-rw-r--r--Lib/test/test_typing.py46
-rw-r--r--Lib/typing.py2
-rw-r--r--Misc/NEWS.d/next/Library/2025-01-09-12-06-52.gh-issue-128661.ixx_0z.rst2
3 files changed, 49 insertions, 1 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 1c86b95..c51ee76 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -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):
diff --git a/Lib/typing.py b/Lib/typing.py
index e69b485..66570db 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -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(),
):
diff --git a/Misc/NEWS.d/next/Library/2025-01-09-12-06-52.gh-issue-128661.ixx_0z.rst b/Misc/NEWS.d/next/Library/2025-01-09-12-06-52.gh-issue-128661.ixx_0z.rst
new file mode 100644
index 0000000..6c52b3d
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-01-09-12-06-52.gh-issue-128661.ixx_0z.rst
@@ -0,0 +1,2 @@
+Fixes :func:`typing.evaluate_forward_ref` not showing deprecation when
+``type_params`` arg is not passed.