Skip to content

Commit

Permalink
Merge pull request #222 from sloria/collections-imports
Browse files Browse the repository at this point in the history
Address all collections.abc DeprecationWarnings
  • Loading branch information
blag authored Sep 14, 2019
2 parents 233f814 + 0831e52 commit 6116ffd
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 29 deletions.
5 changes: 5 additions & 0 deletions flex/_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Python 2/3 compatibility module."""
try:
from collections.abc import Mapping, Sequence # noqa: F401
except ImportError: # Python 2
from collections import Mapping, Sequence # noqa: F401
6 changes: 2 additions & 4 deletions flex/constants.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from __future__ import unicode_literals

import numbers
try:
from collections.abc import Sequence, Mapping
except ImportError: # Python 2
from collections import Sequence, Mapping
import six

from flex._compat import Sequence, Mapping


SCHEMES = (
'http', 'https', 'ws', 'wss',
Expand Down
4 changes: 2 additions & 2 deletions flex/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

from six.moves import urllib_parse as urlparse
import os
import collections
import requests
from copy import deepcopy

import six
import json
import yaml

from flex._compat import Mapping
from flex.context_managers import ErrorDict
from flex.exceptions import ValidationError
from flex.loading.definitions import (
Expand Down Expand Up @@ -42,7 +42,7 @@ def load_source(source):
- json string.
- yaml string.
"""
if isinstance(source, collections.Mapping):
if isinstance(source, Mapping):
return deepcopy(source)
elif hasattr(source, 'read') and callable(source.read):
raw_source = source.read()
Expand Down
5 changes: 3 additions & 2 deletions flex/datastructures.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import collections

from flex._compat import Mapping
from flex.exceptions import (
ValidationError,
ErrorDict,
Expand Down Expand Up @@ -28,7 +29,7 @@ def __init__(self, value=None):

def add_validator(self, validator):
if is_non_string_iterable(validator)\
and not isinstance(validator, collections.Mapping):
and not isinstance(validator, Mapping):
for value in validator:
self.add_validator(value)
else:
Expand All @@ -50,7 +51,7 @@ class ValidationDict(collections.defaultdict):
def __init__(self, validators=None):
super(ValidationDict, self).__init__(ValidationList)
if validators is not None:
if not isinstance(validators, collections.Mapping):
if not isinstance(validators, Mapping):
raise ValueError("ValidationDict may only be instantiated with a mapping")
for key, validator in validators.items():
self.add_validator(key, validator)
Expand Down
11 changes: 6 additions & 5 deletions flex/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import six
import collections

from flex._compat import Mapping
from flex.utils import (
is_non_string_iterable,
prettify_errors,
Expand Down Expand Up @@ -37,7 +38,7 @@ def add_error(self, error):
Otherwise, the value is appended.
"""
if is_non_string_iterable(error) and not isinstance(error, collections.Mapping):
if is_non_string_iterable(error) and not isinstance(error, Mapping):
for value in error:
self.add_error(value)
else:
Expand All @@ -56,13 +57,13 @@ def add_error(self, key, error):

class ValidationError(ValueError):
def __init__(self, error):
if not isinstance(error, collections.Mapping) and \
if not isinstance(error, Mapping) and \
is_non_string_iterable(error) and \
len(error) == 1:
error = error[0]
self._error = error

if isinstance(self._error, collections.Mapping):
if isinstance(self._error, Mapping):
self.error_dict = self._error
elif is_non_string_iterable(self._error):
self.error_list = self._error
Expand All @@ -80,15 +81,15 @@ def __str__(self):
def detail(self):
if isinstance(self._error, six.string_types):
return [self._error]
elif isinstance(self._error, collections.Mapping):
elif isinstance(self._error, Mapping):
return self._error
return self._error

@property
def messages(self):
if isinstance(self._error, six.string_types):
return [self._error]
elif isinstance(self._error, collections.Mapping):
elif isinstance(self._error, Mapping):
return [self._error]
return self._error

Expand Down
3 changes: 2 additions & 1 deletion flex/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import functools
import re

from flex._compat import Mapping
from flex.exceptions import MultiplePathsFound
from flex.error_messages import MESSAGES
from flex.constants import (
Expand Down Expand Up @@ -146,7 +147,7 @@ def match_path_to_api_path(path_definitions, target_path, base_path='',
"""
if context is None:
context = {}
assert isinstance(context, collections.Mapping)
assert isinstance(context, Mapping)
if target_path.startswith(base_path):
# Convert all of the api paths into Path instances for easier regex
# matching.
Expand Down
8 changes: 4 additions & 4 deletions flex/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import math
import collections
import numbers

from six.moves import urllib_parse as urlparse
import six

import jsonpointer

from flex._compat import Mapping, Sequence
from flex.constants import (
PRIMITIVE_TYPES,
NULL,
Expand Down Expand Up @@ -43,7 +43,7 @@ def is_non_string_iterable(value):


def pluralize(value):
if is_non_string_iterable(value) and not isinstance(value, collections.Mapping):
if is_non_string_iterable(value) and not isinstance(value, Mapping):
return value
return [value]

Expand Down Expand Up @@ -127,7 +127,7 @@ def get_type_for_value(value):

def is_single_item_iterable(value):
if is_non_string_iterable(value):
if isinstance(value, collections.Sequence):
if isinstance(value, Sequence):
if len(value) == 1:
return True
return False
Expand Down Expand Up @@ -161,7 +161,7 @@ def format_errors(errors, indent=0, prefix='', suffix=''):
if isinstance(errors, SINGULAR_TYPES):
yield indent_message(repr(errors), indent, prefix=prefix, suffix=suffix)

elif isinstance(errors, collections.Mapping):
elif isinstance(errors, Mapping):
for key, value in errors.items():
assert isinstance(key, SINGULAR_TYPES), type(key)
if isinstance(value, SINGULAR_TYPES):
Expand Down
5 changes: 3 additions & 2 deletions flex/validation/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import six


from flex._compat import Mapping, Sequence
from flex.exceptions import (
ValidationError,
ErrorDict,
Expand Down Expand Up @@ -458,11 +459,11 @@ def generate_value_processor(type_, collectionFormat=None, items=None, **kwargs)
# strip off any whitespace
processors.append(functools.partial(map, operator.methodcaller('strip')))
if items is not None:
if isinstance(items, collections.Mapping):
if isinstance(items, Mapping):
items_processors = itertools.repeat(
generate_value_processor(**items)
)
elif isinstance(items, collections.Sequence):
elif isinstance(items, Sequence):
items_processors = itertools.chain(
(generate_value_processor(**item) for item in items),
itertools.repeat(lambda v: v),
Expand Down
8 changes: 4 additions & 4 deletions flex/validation/schema.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import itertools
import collections
import functools

import six

from flex._compat import Mapping, Sequence
from flex.exceptions import (
ValidationError,
ErrorList,
Expand Down Expand Up @@ -93,7 +93,7 @@ def generate_max_properties_validator(maxProperties, **kwargs):


def construct_items_validators(items, context):
if isinstance(items, collections.Mapping):
if isinstance(items, Mapping):
items_validators = construct_schema_validators(
schema=items,
context=context,
Expand Down Expand Up @@ -126,15 +126,15 @@ def validate_items(objs, field_validators, **kwargs):


def generate_items_validator(items, context, **kwargs):
if isinstance(items, collections.Mapping):
if isinstance(items, Mapping):
# If items is a reference or a schema, we pass it through as an
# ever repeating list of the same validation dictionary, thus
# validating all of the objects against the same schema.
items_validators = itertools.repeat(construct_items_validators(
items,
context,
))
elif isinstance(items, collections.Sequence):
elif isinstance(items, Sequence):
# We generate a list of validator dictionaries and then chain it
# with an empty schema that repeats forever. This ensures that if
# the array of objects to be validated is longer than the array of
Expand Down
4 changes: 2 additions & 2 deletions tests/core/test_load_source.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from __future__ import unicode_literals

import tempfile
import collections

import six

import json
import yaml

from flex._compat import Mapping
from flex.core import load_source


Expand Down Expand Up @@ -105,7 +105,7 @@ def test_url(httpbin):
}
source = httpbin.url + '/get'
result = load_source(source)
assert isinstance(result, collections.Mapping)
assert isinstance(result, Mapping)
result.pop('headers')
result.pop('url')
assert result == native
7 changes: 4 additions & 3 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import functools
import collections
import re
import six

from flex._compat import Mapping
from flex.validation.common import validate_object
from flex.loading.schema.paths.path_item.operation.responses.single.schema import (
schema_validator,
Expand Down Expand Up @@ -57,7 +58,7 @@ def _find_message_in_errors(message, errors, namespace=''):
if isinstance(errors, six.string_types):
if check_if_error_message_equal(errors, message):
yield namespace
elif isinstance(errors, collections.Mapping):
elif isinstance(errors, Mapping):
for key, error in errors.items():
for match in _find_message_in_errors(
message,
Expand Down Expand Up @@ -124,7 +125,7 @@ def assert_message_not_in_errors(message, errors):
def _enumerate_error_paths(errors, namespace=''):
if isinstance(errors, six.string_types):
yield namespace
elif isinstance(errors, collections.Mapping):
elif isinstance(errors, Mapping):
for key, error in errors.items():
for match in _enumerate_error_paths(
error,
Expand Down

0 comments on commit 6116ffd

Please sign in to comment.