Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1778,14 +1778,55 @@ 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):
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),
'List[testModule.typing.A] | int',
)


class TestIsMethodDescriptor(unittest.TestCase):

Expand Down
Loading