aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_annotationlib.py
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2024-09-25 17:01:09 -0700
committerGitHub <noreply@github.com>2024-09-26 00:01:09 +0000
commit4e829c0e6fbd47818451660c234eacc42a0b4a08 (patch)
tree20f181a657ba4a3e65fe90c5971d04105d4bf9b5 /Lib/test/test_annotationlib.py
parent0268b072d84bc4be890d1b7459815ba1cb9f9945 (diff)
downloadcpython-4e829c0e6fbd47818451660c234eacc42a0b4a08.tar.gz
cpython-4e829c0e6fbd47818451660c234eacc42a0b4a08.zip
gh-124412: Add helpers for converting annotations to source format (#124551)
Diffstat (limited to 'Lib/test/test_annotationlib.py')
-rw-r--r--Lib/test/test_annotationlib.py45
1 files changed, 41 insertions, 4 deletions
diff --git a/Lib/test/test_annotationlib.py b/Lib/test/test_annotationlib.py
index 5b052dab500..dc1106aee1e 100644
--- a/Lib/test/test_annotationlib.py
+++ b/Lib/test/test_annotationlib.py
@@ -7,7 +7,14 @@ import functools
import itertools
import pickle
import unittest
-from annotationlib import Format, ForwardRef, get_annotations, get_annotate_function
+from annotationlib import (
+ Format,
+ ForwardRef,
+ get_annotations,
+ get_annotate_function,
+ annotations_to_source,
+ value_to_source,
+)
from typing import Unpack
from test import support
@@ -25,6 +32,11 @@ def times_three(fn):
return wrapper
+class MyClass:
+ def __repr__(self):
+ return "my repr"
+
+
class TestFormat(unittest.TestCase):
def test_enum(self):
self.assertEqual(annotationlib.Format.VALUE.value, 1)
@@ -324,7 +336,10 @@ class TestForwardRefClass(unittest.TestCase):
# namespaces without going through eval()
self.assertIs(ForwardRef("int").evaluate(), int)
self.assertIs(ForwardRef("int").evaluate(locals={"int": str}), str)
- self.assertIs(ForwardRef("int").evaluate(locals={"int": float}, globals={"int": str}), float)
+ self.assertIs(
+ ForwardRef("int").evaluate(locals={"int": float}, globals={"int": str}),
+ float,
+ )
self.assertIs(ForwardRef("int").evaluate(globals={"int": str}), str)
with support.swap_attr(builtins, "int", dict):
self.assertIs(ForwardRef("int").evaluate(), dict)
@@ -788,9 +803,8 @@ class TestGetAnnotations(unittest.TestCase):
annotationlib.get_annotations(ha, format=Format.FORWARDREF), {"x": int}
)
- # TODO(gh-124412): This should return {'x': 'int'} instead.
self.assertEqual(
- annotationlib.get_annotations(ha, format=Format.SOURCE), {"x": int}
+ annotationlib.get_annotations(ha, format=Format.SOURCE), {"x": "int"}
)
def test_raising_annotations_on_custom_object(self):
@@ -1078,6 +1092,29 @@ class TestGetAnnotateFunction(unittest.TestCase):
self.assertEqual(get_annotate_function(C)(Format.VALUE), {"a": int})
+class TestToSource(unittest.TestCase):
+ def test_value_to_source(self):
+ self.assertEqual(value_to_source(int), "int")
+ self.assertEqual(value_to_source(MyClass), "test.test_annotationlib.MyClass")
+ self.assertEqual(value_to_source(len), "len")
+ self.assertEqual(value_to_source(value_to_source), "value_to_source")
+ self.assertEqual(value_to_source(times_three), "times_three")
+ self.assertEqual(value_to_source(...), "...")
+ self.assertEqual(value_to_source(None), "None")
+ self.assertEqual(value_to_source(1), "1")
+ self.assertEqual(value_to_source("1"), "'1'")
+ self.assertEqual(value_to_source(Format.VALUE), repr(Format.VALUE))
+ self.assertEqual(value_to_source(MyClass()), "my repr")
+
+ def test_annotations_to_source(self):
+ self.assertEqual(annotations_to_source({}), {})
+ self.assertEqual(annotations_to_source({"x": int}), {"x": "int"})
+ self.assertEqual(annotations_to_source({"x": "int"}), {"x": "int"})
+ self.assertEqual(
+ annotations_to_source({"x": int, "y": str}), {"x": "int", "y": "str"}
+ )
+
+
class TestAnnotationLib(unittest.TestCase):
def test__all__(self):
support.check__all__(self, annotationlib)