@@ -1778,14 +1778,55 @@ class C(metaclass=M):
1778
1778
1779
1779
class TestFormatAnnotation (unittest .TestCase ):
1780
1780
def test_typing_replacement (self ):
1781
- from test .typinganndata .ann_module9 import ann , ann1
1781
+ from test .typinganndata .ann_module9 import A , ann , ann1
1782
1782
self .assertEqual (inspect .formatannotation (ann ), 'List[str] | int' )
1783
1783
self .assertEqual (inspect .formatannotation (ann1 ), 'List[testModule.typing.A] | int' )
1784
1784
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
+
1785
1792
def test_forwardref (self ):
1786
1793
fwdref = ForwardRef ('fwdref' )
1787
1794
self .assertEqual (inspect .formatannotation (fwdref ), 'fwdref' )
1788
1795
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
+
1789
1830
1790
1831
class TestIsMethodDescriptor (unittest .TestCase ):
1791
1832
0 commit comments