]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-138729: Cover `inspect.formatannotationrelativeto` with tests (GH-138730...
authorsobolevn <mail@sobolevn.me>
Wed, 10 Sep 2025 14:42:49 +0000 (17:42 +0300)
committerGitHub <noreply@github.com>
Wed, 10 Sep 2025 14:42:49 +0000 (14:42 +0000)
(cherry picked from commit f5fa336579d124e389b1c36a5431e7908aaf5e53)

Lib/test/test_inspect/test_inspect.py

index f7f73b25b8cb5fa6b2c94dde0d6051d5f279dace..3e73dc5fe2694c4a43e67561684c3e1d3a5b31eb 100644 (file)
@@ -1986,10 +1986,51 @@ class TestClassesAndFunctions(unittest.TestCase):
 
 class TestFormatAnnotation(unittest.TestCase):
     def test_typing_replacement(self):
-        from test.typinganndata.ann_module9 import ann, ann1
+        from test.typinganndata.ann_module9 import A, ann, ann1
         self.assertEqual(inspect.formatannotation(ann), 'Union[List[str], int]')
         self.assertEqual(inspect.formatannotation(ann1), 'Union[List[testModule.typing.A], int]')
 
+        self.assertEqual(inspect.formatannotation(A, 'testModule.typing'), 'A')
+        self.assertEqual(inspect.formatannotation(A, 'other'), 'testModule.typing.A')
+        self.assertEqual(
+            inspect.formatannotation(ann1, 'testModule.typing'),
+            'Union[List[testModule.typing.A], int]',
+        )
+
+    def test_formatannotationrelativeto(self):
+        from test.typinganndata.ann_module9 import A, ann1
+
+        # Builtin types:
+        self.assertEqual(
+            inspect.formatannotationrelativeto(object)(type),
+            'type',
+        )
+
+        # Custom types:
+        self.assertEqual(
+            inspect.formatannotationrelativeto(None)(A),
+            'testModule.typing.A',
+        )
+
+        class B: ...
+        B.__module__ = 'testModule.typing'
+
+        self.assertEqual(
+            inspect.formatannotationrelativeto(B)(A),
+            'A',
+        )
+
+        self.assertEqual(
+            inspect.formatannotationrelativeto(object)(A),
+            'testModule.typing.A',
+        )
+
+        # Not an instance of "type":
+        self.assertEqual(
+            inspect.formatannotationrelativeto(A)(ann1),
+            'Union[List[testModule.typing.A], int]',
+        )
+
 
 class TestIsMethodDescriptor(unittest.TestCase):