]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-128661: Fix `typing.evaluate_forward_ref` not showing deprecation (#128663)
authorsobolevn <mail@sobolevn.me>
Thu, 9 Jan 2025 15:15:13 +0000 (18:15 +0300)
committerGitHub <noreply@github.com>
Thu, 9 Jan 2025 15:15:13 +0000 (18:15 +0300)
gh-128661: Fix `typing.evaluate_forward_ref` not showing deprecataion

Lib/test/test_typing.py
Lib/typing.py
Misc/NEWS.d/next/Library/2025-01-09-12-06-52.gh-issue-128661.ixx_0z.rst [new file with mode: 0644]

index 1c86b95e8e5c29540e8f511bd82296ee163197d5..c51ee763890af25d50434f158ff2a1651f61a999 100644 (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):
index e69b485422cbd2a64fb4470dfd28b8a4df443371..66570db7a5bd74000ebb1458adae3a9a94a62293 100644 (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(),
 ):
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 (file)
index 0000000..6c52b3d
--- /dev/null
@@ -0,0 +1,2 @@
+Fixes :func:`typing.evaluate_forward_ref` not showing deprecation when
+``type_params`` arg is not passed.