Skip to content

Commit 98a2e27

Browse files
sobolevnmiss-islington
authored andcommitted
pythongh-138729: Cover inspect.formatannotationrelativeto with tests (pythonGH-138730)
(cherry picked from commit f5fa336) Co-authored-by: sobolevn <[email protected]>
1 parent 3894503 commit 98a2e27

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

17501750
class TestFormatAnnotation(unittest.TestCase):
17511751
def test_typing_replacement(self):
1752-
from test.typinganndata.ann_module9 import ann, ann1
1752+
from test.typinganndata.ann_module9 import A, ann, ann1
17531753
self.assertEqual(inspect.formatannotation(ann), 'List[str] | int')
17541754
self.assertEqual(inspect.formatannotation(ann1), 'List[testModule.typing.A] | int')
17551755

1756+
self.assertEqual(inspect.formatannotation(A, 'testModule.typing'), 'A')
1757+
self.assertEqual(inspect.formatannotation(A, 'other'), 'testModule.typing.A')
1758+
self.assertEqual(
1759+
inspect.formatannotation(ann1, 'testModule.typing'),
1760+
'List[testModule.typing.A] | int',
1761+
)
1762+
17561763
def test_forwardref(self):
17571764
fwdref = ForwardRef('fwdref')
17581765
self.assertEqual(inspect.formatannotation(fwdref), 'fwdref')
17591766

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

17611802
class TestIsMethodDescriptor(unittest.TestCase):
17621803

0 commit comments

Comments
 (0)