summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2024-12-30 16:19:38 (GMT)
committerGitHub <noreply@github.com>2024-12-30 16:19:38 (GMT)
commit348012432155271cfbee71a78d0c27254fc230e2 (patch)
tree387b0044e245218cc739a65461dc09d23cab2875 /Lib
parent2bd5a7ab0f4a1f65ab8043001bd6e8416c5079bd (diff)
downloadcpython-348012432155271cfbee71a78d0c27254fc230e2.zip
cpython-348012432155271cfbee71a78d0c27254fc230e2.tar.gz
cpython-348012432155271cfbee71a78d0c27254fc230e2.tar.bz2
gh-119180: Set the name of the param to __annotate__ to "format" (#124730)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_pydoc/test_pydoc.py4
-rw-r--r--Lib/test/test_type_annotations.py49
2 files changed, 51 insertions, 2 deletions
diff --git a/Lib/test/test_pydoc/test_pydoc.py b/Lib/test/test_pydoc/test_pydoc.py
index 3283fde..c798b11 100644
--- a/Lib/test/test_pydoc/test_pydoc.py
+++ b/Lib/test/test_pydoc/test_pydoc.py
@@ -79,7 +79,7 @@ CLASSES
class B(builtins.object)
| Methods defined here:
|
- | __annotate__(...)
+ | __annotate__(format, /)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
@@ -180,7 +180,7 @@ class A(builtins.object)
class B(builtins.object)
Methods defined here:
- __annotate__(...)
+ __annotate__(format, /)
----------------------------------------------------------------------
Data descriptors defined here:
__dict__
diff --git a/Lib/test/test_type_annotations.py b/Lib/test/test_type_annotations.py
index 7d88f4c..0afcd76 100644
--- a/Lib/test/test_type_annotations.py
+++ b/Lib/test/test_type_annotations.py
@@ -1,4 +1,5 @@
import annotationlib
+import inspect
import textwrap
import types
import unittest
@@ -380,6 +381,11 @@ class DeferredEvaluationTests(unittest.TestCase):
annotate(None)
self.assertEqual(annotate(annotationlib.Format.VALUE), {"x": int})
+ sig = inspect.signature(annotate)
+ self.assertEqual(sig, inspect.Signature([
+ inspect.Parameter("format", inspect.Parameter.POSITIONAL_ONLY)
+ ]))
+
def test_comprehension_in_annotation(self):
# This crashed in an earlier version of the code
ns = run_code("x: [y for y in range(10)]")
@@ -400,6 +406,7 @@ class DeferredEvaluationTests(unittest.TestCase):
def test_name_clash_with_format(self):
# this test would fail if __annotate__'s parameter was called "format"
+ # during symbol table construction
code = """
class format: pass
@@ -408,3 +415,45 @@ class DeferredEvaluationTests(unittest.TestCase):
ns = run_code(code)
f = ns["f"]
self.assertEqual(f.__annotations__, {"x": ns["format"]})
+
+ code = """
+ class Outer:
+ class format: pass
+
+ def meth(self, x: format): ...
+ """
+ ns = run_code(code)
+ self.assertEqual(ns["Outer"].meth.__annotations__, {"x": ns["Outer"].format})
+
+ code = """
+ def f(format):
+ def inner(x: format): pass
+ return inner
+ res = f("closure var")
+ """
+ ns = run_code(code)
+ self.assertEqual(ns["res"].__annotations__, {"x": "closure var"})
+
+ code = """
+ def f(x: format):
+ pass
+ """
+ ns = run_code(code)
+ # picks up the format() builtin
+ self.assertEqual(ns["f"].__annotations__, {"x": format})
+
+ code = """
+ def outer():
+ def f(x: format):
+ pass
+ if False:
+ class format: pass
+ return f
+ f = outer()
+ """
+ ns = run_code(code)
+ with self.assertRaisesRegex(
+ NameError,
+ "cannot access free variable 'format' where it is not associated with a value in enclosing scope",
+ ):
+ ns["f"].__annotations__