@@ -1986,10 +1986,51 @@ def test_pep_695_generics_with_future_annotations_nested_in_function(self):
19861986
19871987class 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
19942035class TestIsMethodDescriptor (unittest .TestCase ):
19952036
0 commit comments