Skip to content

Commit 63bd8cf

Browse files
[3.14] gh-138729: Cover inspect.formatannotationrelativeto with tests (GH-138730) (GH-138747)
(cherry picked from commit f5fa336) Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent dfdda53 commit 63bd8cf

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

Lib/test/test_inspect/test_inspect.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1755,14 +1755,55 @@ class C(metaclass=M):
17551755

17561756
class TestFormatAnnotation(unittest.TestCase):
17571757
def test_typing_replacement(self):
1758-
from test.typinganndata.ann_module9 import ann, ann1
1758+
from test.typinganndata.ann_module9 import A, ann, ann1
17591759
self.assertEqual(inspect.formatannotation(ann), 'List[str] | int')
17601760
self.assertEqual(inspect.formatannotation(ann1), 'List[testModule.typing.A] | int')
17611761

1762+
self.assertEqual(inspect.formatannotation(A, 'testModule.typing'), 'A')
1763+
self.assertEqual(inspect.formatannotation(A, 'other'), 'testModule.typing.A')
1764+
self.assertEqual(
1765+
inspect.formatannotation(ann1, 'testModule.typing'),
1766+
'List[testModule.typing.A] | int',
1767+
)
1768+
17621769
def test_forwardref(self):
17631770
fwdref = ForwardRef('fwdref')
17641771
self.assertEqual(inspect.formatannotation(fwdref), 'fwdref')
17651772

1773+
def test_formatannotationrelativeto(self):
1774+
from test.typinganndata.ann_module9 import A, ann1
1775+
1776+
# Builtin types:
1777+
self.assertEqual(
1778+
inspect.formatannotationrelativeto(object)(type),
1779+
'type',
1780+
)
1781+
1782+
# Custom types:
1783+
self.assertEqual(
1784+
inspect.formatannotationrelativeto(None)(A),
1785+
'testModule.typing.A',
1786+
)
1787+
1788+
class B: ...
1789+
B.__module__ = 'testModule.typing'
1790+
1791+
self.assertEqual(
1792+
inspect.formatannotationrelativeto(B)(A),
1793+
'A',
1794+
)
1795+
1796+
self.assertEqual(
1797+
inspect.formatannotationrelativeto(object)(A),
1798+
'testModule.typing.A',
1799+
)
1800+
1801+
# Not an instance of "type":
1802+
self.assertEqual(
1803+
inspect.formatannotationrelativeto(A)(ann1),
1804+
'List[testModule.typing.A] | int',
1805+
)
1806+
17661807

17671808
class TestIsMethodDescriptor(unittest.TestCase):
17681809

0 commit comments

Comments
 (0)