26
26
from typing import Pattern , Match
27
27
import typing
28
28
import weakref
29
- try :
30
- import collections .abc as collections_abc
31
- except ImportError :
32
- import collections as collections_abc # Fallback for PY3.2.
29
+ import collections
33
30
34
31
35
32
class BaseTestCase (TestCase ):
@@ -1055,7 +1052,7 @@ def test_supports_index(self):
1055
1052
self .assertIsSubclass (int , typing .SupportsIndex )
1056
1053
self .assertNotIsSubclass (str , typing .SupportsIndex )
1057
1054
1058
- def test_protocol_instance_type_error (self ):
1055
+ def test_protocol_instance_works (self ):
1059
1056
self .assertIsInstance (0 , typing .SupportsAbs )
1060
1057
self .assertNotIsInstance ('no' , typing .SupportsAbs )
1061
1058
class C1 (typing .SupportsInt ):
@@ -1066,6 +1063,21 @@ class C2(C1):
1066
1063
c = C2 ()
1067
1064
self .assertIsInstance (c , C1 )
1068
1065
1066
+ def test_collections_protocols_allowed (self ):
1067
+ @runtime_checkable
1068
+ class Custom (collections .Iterable , Protocol ):
1069
+ def close (self ): pass
1070
+
1071
+ class A (object ): pass
1072
+ class B (object ):
1073
+ def __iter__ (self ):
1074
+ return []
1075
+ def close (self ):
1076
+ return 0
1077
+
1078
+ self .assertIsSubclass (B , Custom )
1079
+ self .assertNotIsSubclass (A , Custom )
1080
+
1069
1081
1070
1082
class GenericTests (BaseTestCase ):
1071
1083
@@ -1243,17 +1255,17 @@ def __len__(self):
1243
1255
return 0
1244
1256
# this should just work
1245
1257
MM ().update ()
1246
- self .assertIsInstance (MM (), collections_abc .MutableMapping )
1258
+ self .assertIsInstance (MM (), collections .MutableMapping )
1247
1259
self .assertIsInstance (MM (), MutableMapping )
1248
1260
self .assertNotIsInstance (MM (), List )
1249
1261
self .assertNotIsInstance ({}, MM )
1250
1262
1251
1263
def test_multiple_bases (self ):
1252
- class MM1 (MutableMapping [str , str ], collections_abc .MutableMapping ):
1264
+ class MM1 (MutableMapping [str , str ], collections .MutableMapping ):
1253
1265
pass
1254
1266
with self .assertRaises (TypeError ):
1255
1267
# consistent MRO not possible
1256
- class MM2 (collections_abc .MutableMapping , MutableMapping [str , str ]):
1268
+ class MM2 (collections .MutableMapping , MutableMapping [str , str ]):
1257
1269
pass
1258
1270
1259
1271
def test_orig_bases (self ):
@@ -1426,9 +1438,9 @@ def __call__(self):
1426
1438
1427
1439
self .assertEqual (repr (C1 [int ]).split ('.' )[- 1 ], 'C1[int]' )
1428
1440
self .assertEqual (C2 .__parameters__ , ())
1429
- self .assertIsInstance (C2 (), collections_abc .Callable )
1430
- self .assertIsSubclass (C2 , collections_abc .Callable )
1431
- self .assertIsSubclass (C1 , collections_abc .Callable )
1441
+ self .assertIsInstance (C2 (), collections .Callable )
1442
+ self .assertIsSubclass (C2 , collections .Callable )
1443
+ self .assertIsSubclass (C1 , collections .Callable )
1432
1444
self .assertIsInstance (T1 (), tuple )
1433
1445
self .assertIsSubclass (T2 , tuple )
1434
1446
self .assertIsSubclass (Tuple [int , ...], typing .Sequence )
@@ -2287,11 +2299,11 @@ def __len__(self):
2287
2299
self .assertIsSubclass (MMC , typing .Mapping )
2288
2300
2289
2301
self .assertIsInstance (MMB [KT , VT ](), typing .Mapping )
2290
- self .assertIsInstance (MMB [KT , VT ](), collections_abc .Mapping )
2302
+ self .assertIsInstance (MMB [KT , VT ](), collections .Mapping )
2291
2303
2292
- self .assertIsSubclass (MMA , collections_abc .Mapping )
2293
- self .assertIsSubclass (MMB , collections_abc .Mapping )
2294
- self .assertIsSubclass (MMC , collections_abc .Mapping )
2304
+ self .assertIsSubclass (MMA , collections .Mapping )
2305
+ self .assertIsSubclass (MMB , collections .Mapping )
2306
+ self .assertIsSubclass (MMC , collections .Mapping )
2295
2307
2296
2308
self .assertIsSubclass (MMB [str , str ], typing .Mapping )
2297
2309
self .assertIsSubclass (MMC , MMA )
@@ -2303,9 +2315,9 @@ class G(typing.Generator[int, int, int]): pass
2303
2315
def g (): yield 0
2304
2316
self .assertIsSubclass (G , typing .Generator )
2305
2317
self .assertIsSubclass (G , typing .Iterable )
2306
- if hasattr (collections_abc , 'Generator' ):
2307
- self .assertIsSubclass (G , collections_abc .Generator )
2308
- self .assertIsSubclass (G , collections_abc .Iterable )
2318
+ if hasattr (collections , 'Generator' ):
2319
+ self .assertIsSubclass (G , collections .Generator )
2320
+ self .assertIsSubclass (G , collections .Iterable )
2309
2321
self .assertNotIsSubclass (type (g ), G )
2310
2322
2311
2323
def test_subclassing_subclasshook (self ):
@@ -2341,23 +2353,23 @@ class D: pass
2341
2353
self .assertIsSubclass (D , B )
2342
2354
2343
2355
class M (): pass
2344
- collections_abc .MutableMapping .register (M )
2356
+ collections .MutableMapping .register (M )
2345
2357
self .assertIsSubclass (M , typing .Mapping )
2346
2358
2347
2359
def test_collections_as_base (self ):
2348
2360
2349
- class M (collections_abc .Mapping ): pass
2361
+ class M (collections .Mapping ): pass
2350
2362
self .assertIsSubclass (M , typing .Mapping )
2351
2363
self .assertIsSubclass (M , typing .Iterable )
2352
2364
2353
- class S (collections_abc .MutableSequence ): pass
2365
+ class S (collections .MutableSequence ): pass
2354
2366
self .assertIsSubclass (S , typing .MutableSequence )
2355
2367
self .assertIsSubclass (S , typing .Iterable )
2356
2368
2357
- class I (collections_abc .Iterable ): pass
2369
+ class I (collections .Iterable ): pass
2358
2370
self .assertIsSubclass (I , typing .Iterable )
2359
2371
2360
- class A (collections_abc .Mapping ): pass
2372
+ class A (collections .Mapping ): pass
2361
2373
class B : pass
2362
2374
A .register (B )
2363
2375
self .assertIsSubclass (B , typing .Mapping )
0 commit comments