Skip to content

Commit 2422e81

Browse files
authored
[3.13] gh-138729: Cover inspect.formatannotationrelativeto with tests (GH-138730) (#138748)
(cherry picked from commit f5fa336)
1 parent 7d72470 commit 2422e81

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
@@ -1986,10 +1986,51 @@ def test_pep_695_generics_with_future_annotations_nested_in_function(self):
19861986

19871987
class TestFormatAnnotation(unittest.TestCase):
19881988
def test_typing_replacement(self):
1989-
from test.typinganndata.ann_module9 import ann, ann1
1989+
from test.typinganndata.ann_module9 import A, ann, ann1
19901990
self.assertEqual(inspect.formatannotation(ann), 'Union[List[str], int]')
19911991
self.assertEqual(inspect.formatannotation(ann1), 'Union[List[testModule.typing.A], int]')
19921992

1993+
self.assertEqual(inspect.formatannotation(A, 'testModule.typing'), 'A')
1994+
self.assertEqual(inspect.formatannotation(A, 'other'), 'testModule.typing.A')
1995+
self.assertEqual(
1996+
inspect.formatannotation(ann1, 'testModule.typing'),
1997+
'Union[List[testModule.typing.A], int]',
1998+
)
1999+
2000+
def test_formatannotationrelativeto(self):
2001+
from test.typinganndata.ann_module9 import A, ann1
2002+
2003+
# Builtin types:
2004+
self.assertEqual(
2005+
inspect.formatannotationrelativeto(object)(type),
2006+
'type',
2007+
)
2008+
2009+
# Custom types:
2010+
self.assertEqual(
2011+
inspect.formatannotationrelativeto(None)(A),
2012+
'testModule.typing.A',
2013+
)
2014+
2015+
class B: ...
2016+
B.__module__ = 'testModule.typing'
2017+
2018+
self.assertEqual(
2019+
inspect.formatannotationrelativeto(B)(A),
2020+
'A',
2021+
)
2022+
2023+
self.assertEqual(
2024+
inspect.formatannotationrelativeto(object)(A),
2025+
'testModule.typing.A',
2026+
)
2027+
2028+
# Not an instance of "type":
2029+
self.assertEqual(
2030+
inspect.formatannotationrelativeto(A)(ann1),
2031+
'Union[List[testModule.typing.A], int]',
2032+
)
2033+
19932034

19942035
class TestIsMethodDescriptor(unittest.TestCase):
19952036

0 commit comments

Comments
 (0)