1
- import asyncio
2
1
import pickle
3
2
import re
4
3
import sys
5
- from unittest import TestCase , main
4
+ from unittest import TestCase , main , skipUnless
6
5
7
6
from typing import Any
8
7
from typing import TypeVar , AnyStr
@@ -133,6 +132,7 @@ def test_basic_constrained(self):
133
132
def test_constrained_error (self ):
134
133
with self .assertRaises (TypeError ):
135
134
X = TypeVar ('X' , int )
135
+ X
136
136
137
137
def test_union_unique (self ):
138
138
X = TypeVar ('X' )
@@ -317,6 +317,7 @@ def test_union_instance_type_error(self):
317
317
def test_union_str_pattern (self ):
318
318
# Shouldn't crash; see http://bugs.python.org/issue25390
319
319
A = Union [str , Pattern ]
320
+ A
320
321
321
322
322
323
class TypeVarUnionTests (TestCase ):
@@ -487,7 +488,7 @@ def get(self, key: XK, default: XV = None) -> XV:
487
488
...
488
489
489
490
490
- class MySimpleMapping (SimpleMapping ):
491
+ class MySimpleMapping (SimpleMapping [ XK , XV ] ):
491
492
492
493
def __init__ (self ):
493
494
self .store = {}
@@ -541,6 +542,7 @@ def test_supports_abs(self):
541
542
assert not issubclass (str , typing .SupportsAbs )
542
543
543
544
def test_supports_round (self ):
545
+ issubclass (float , typing .SupportsRound )
544
546
assert issubclass (float , typing .SupportsRound )
545
547
assert issubclass (int , typing .SupportsRound )
546
548
assert not issubclass (str , typing .SupportsRound )
@@ -551,20 +553,23 @@ def test_reversible(self):
551
553
552
554
def test_protocol_instance_type_error (self ):
553
555
with self .assertRaises (TypeError ):
554
- isinstance ([] , typing .Reversible )
556
+ isinstance (0 , typing .SupportsAbs )
555
557
556
558
557
559
class GenericTests (TestCase ):
558
560
559
561
def test_basics (self ):
560
562
X = SimpleMapping [str , Any ]
561
- Y = SimpleMapping [XK , str ]
562
- X [str , str ]
563
- Y [str , str ]
563
+ assert X .__parameters__ == ()
564
564
with self .assertRaises (TypeError ):
565
- X [int , str ]
565
+ X [str ]
566
+ with self .assertRaises (TypeError ):
567
+ X [str , str ]
568
+ Y = SimpleMapping [XK , str ]
569
+ assert Y .__parameters__ == (XK ,)
570
+ Y [str ]
566
571
with self .assertRaises (TypeError ):
567
- Y [str , bytes ]
572
+ Y [str , str ]
568
573
569
574
def test_init (self ):
570
575
T = TypeVar ('T' )
@@ -576,30 +581,61 @@ def test_init(self):
576
581
577
582
def test_repr (self ):
578
583
self .assertEqual (repr (SimpleMapping ),
579
- __name__ + '.' + 'SimpleMapping[ ~XK, ~XV] ' )
584
+ __name__ + '.' + 'SimpleMapping< ~XK, ~XV> ' )
580
585
self .assertEqual (repr (MySimpleMapping ),
581
- __name__ + '.' + 'MySimpleMapping[~XK, ~XV]' )
586
+ __name__ + '.' + 'MySimpleMapping<~XK, ~XV>' )
587
+
588
+ def test_chain_repr (self ):
589
+ T = TypeVar ('T' )
590
+ S = TypeVar ('S' )
591
+
592
+ class C (Generic [T ]):
593
+ pass
594
+
595
+ X = C [Tuple [S , T ]]
596
+ assert X == C [Tuple [S , T ]]
597
+ assert X != C [Tuple [T , S ]]
598
+
599
+ Y = X [T , int ]
600
+ assert Y == X [T , int ]
601
+ assert Y != X [S , int ]
602
+ assert Y != X [T , str ]
603
+
604
+ Z = Y [str ]
605
+ assert Z == Y [str ]
606
+ assert Z != Y [int ]
607
+ assert Z != Y [T ]
608
+
609
+ assert str (Z ).endswith (
610
+ '.C<~T>[typing.Tuple[~S, ~T]]<~S, ~T>[~T, int]<~T>[str]' )
582
611
583
612
def test_dict (self ):
584
613
T = TypeVar ('T' )
614
+
585
615
class B (Generic [T ]):
586
616
pass
617
+
587
618
b = B ()
588
619
b .foo = 42
589
620
self .assertEqual (b .__dict__ , {'foo' : 42 })
621
+
590
622
class C (B [int ]):
591
623
pass
624
+
592
625
c = C ()
593
626
c .bar = 'abc'
594
627
self .assertEqual (c .__dict__ , {'bar' : 'abc' })
595
628
596
629
def test_pickle (self ):
630
+ global C # pickle wants to reference the class by name
597
631
T = TypeVar ('T' )
632
+
598
633
class B (Generic [T ]):
599
634
pass
600
- global C # pickle wants to reference the class by name
635
+
601
636
class C (B [int ]):
602
637
pass
638
+
603
639
c = C ()
604
640
c .foo = 42
605
641
c .bar = 'abc'
@@ -626,20 +662,20 @@ class C(Generic[T]):
626
662
assert C .__module__ == __name__
627
663
if not PY32 :
628
664
assert C .__qualname__ == 'GenericTests.test_repr_2.<locals>.C'
629
- assert repr (C ).split ('.' )[- 1 ] == 'C[~T] '
665
+ assert repr (C ).split ('.' )[- 1 ] == 'C<~T> '
630
666
X = C [int ]
631
667
assert X .__module__ == __name__
632
668
if not PY32 :
633
669
assert X .__qualname__ == 'C'
634
- assert repr (X ).split ('.' )[- 1 ] == 'C[int]'
670
+ assert repr (X ).split ('.' )[- 1 ] == 'C<~T> [int]'
635
671
636
672
class Y (C [int ]):
637
673
pass
638
674
639
675
assert Y .__module__ == __name__
640
676
if not PY32 :
641
677
assert Y .__qualname__ == 'GenericTests.test_repr_2.<locals>.Y'
642
- assert repr (Y ).split ('.' )[- 1 ] == 'Y[int] '
678
+ assert repr (Y ).split ('.' )[- 1 ] == 'Y'
643
679
644
680
def test_eq_1 (self ):
645
681
assert Generic == Generic
@@ -667,15 +703,14 @@ class A(Generic[T, VT]):
667
703
class B (Generic [KT , T ]):
668
704
pass
669
705
670
- class C (A , Generic [KT , VT ], B ):
706
+ class C (A [ T , VT ], Generic [VT , T , KT ], B [ KT , T ] ):
671
707
pass
672
708
673
- assert C .__parameters__ == (T , VT , KT )
709
+ assert C .__parameters__ == (VT , T , KT )
674
710
675
711
def test_nested (self ):
676
712
677
- class G (Generic ):
678
- pass
713
+ G = Generic
679
714
680
715
class Visitor (G [T ]):
681
716
@@ -721,9 +756,30 @@ def foo(x: T):
721
756
assert type (a ) is Node
722
757
assert type (b ) is Node
723
758
assert type (c ) is Node
759
+ assert a .label == x
760
+ assert b .label == x
761
+ assert c .label == x
724
762
725
763
foo (42 )
726
764
765
+ def test_implicit_any (self ):
766
+ T = TypeVar ('T' )
767
+
768
+ class C (Generic [T ]):
769
+ pass
770
+
771
+ class D (C ):
772
+ pass
773
+
774
+ assert D .__parameters__ == ()
775
+
776
+ with self .assertRaises (Exception ):
777
+ D [int ]
778
+ with self .assertRaises (Exception ):
779
+ D [Any ]
780
+ with self .assertRaises (Exception ):
781
+ D [T ]
782
+
727
783
728
784
class VarianceTests (TestCase ):
729
785
@@ -956,13 +1012,32 @@ def test_overload_fails(self):
956
1012
from typing import overload
957
1013
958
1014
with self .assertRaises (RuntimeError ):
1015
+
959
1016
@overload
960
1017
def blah ():
961
1018
pass
962
1019
1020
+ blah ()
963
1021
964
- T_a = TypeVar ('T' )
1022
+ def test_overload_succeeds (self ):
1023
+ from typing import overload
1024
+
1025
+ @overload
1026
+ def blah ():
1027
+ pass
1028
+
1029
+ def blah ():
1030
+ pass
1031
+
1032
+ blah ()
1033
+
1034
+
1035
+ PY35 = sys .version_info [:2 ] >= (3 , 5 )
965
1036
1037
+ PY35_TESTS = """
1038
+ import asyncio
1039
+
1040
+ T_a = TypeVar('T')
966
1041
967
1042
class AwaitableWrapper(typing.Awaitable[T_a]):
968
1043
@@ -973,7 +1048,6 @@ def __await__(self) -> typing.Iterator[T_a]:
973
1048
yield
974
1049
return self.value
975
1050
976
-
977
1051
class AsyncIteratorWrapper(typing.AsyncIterator[T_a]):
978
1052
979
1053
def __init__(self, value: typing.Iterable[T_a]):
@@ -989,6 +1063,10 @@ def __anext__(self) -> T_a:
989
1063
return data
990
1064
else:
991
1065
raise StopAsyncIteration
1066
+ """
1067
+
1068
+ if PY35 :
1069
+ exec (PY35_TESTS )
992
1070
993
1071
994
1072
class CollectionsAbcTests (TestCase ):
@@ -1015,9 +1093,14 @@ def test_iterator(self):
1015
1093
assert isinstance (it , typing .Iterator [int ])
1016
1094
assert not isinstance (42 , typing .Iterator )
1017
1095
1096
+ @skipUnless (PY35 , 'Python 3.5 required' )
1018
1097
def test_awaitable (self ):
1019
- async def foo () -> typing .Awaitable [int ]:
1020
- return await AwaitableWrapper (42 )
1098
+ ns = {}
1099
+ exec (
1100
+ "async def foo() -> typing.Awaitable[int]:\n "
1101
+ " return await AwaitableWrapper(42)\n " ,
1102
+ globals (), ns )
1103
+ foo = ns ['foo' ]
1021
1104
g = foo ()
1022
1105
assert issubclass (type (g ), typing .Awaitable [int ])
1023
1106
assert isinstance (g , typing .Awaitable )
@@ -1028,6 +1111,7 @@ async def foo() -> typing.Awaitable[int]:
1028
1111
typing .Awaitable [Manager ])
1029
1112
g .send (None ) # Run foo() till completion, to avoid warning.
1030
1113
1114
+ @skipUnless (PY35 , 'Python 3.5 required' )
1031
1115
def test_async_iterable (self ):
1032
1116
base_it = range (10 ) # type: Iterator[int]
1033
1117
it = AsyncIteratorWrapper (base_it )
@@ -1037,6 +1121,7 @@ def test_async_iterable(self):
1037
1121
typing .AsyncIterable [Employee ])
1038
1122
assert not isinstance (42 , typing .AsyncIterable )
1039
1123
1124
+ @skipUnless (PY35 , 'Python 3.5 required' )
1040
1125
def test_async_iterator (self ):
1041
1126
base_it = range (10 ) # type: Iterator[int]
1042
1127
it = AsyncIteratorWrapper (base_it )
@@ -1127,6 +1212,22 @@ class MyDict(typing.Dict[str, int]):
1127
1212
d = MyDict ()
1128
1213
assert isinstance (d , MyDict )
1129
1214
1215
+ def test_no_defaultdict_instantiation (self ):
1216
+ with self .assertRaises (TypeError ):
1217
+ typing .DefaultDict ()
1218
+ with self .assertRaises (TypeError ):
1219
+ typing .DefaultDict [KT , VT ]()
1220
+ with self .assertRaises (TypeError ):
1221
+ typing .DefaultDict [str , int ]()
1222
+
1223
+ def test_defaultdict_subclass_instantiation (self ):
1224
+
1225
+ class MyDefDict (typing .DefaultDict [str , int ]):
1226
+ pass
1227
+
1228
+ dd = MyDefDict ()
1229
+ assert isinstance (dd , MyDefDict )
1230
+
1130
1231
def test_no_set_instantiation (self ):
1131
1232
with self .assertRaises (TypeError ):
1132
1233
typing .Set ()
@@ -1251,15 +1352,15 @@ def stuff(a: TextIO) -> str:
1251
1352
return a .readline ()
1252
1353
1253
1354
a = stuff .__annotations__ ['a' ]
1254
- assert a .__parameters__ == (str , )
1355
+ assert a .__parameters__ == ()
1255
1356
1256
1357
def test_binaryio (self ):
1257
1358
1258
1359
def stuff (a : BinaryIO ) -> bytes :
1259
1360
return a .readline ()
1260
1361
1261
1362
a = stuff .__annotations__ ['a' ]
1262
- assert a .__parameters__ == (bytes , )
1363
+ assert a .__parameters__ == ()
1263
1364
1264
1365
def test_io_submodule (self ):
1265
1366
from typing .io import IO , TextIO , BinaryIO , __all__ , __name__
@@ -1346,8 +1447,9 @@ def test_all(self):
1346
1447
assert 'ValuesView' in a
1347
1448
assert 'cast' in a
1348
1449
assert 'overload' in a
1349
- assert 'io' in a
1350
- assert 're' in a
1450
+ # Check that io and re are not exported.
1451
+ assert 'io' not in a
1452
+ assert 're' not in a
1351
1453
# Spot-check that stdlib modules aren't exported.
1352
1454
assert 'os' not in a
1353
1455
assert 'sys' not in a
0 commit comments