17
17
__all__ = ['deque' , 'defaultdict' , 'namedtuple' , 'UserDict' , 'UserList' ,
18
18
'UserString' , 'Counter' , 'OrderedDict' , 'ChainMap' ]
19
19
20
- # For backwards compatibility, continue to make the collections ABCs
21
- # through Python 3.6 available through the collections module.
22
- # Note, no new collections ABCs were added in Python 3.7
23
20
import _collections_abc
24
- from _collections_abc import (AsyncGenerator , AsyncIterable , AsyncIterator ,
25
- Awaitable , ByteString , Callable , Collection , Container , Coroutine ,
26
- Generator , Hashable , ItemsView , Iterable , Iterator , KeysView , Mapping ,
27
- MappingView , MutableMapping , MutableSequence , MutableSet , Reversible ,
28
- Sequence , Set , Sized , ValuesView )
29
-
30
21
from operator import itemgetter as _itemgetter , eq as _eq
31
22
from keyword import iskeyword as _iskeyword
32
23
import sys as _sys
40
31
except ImportError :
41
32
pass
42
33
else :
43
- MutableSequence .register (deque )
34
+ _collections_abc . MutableSequence .register (deque )
44
35
45
36
try :
46
37
from _collections import defaultdict
47
38
except ImportError :
48
39
pass
49
40
50
41
42
+ def __getattr__ (name ):
43
+ # For backwards compatibility, continue to make the collections ABCs
44
+ # through Python 3.6 available through the collections module.
45
+ # Note, no new collections ABCs were added in Python 3.7
46
+ if name in _collections_abc .__all__ :
47
+ obj = getattr (_collections_abc , name )
48
+ import warnings
49
+ warnings .warn ("Using or importing the ABCs from 'collections' instead "
50
+ "of from 'collections.abc' is deprecated, "
51
+ "and in 3.8 it will stop working" ,
52
+ DeprecationWarning , stacklevel = 2 )
53
+ globals ()[name ] = obj
54
+ return obj
55
+ raise AttributeError (f'module { __name__ !r} has no attribute { name !r} ' )
56
+
51
57
################################################################################
52
58
### OrderedDict
53
59
################################################################################
54
60
55
- class _OrderedDictKeysView (KeysView ):
61
+ class _OrderedDictKeysView (_collections_abc . KeysView ):
56
62
57
63
def __reversed__ (self ):
58
64
yield from reversed (self ._mapping )
59
65
60
- class _OrderedDictItemsView (ItemsView ):
66
+ class _OrderedDictItemsView (_collections_abc . ItemsView ):
61
67
62
68
def __reversed__ (self ):
63
69
for key in reversed (self ._mapping ):
64
70
yield (key , self ._mapping [key ])
65
71
66
- class _OrderedDictValuesView (ValuesView ):
72
+ class _OrderedDictValuesView (_collections_abc . ValuesView ):
67
73
68
74
def __reversed__ (self ):
69
75
for key in reversed (self ._mapping ):
@@ -215,7 +221,7 @@ def __sizeof__(self):
215
221
size += sizeof (self .__root ) * n # proxy objects
216
222
return size
217
223
218
- update = __update = MutableMapping .update
224
+ update = __update = _collections_abc . MutableMapping .update
219
225
220
226
def keys (self ):
221
227
"D.keys() -> a set-like object providing a view on D's keys"
@@ -229,7 +235,7 @@ def values(self):
229
235
"D.values() -> an object providing a view on D's values"
230
236
return _OrderedDictValuesView (self )
231
237
232
- __ne__ = MutableMapping .__ne__
238
+ __ne__ = _collections_abc . MutableMapping .__ne__
233
239
234
240
__marker = object ()
235
241
@@ -636,7 +642,7 @@ def update(*args, **kwds):
636
642
raise TypeError ('expected at most 1 arguments, got %d' % len (args ))
637
643
iterable = args [0 ] if args else None
638
644
if iterable is not None :
639
- if isinstance (iterable , Mapping ):
645
+ if isinstance (iterable , _collections_abc . Mapping ):
640
646
if self :
641
647
self_get = self .get
642
648
for elem , count in iterable .items ():
@@ -673,7 +679,7 @@ def subtract(*args, **kwds):
673
679
iterable = args [0 ] if args else None
674
680
if iterable is not None :
675
681
self_get = self .get
676
- if isinstance (iterable , Mapping ):
682
+ if isinstance (iterable , _collections_abc . Mapping ):
677
683
for elem , count in iterable .items ():
678
684
self [elem ] = self_get (elem , 0 ) - count
679
685
else :
@@ -875,7 +881,7 @@ def __iand__(self, other):
875
881
### ChainMap
876
882
########################################################################
877
883
878
- class ChainMap (MutableMapping ):
884
+ class ChainMap (_collections_abc . MutableMapping ):
879
885
''' A ChainMap groups multiple dicts (or other mappings) together
880
886
to create a single, updateable view.
881
887
@@ -983,7 +989,7 @@ def clear(self):
983
989
### UserDict
984
990
################################################################################
985
991
986
- class UserDict (MutableMapping ):
992
+ class UserDict (_collections_abc . MutableMapping ):
987
993
988
994
# Start by filling-out the abstract methods
989
995
def __init__ (* args , ** kwargs ):
@@ -1050,7 +1056,7 @@ def fromkeys(cls, iterable, value=None):
1050
1056
### UserList
1051
1057
################################################################################
1052
1058
1053
- class UserList (MutableSequence ):
1059
+ class UserList (_collections_abc . MutableSequence ):
1054
1060
"""A more or less complete user-defined wrapper around list objects."""
1055
1061
def __init__ (self , initlist = None ):
1056
1062
self .data = []
@@ -1123,7 +1129,7 @@ def extend(self, other):
1123
1129
### UserString
1124
1130
################################################################################
1125
1131
1126
- class UserString (Sequence ):
1132
+ class UserString (_collections_abc . Sequence ):
1127
1133
def __init__ (self , seq ):
1128
1134
if isinstance (seq , str ):
1129
1135
self .data = seq
0 commit comments