Switch from 'collections' -> 'collections.abc'

Resolve the following deprecation warnings on Python 3.x:

    DeprecationWarning: Using or importing the ABCs from 'collections'
    instead of from 'collections.abc' is deprecated since Python 3.3,and
    in 3.9 it will stop working"

Note that even though we're in Ussuri, I've kept this Python 2
compatible since we haven't done all the other work to mark this
package as Python 3-only.

Change-Id: Iff4cf1871a6a91d91da03d9b79ef61e715a979cf
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2019-12-05 14:46:18 +00:00
parent 9da166ae7f
commit 1209fb97c7
4 changed files with 29 additions and 14 deletions

View File

@ -21,13 +21,17 @@ store unserialized data generated by generators during
the report serialization process.
"""
import collections as col
import copy
try: # python 3
from collections import abc
except ImportError: # python 2
import collections as abc
import six
class ReportModel(col.MutableMapping):
class ReportModel(abc.MutableMapping):
"""A Report Data Model
A report data model contains data generated by some
@ -52,9 +56,9 @@ class ReportModel(col.MutableMapping):
self.attached_view = attached_view
if data is not None:
if isinstance(data, col.Mapping):
if isinstance(data, abc.Mapping):
self.data = dict(data)
elif isinstance(data, col.Sequence):
elif isinstance(data, abc.Sequence):
# convert a list [a, b, c] to a dict {0: a, 1: b, 2: c}
self.data = dict(enumerate(data))
else:
@ -151,11 +155,11 @@ class ReportModel(col.MutableMapping):
if hasattr(obj, 'set_current_view_type'):
obj.set_current_view_type(tp, visited=visited)
if isinstance(obj, col.Sequence):
if isinstance(obj, abc.Sequence):
for item in obj:
traverse_obj(item)
elif isinstance(obj, col.Mapping):
elif isinstance(obj, abc.Mapping):
for val in six.itervalues(obj):
traverse_obj(val)

View File

@ -12,9 +12,13 @@
# License for the specific language governing permissions and limitations
# under the License.
import collections as col
import re
try: # python 3
from collections import abc
except ImportError: # python 2
import collections as abc
from oslotest import base
import six
@ -119,7 +123,7 @@ class TestBaseModel(base.BaseTestCase):
self.assertEqual('0: a;1: b;', six.text_type(model))
def test_immutable_mappings_produce_mutable_models(self):
class SomeImmutableMapping(col.Mapping):
class SomeImmutableMapping(abc.Mapping):
def __init__(self):
self.data = {'a': 2, 'b': 4, 'c': 8}

View File

@ -18,7 +18,10 @@ This modules provides several generic views for
serializing models into human-readable text.
"""
import collections as col
try: # python 3
from collections import abc
except ImportError: # python 2
import collections as abc
import six
@ -112,7 +115,7 @@ class KeyValueView(object):
if rootkey is not None:
res.append((self.indent_str * indent) + rootkey)
if isinstance(root, col.Mapping):
if isinstance(root, abc.Mapping):
if rootkey is None and indent > 0:
res.append((self.indent_str * indent) + self.anon_dict)
elif rootkey is not None:
@ -122,7 +125,7 @@ class KeyValueView(object):
for key in sorted(root):
res.extend(serialize(root[key], key, indent + 1))
elif (isinstance(root, col.Sequence) and
elif (isinstance(root, abc.Sequence) and
not isinstance(root, six.string_types)):
if rootkey is not None:
res[0] += self.list_sep

View File

@ -23,10 +23,14 @@ and non-naive serializers check for this attribute and handle
such strings specially)
"""
import collections as col
import copy
import xml.etree.ElementTree as ET
try: # python 3
from collections import abc
except ImportError: # python 2
import collections as abc
import six
from oslo_reports import _utils as utils
@ -65,10 +69,10 @@ class KeyValueView(object):
def serialize(rootmodel, rootkeyname):
res = ET.Element(rootkeyname)
if isinstance(rootmodel, col.Mapping):
if isinstance(rootmodel, abc.Mapping):
for key in sorted(rootmodel):
res.append(serialize(rootmodel[key], key))
elif (isinstance(rootmodel, col.Sequence) and
elif (isinstance(rootmodel, abc.Sequence) and
not isinstance(rootmodel, six.string_types)):
for val in sorted(rootmodel, key=str):
res.append(serialize(val, 'item'))