@@ -4791,66 +4791,185 @@ def test_no_isinstance(self):
4791
4791
issubclass (int , TypeGuard )
4792
4792
4793
4793
4794
+ SpecialAttrsP = typing .ParamSpec ('SpecialAttrsP' )
4795
+ SpecialAttrsT = typing .TypeVar ('SpecialAttrsT' , int , float , complex )
4796
+
4797
+
4794
4798
class SpecialAttrsTests (BaseTestCase ):
4799
+
4795
4800
def test_special_attrs (self ):
4796
- cls_to_check = (
4801
+ cls_to_check = {
4797
4802
# ABC classes
4798
- typing .AbstractSet ,
4799
- typing .AsyncContextManager ,
4800
- typing .AsyncGenerator ,
4801
- typing .AsyncIterable ,
4802
- typing .AsyncIterator ,
4803
- typing .Awaitable ,
4804
- typing .ByteString ,
4805
- typing .Callable ,
4806
- typing .ChainMap ,
4807
- typing .Collection ,
4808
- typing .Container ,
4809
- typing .ContextManager ,
4810
- typing .Coroutine ,
4811
- typing .Counter ,
4812
- typing .DefaultDict ,
4813
- typing .Deque ,
4814
- typing .Dict ,
4815
- typing .FrozenSet ,
4816
- typing .Generator ,
4817
- typing .Hashable ,
4818
- typing .ItemsView ,
4819
- typing .Iterable ,
4820
- typing .Iterator ,
4821
- typing .KeysView ,
4822
- typing .List ,
4823
- typing .Mapping ,
4824
- typing .MappingView ,
4825
- typing .MutableMapping ,
4826
- typing .MutableSequence ,
4827
- typing .MutableSet ,
4828
- typing .OrderedDict ,
4829
- typing .Reversible ,
4830
- typing .Sequence ,
4831
- typing .Set ,
4832
- typing .Sized ,
4833
- typing .Tuple ,
4834
- typing .Type ,
4835
- typing .ValuesView ,
4803
+ typing .AbstractSet : 'AbstractSet' ,
4804
+ typing .AsyncContextManager : 'AsyncContextManager' ,
4805
+ typing .AsyncGenerator : 'AsyncGenerator' ,
4806
+ typing .AsyncIterable : 'AsyncIterable' ,
4807
+ typing .AsyncIterator : 'AsyncIterator' ,
4808
+ typing .Awaitable : 'Awaitable' ,
4809
+ typing .ByteString : 'ByteString' ,
4810
+ typing .Callable : 'Callable' ,
4811
+ typing .ChainMap : 'ChainMap' ,
4812
+ typing .Collection : 'Collection' ,
4813
+ typing .Container : 'Container' ,
4814
+ typing .ContextManager : 'ContextManager' ,
4815
+ typing .Coroutine : 'Coroutine' ,
4816
+ typing .Counter : 'Counter' ,
4817
+ typing .DefaultDict : 'DefaultDict' ,
4818
+ typing .Deque : 'Deque' ,
4819
+ typing .Dict : 'Dict' ,
4820
+ typing .FrozenSet : 'FrozenSet' ,
4821
+ typing .Generator : 'Generator' ,
4822
+ typing .Hashable : 'Hashable' ,
4823
+ typing .ItemsView : 'ItemsView' ,
4824
+ typing .Iterable : 'Iterable' ,
4825
+ typing .Iterator : 'Iterator' ,
4826
+ typing .KeysView : 'KeysView' ,
4827
+ typing .List : 'List' ,
4828
+ typing .Mapping : 'Mapping' ,
4829
+ typing .MappingView : 'MappingView' ,
4830
+ typing .MutableMapping : 'MutableMapping' ,
4831
+ typing .MutableSequence : 'MutableSequence' ,
4832
+ typing .MutableSet : 'MutableSet' ,
4833
+ typing .OrderedDict : 'OrderedDict' ,
4834
+ typing .Reversible : 'Reversible' ,
4835
+ typing .Sequence : 'Sequence' ,
4836
+ typing .Set : 'Set' ,
4837
+ typing .Sized : 'Sized' ,
4838
+ typing .Tuple : 'Tuple' ,
4839
+ typing .Type : 'Type' ,
4840
+ typing .ValuesView : 'ValuesView' ,
4841
+ # Subscribed ABC classes
4842
+ typing .AbstractSet [Any ]: 'AbstractSet' ,
4843
+ typing .AsyncContextManager [Any ]: 'AsyncContextManager' ,
4844
+ typing .AsyncGenerator [Any , Any ]: 'AsyncGenerator' ,
4845
+ typing .AsyncIterable [Any ]: 'AsyncIterable' ,
4846
+ typing .AsyncIterator [Any ]: 'AsyncIterator' ,
4847
+ typing .Awaitable [Any ]: 'Awaitable' ,
4848
+ typing .Callable [[], Any ]: 'Callable' ,
4849
+ typing .Callable [..., Any ]: 'Callable' ,
4850
+ typing .ChainMap [Any , Any ]: 'ChainMap' ,
4851
+ typing .Collection [Any ]: 'Collection' ,
4852
+ typing .Container [Any ]: 'Container' ,
4853
+ typing .ContextManager [Any ]: 'ContextManager' ,
4854
+ typing .Coroutine [Any , Any , Any ]: 'Coroutine' ,
4855
+ typing .Counter [Any ]: 'Counter' ,
4856
+ typing .DefaultDict [Any , Any ]: 'DefaultDict' ,
4857
+ typing .Deque [Any ]: 'Deque' ,
4858
+ typing .Dict [Any , Any ]: 'Dict' ,
4859
+ typing .FrozenSet [Any ]: 'FrozenSet' ,
4860
+ typing .Generator [Any , Any , Any ]: 'Generator' ,
4861
+ typing .ItemsView [Any , Any ]: 'ItemsView' ,
4862
+ typing .Iterable [Any ]: 'Iterable' ,
4863
+ typing .Iterator [Any ]: 'Iterator' ,
4864
+ typing .KeysView [Any ]: 'KeysView' ,
4865
+ typing .List [Any ]: 'List' ,
4866
+ typing .Mapping [Any , Any ]: 'Mapping' ,
4867
+ typing .MappingView [Any ]: 'MappingView' ,
4868
+ typing .MutableMapping [Any , Any ]: 'MutableMapping' ,
4869
+ typing .MutableSequence [Any ]: 'MutableSequence' ,
4870
+ typing .MutableSet [Any ]: 'MutableSet' ,
4871
+ typing .OrderedDict [Any , Any ]: 'OrderedDict' ,
4872
+ typing .Reversible [Any ]: 'Reversible' ,
4873
+ typing .Sequence [Any ]: 'Sequence' ,
4874
+ typing .Set [Any ]: 'Set' ,
4875
+ typing .Tuple [Any ]: 'Tuple' ,
4876
+ typing .Tuple [Any , ...]: 'Tuple' ,
4877
+ typing .Type [Any ]: 'Type' ,
4878
+ typing .ValuesView [Any ]: 'ValuesView' ,
4836
4879
# Special Forms
4837
- typing .Any ,
4838
- typing .NoReturn ,
4839
- typing .ClassVar ,
4840
- typing .Final ,
4841
- typing .Union ,
4842
- typing .Optional ,
4843
- typing .Literal ,
4844
- typing .TypeAlias ,
4845
- typing .Concatenate ,
4846
- typing .TypeGuard ,
4847
- )
4880
+ typing .Annotated : 'Annotated' ,
4881
+ typing .Any : 'Any' ,
4882
+ typing .ClassVar : 'ClassVar' ,
4883
+ typing .Concatenate : 'Concatenate' ,
4884
+ typing .Final : 'Final' ,
4885
+ typing .ForwardRef : 'ForwardRef' ,
4886
+ typing .Literal : 'Literal' ,
4887
+ typing .NewType : 'NewType' ,
4888
+ typing .NoReturn : 'NoReturn' ,
4889
+ typing .Optional : 'Optional' ,
4890
+ typing .TypeAlias : 'TypeAlias' ,
4891
+ typing .TypeGuard : 'TypeGuard' ,
4892
+ typing .TypeVar : 'TypeVar' ,
4893
+ typing .Union : 'Union' ,
4894
+ # Subscribed special forms
4895
+ typing .Annotated [Any , "Annotation" ]: 'Annotated' ,
4896
+ typing .ClassVar [Any ]: 'ClassVar' ,
4897
+ typing .Concatenate [Any , SpecialAttrsP ]: 'Concatenate' ,
4898
+ typing .Final [Any ]: 'Final' ,
4899
+ typing .Literal [Any ]: 'Literal' ,
4900
+ typing .Optional [Any ]: 'Optional' ,
4901
+ typing .TypeGuard [Any ]: 'TypeGuard' ,
4902
+ typing .Union [Any ]: 'Any' ,
4903
+ typing .Union [int , float ]: 'Union' ,
4904
+ # Incompatible special forms (tested in test_special_attrs2)
4905
+ # - typing.ForwardRef('set[Any]')
4906
+ # - typing.NewType('TypeName', Any)
4907
+ # - typing.ParamSpec('SpecialAttrsP')
4908
+ # - typing.TypeVar('T')
4909
+ }
4848
4910
4849
- for cls in cls_to_check :
4911
+ for cls , name in cls_to_check . items () :
4850
4912
with self .subTest (cls = cls ):
4851
- self .assertEqual (cls .__name__ , cls ._name )
4852
- self .assertEqual (cls .__qualname__ , cls ._name )
4853
- self .assertEqual (cls .__module__ , 'typing' )
4913
+ self .assertEqual (cls .__name__ , name , str (cls ))
4914
+ self .assertEqual (cls .__qualname__ , name , str (cls ))
4915
+ self .assertEqual (cls .__module__ , 'typing' , str (cls ))
4916
+ self .assertEqual (getattr (cls , '_name' , name ), name , str (cls ))
4917
+ for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
4918
+ s = pickle .dumps (cls , proto )
4919
+ loaded = pickle .loads (s )
4920
+ self .assertIs (cls , loaded )
4921
+
4922
+ TypeName = typing .NewType ('SpecialAttrsTests.TypeName' , Any )
4923
+
4924
+ def test_special_attrs2 (self ):
4925
+ # Forward refs provide a different introspection API. __name__ and
4926
+ # __qualname__ make little sense for forward refs as they can store
4927
+ # complex typing expressions.
4928
+ fr = typing .ForwardRef ('set[Any]' )
4929
+ self .assertFalse (hasattr (fr , '__name__' ))
4930
+ self .assertFalse (hasattr (fr , '__qualname__' ))
4931
+ self .assertEqual (fr .__module__ , 'typing' )
4932
+ # Forward refs are currently unpicklable.
4933
+ for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
4934
+ with self .assertRaises (TypeError ) as exc :
4935
+ pickle .dumps (fr , proto )
4936
+
4937
+ self .assertEqual (SpecialAttrsTests .TypeName .__name__ , 'TypeName' )
4938
+ self .assertEqual (
4939
+ SpecialAttrsTests .TypeName .__qualname__ ,
4940
+ 'SpecialAttrsTests.TypeName' ,
4941
+ )
4942
+ self .assertEqual (
4943
+ SpecialAttrsTests .TypeName .__module__ ,
4944
+ 'test.test_typing' ,
4945
+ )
4946
+ # NewTypes are picklable assuming correct qualname information.
4947
+ for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
4948
+ s = pickle .dumps (SpecialAttrsTests .TypeName , proto )
4949
+ loaded = pickle .loads (s )
4950
+ self .assertIs (SpecialAttrsTests .TypeName , loaded )
4951
+
4952
+ # Type variables don't support non-global instantiation per PEP 484
4953
+ # restriction that "The argument to TypeVar() must be a string equal
4954
+ # to the variable name to which it is assigned". Thus, providing
4955
+ # __qualname__ is unnecessary.
4956
+ self .assertEqual (SpecialAttrsT .__name__ , 'SpecialAttrsT' )
4957
+ self .assertFalse (hasattr (SpecialAttrsT , '__qualname__' ))
4958
+ self .assertEqual (SpecialAttrsT .__module__ , 'test.test_typing' )
4959
+ # Module-level type variables are picklable.
4960
+ for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
4961
+ s = pickle .dumps (SpecialAttrsT , proto )
4962
+ loaded = pickle .loads (s )
4963
+ self .assertIs (SpecialAttrsT , loaded )
4964
+
4965
+ self .assertEqual (SpecialAttrsP .__name__ , 'SpecialAttrsP' )
4966
+ self .assertFalse (hasattr (SpecialAttrsP , '__qualname__' ))
4967
+ self .assertEqual (SpecialAttrsP .__module__ , 'test.test_typing' )
4968
+ # Module-level ParamSpecs are picklable.
4969
+ for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
4970
+ s = pickle .dumps (SpecialAttrsP , proto )
4971
+ loaded = pickle .loads (s )
4972
+ self .assertIs (SpecialAttrsP , loaded )
4854
4973
4855
4974
class AllTests (BaseTestCase ):
4856
4975
"""Tests for __all__."""
0 commit comments