@@ -1778,39 +1778,60 @@ class C(metaclass=M):
17781778
17791779class 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
17891796 def test_formatannotationrelativeto (self ):
1790- import typing
1791- from test .typinganndata .ann_module9 import A
1797+ from test .typinganndata .ann_module9 import A , ann1
17921798
1793- class B : ...
1799+ # Builtin types:
1800+ self .assertEqual (
1801+ inspect .formatannotationrelativeto (object )(type ),
1802+ 'type' ,
1803+ )
17941804
1805+ # Custom types:
17951806 self .assertEqual (
17961807 inspect .formatannotationrelativeto (None )(A ),
17971808 'testModule.typing.A' ,
17981809 )
17991810 self .assertEqual (
1800- inspect .formatannotationrelativeto (inspect )(A ),
1801- 'testModule.typing. A' ,
1811+ inspect .formatannotationrelativeto (A )(A ),
1812+ 'A' ,
18021813 )
1814+
1815+ class B : ...
1816+ B .__module__ = 'testModule.typing'
1817+
18031818 self .assertEqual (
1804- inspect .formatannotationrelativeto (typing . Literal )(A ),
1805- 'testModule.typing. A' ,
1819+ inspect .formatannotationrelativeto (B )(A ),
1820+ 'A' ,
18061821 )
1822+
1823+ class C : ...
1824+ C .__module__ = 'other'
1825+
18071826 self .assertEqual (
1808- inspect .formatannotationrelativeto (B )(A ),
1827+ inspect .formatannotationrelativeto (C )(A ),
18091828 'testModule.typing.A' ,
18101829 )
1830+
1831+ # Not an instance of "type":
18111832 self .assertEqual (
1812- inspect .formatannotationrelativeto (A )(A ),
1813- 'A ' ,
1833+ inspect .formatannotationrelativeto (A )(ann1 ),
1834+ 'List[testModule.typing.A] | int ' ,
18141835 )
18151836
18161837
0 commit comments