From 370dfd9992960ee78239597007e23d2a46e414e3 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 10 Sep 2025 10:38:24 +0300 Subject: [PATCH 1/3] gh-138729: Cover `inspect.formatannotationrelativeto` with tests --- Lib/test/test_inspect/test_inspect.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index 88fa4b7460c412..107e370e48fd4a 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -1786,6 +1786,33 @@ def test_forwardref(self): fwdref = ForwardRef('fwdref') self.assertEqual(inspect.formatannotation(fwdref), 'fwdref') + def test_formatannotationrelativeto(self): + import typing + from test.typinganndata.ann_module9 import A + + class B: ... + + self.assertEqual( + inspect.formatannotationrelativeto(None)(A), + 'testModule.typing.A', + ) + self.assertEqual( + inspect.formatannotationrelativeto(inspect)(A), + 'testModule.typing.A', + ) + self.assertEqual( + inspect.formatannotationrelativeto(typing.Literal)(A), + 'testModule.typing.A', + ) + self.assertEqual( + inspect.formatannotationrelativeto(B)(A), + 'testModule.typing.A', + ) + self.assertEqual( + inspect.formatannotationrelativeto(A)(A), + 'A', + ) + class TestIsMethodDescriptor(unittest.TestCase): From a7932f7eb96935451d908fbe00e39bad65356329 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 10 Sep 2025 12:56:16 +0300 Subject: [PATCH 2/3] Address review --- Lib/test/test_inspect/test_inspect.py | 43 ++++++++++++++++++++------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index 107e370e48fd4a..a7e1efb4b0ef3c 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -1778,39 +1778,60 @@ class C(metaclass=M): 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), 'List[str] | int') self.assertEqual(inspect.formatannotation(ann1), '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'), + 'List[testModule.typing.A] | int', + ) + def test_forwardref(self): fwdref = ForwardRef('fwdref') self.assertEqual(inspect.formatannotation(fwdref), 'fwdref') def test_formatannotationrelativeto(self): - import typing - from test.typinganndata.ann_module9 import A + from test.typinganndata.ann_module9 import A, ann1 - class B: ... + # Builtin types: + self.assertEqual( + inspect.formatannotationrelativeto(object)(type), + 'type', + ) + # Custom types: self.assertEqual( inspect.formatannotationrelativeto(None)(A), 'testModule.typing.A', ) self.assertEqual( - inspect.formatannotationrelativeto(inspect)(A), - 'testModule.typing.A', + inspect.formatannotationrelativeto(A)(A), + 'A', ) + + class B: ... + B.__module__ = 'testModule.typing' + self.assertEqual( - inspect.formatannotationrelativeto(typing.Literal)(A), - 'testModule.typing.A', + inspect.formatannotationrelativeto(B)(A), + 'A', ) + + class C: ... + C.__module__ = 'other' + self.assertEqual( - inspect.formatannotationrelativeto(B)(A), + inspect.formatannotationrelativeto(C)(A), 'testModule.typing.A', ) + + # Not an instance of "type": self.assertEqual( - inspect.formatannotationrelativeto(A)(A), - 'A', + inspect.formatannotationrelativeto(A)(ann1), + 'List[testModule.typing.A] | int', ) From b97ddef978bab0f6c84964115f33f90aa2e6a5ba Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 10 Sep 2025 14:46:52 +0300 Subject: [PATCH 3/3] Address review --- Lib/test/test_inspect/test_inspect.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index a7e1efb4b0ef3c..555efb78dcc6aa 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -1807,10 +1807,6 @@ def test_formatannotationrelativeto(self): inspect.formatannotationrelativeto(None)(A), 'testModule.typing.A', ) - self.assertEqual( - inspect.formatannotationrelativeto(A)(A), - 'A', - ) class B: ... B.__module__ = 'testModule.typing' @@ -1820,11 +1816,8 @@ class B: ... 'A', ) - class C: ... - C.__module__ = 'other' - self.assertEqual( - inspect.formatannotationrelativeto(C)(A), + inspect.formatannotationrelativeto(object)(A), 'testModule.typing.A', )