@@ -1778,14 +1778,55 @@ 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
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
17901831class TestIsMethodDescriptor (unittest .TestCase ):
17911832
0 commit comments