Skip to content

Commit f5fa336

Browse files
authored
gh-138729: Cover inspect.formatannotationrelativeto with tests (#138730)
1 parent cf8f36f commit f5fa336

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
@@ -1778,14 +1778,55 @@ class C(metaclass=M):
17781778

17791779
class TestFormatAnnotation(unittest.TestCase):
17801780
def test_typing_replacement(self):
1781-
from test.typinganndata.ann_module9 import ann, ann1
1781+
from test.typinganndata.ann_module9 import A, ann, ann1
17821782
self.assertEqual(inspect.formatannotation(ann), 'List[str] | int')
17831783
self.assertEqual(inspect.formatannotation(ann1), 'List[testModule.typing.A] | int')
17841784

1785+
self.assertEqual(inspect.formatannotation(A, 'testModule.typing'), 'A')
1786+
self.assertEqual(inspect.formatannotation(A, 'other'), 'testModule.typing.A')
1787+
self.assertEqual(
1788+
inspect.formatannotation(ann1, 'testModule.typing'),
1789+
'List[testModule.typing.A] | int',
1790+
)
1791+
17851792
def test_forwardref(self):
17861793
fwdref = ForwardRef('fwdref')
17871794
self.assertEqual(inspect.formatannotation(fwdref), 'fwdref')
17881795

1796+
def test_formatannotationrelativeto(self):
1797+
from test.typinganndata.ann_module9 import A, ann1
1798+
1799+
# Builtin types:
1800+
self.assertEqual(
1801+
inspect.formatannotationrelativeto(object)(type),
1802+
'type',
1803+
)
1804+
1805+
# Custom types:
1806+
self.assertEqual(
1807+
inspect.formatannotationrelativeto(None)(A),
1808+
'testModule.typing.A',
1809+
)
1810+
1811+
class B: ...
1812+
B.__module__ = 'testModule.typing'
1813+
1814+
self.assertEqual(
1815+
inspect.formatannotationrelativeto(B)(A),
1816+
'A',
1817+
)
1818+
1819+
self.assertEqual(
1820+
inspect.formatannotationrelativeto(object)(A),
1821+
'testModule.typing.A',
1822+
)
1823+
1824+
# Not an instance of "type":
1825+
self.assertEqual(
1826+
inspect.formatannotationrelativeto(A)(ann1),
1827+
'List[testModule.typing.A] | int',
1828+
)
1829+
17891830

17901831
class TestIsMethodDescriptor(unittest.TestCase):
17911832

0 commit comments

Comments
 (0)