Switch to collections.abc.*

The abstract base classes previously defined in 'collections' were moved
to 'collections.abc' in 3.3. The aliases will be removed in 3.10.
Preempt this change now with a simple find-replace:

  $ ag -l 'collections.($TYPES)' | \
      xargs sed -i 's/\(collections\)\.\($TYPES\)/\1.abc.\2/g'

Where $TYPES is the list of moved ABCs from [1].

[1] https://docs.python.org/3/library/collections.abc.html

Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
Change-Id: Ib07d778a01275d7c985e059156e95abc112e81c8
This commit is contained in:
Stephen Finucane 2021-02-01 10:31:17 +00:00
parent 4670f0949c
commit c3bda9eeb1
3 changed files with 19 additions and 19 deletions

View File

@ -32,35 +32,35 @@ NO_VALUE = create_marker('<NoValue>')
def is_iterator(obj): def is_iterator(obj):
return isinstance(obj, collections.Iterator) return isinstance(obj, collections.abc.Iterator)
def is_iterable(obj): def is_iterable(obj):
return ( return (
isinstance(obj, collections.Iterable) and isinstance(obj, collections.abc.Iterable) and
not isinstance(obj, (str, MappingType)) not isinstance(obj, (str, MappingType))
) )
def is_sequence(obj): def is_sequence(obj):
return isinstance(obj, collections.Sequence) and not isinstance( return isinstance(obj, collections.abc.Sequence) and not isinstance(
obj, str) obj, str)
def is_mutable(obj): def is_mutable(obj):
return isinstance(obj, (collections.MutableSequence, return isinstance(obj, (collections.abc.MutableSequence,
collections.MutableSet, collections.abc.MutableSet,
collections.MutableMapping)) collections.abc.MutableMapping))
SequenceType = collections.Sequence SequenceType = collections.abc.Sequence
MutableSequenceType = collections.MutableSequence MutableSequenceType = collections.abc.MutableSequence
SetType = collections.Set SetType = collections.abc.Set
MutableSetType = collections.MutableSet MutableSetType = collections.abc.MutableSet
MappingType = collections.Mapping MappingType = collections.abc.Mapping
MutableMappingType = collections.MutableMapping MutableMappingType = collections.abc.MutableMapping
IterableType = collections.Iterable IterableType = collections.abc.Iterable
IteratorType = collections.Iterator IteratorType = collections.abc.Iterator
QueueType = collections.deque QueueType = collections.deque
@ -85,7 +85,7 @@ def convert_input_data(obj, rec=None):
def convert_output_data(obj, limit_func, engine, rec=None): def convert_output_data(obj, limit_func, engine, rec=None):
if rec is None: if rec is None:
rec = convert_output_data rec = convert_output_data
if isinstance(obj, collections.Mapping): if isinstance(obj, collections.abc.Mapping):
result = {} result = {}
for key, value in limit_func(obj.items()): for key, value in limit_func(obj.items()):
result[rec(key, limit_func, engine, rec)] = rec( result[rec(key, limit_func, engine, rec)] = rec(
@ -119,7 +119,7 @@ class MappingRule(object):
self.destination = destination self.destination = destination
class FrozenDict(collections.Mapping): class FrozenDict(collections.abc.Mapping):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self._d = dict(*args, **kwargs) self._d = dict(*args, **kwargs)
self._hash = None self._hash = None

View File

@ -185,7 +185,7 @@ class Iterable(PythonType):
def __init__(self, validators=None, nullable=False): def __init__(self, validators=None, nullable=False):
super(Iterable, self).__init__( super(Iterable, self).__init__(
collections.Iterable, nullable, collections.abc.Iterable, nullable,
[lambda t: not isinstance(t, (str, utils.MappingType))] + ( [lambda t: not isinstance(t, (str, utils.MappingType))] + (
validators or [])) validators or []))
@ -217,7 +217,7 @@ class Sequence(PythonType):
def __init__(self, validators=None, nullable=False): def __init__(self, validators=None, nullable=False):
super(Sequence, self).__init__( super(Sequence, self).__init__(
collections.Sequence, nullable, [ collections.abc.Sequence, nullable, [
lambda t: not isinstance(t, (str, dict))] + ( lambda t: not isinstance(t, (str, dict))] + (
validators or [])) validators or []))

View File

@ -883,7 +883,7 @@ class GroupAggregator(object):
else: else:
if not ( if not (
len(value_list) == 2 and len(value_list) == 2 and
isinstance(result, collections.Sequence) and isinstance(result, collections.abc.Sequence) and
not isinstance(result, str) and not isinstance(result, str) and
len(result) == 2 and len(result) == 2 and
result[0] == value_list[0] result[0] == value_list[0]