From 136da9ddfa19b8eb30004553a017e84364c9ffd8 Mon Sep 17 00:00:00 2001 From: Colin-b Date: Mon, 14 Oct 2019 13:25:17 +0200 Subject: [PATCH] Import the ABCs from 'collections.abc' instead of 'collections' by default as it is deprecated since Python3.7, and in 3.8 it will stop working. --- CHANGELOG.rst | 1 + doc/marshalling.rst | 6 +----- doc/quickstart.rst | 2 -- flask_restplus/api.py | 6 +++++- flask_restplus/marshalling.py | 6 +++++- flask_restplus/mask.py | 6 +++++- flask_restplus/model.py | 6 +++++- flask_restplus/schemas/__init__.py | 7 +++++-- flask_restplus/swagger.py | 6 +++--- flask_restplus/utils.py | 6 +++++- tests/test_fields.py | 6 +++++- tests/test_fields_mask.py | 6 +++++- tests/test_marshalling.py | 6 +++++- tests/test_model.py | 6 +++++- 14 files changed, 55 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 66a418b3..ceceaa0a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,7 @@ Current ------- - Ensure that exceptions raised in error handler, including programming errors, are logged (:issue:`705`, :pr:`706`) +- Import the ABCs from 'collections.abc' instead of 'collections' by default as it is deprecated since Python3.7, and in 3.8 it will stop working. Python2.7 is still supported though. 0.13.0 (2019-08-12) ------------------- diff --git a/doc/marshalling.rst b/doc/marshalling.rst index 33fee685..5a13c71d 100644 --- a/doc/marshalling.rst +++ b/doc/marshalling.rst @@ -257,15 +257,11 @@ with other fields, you may want to use an ``OrderedDict`` and use the :class:`~fields.Wildcard` as the last field :: >>> from flask_restplus import fields, marshal - >>> from collections import OrderedDict >>> import json >>> >>> wild = fields.Wildcard(fields.Integer) - >>> mod = OrderedDict() - >>> mod['zoro'] = fields.String - >>> mod['*'] = wild >>> # you can use it in api.model like this: - >>> # some_fields = api.model('MyModel', mod) + >>> # some_fields = api.model('MyModel', {'zoro': fields.String, '*': wild}) >>> >>> data = {'John': 12, 'bob': 42, 'Jane': '68', 'zoro': 72} >>> json.dumps(marshal(data, mod)) diff --git a/doc/quickstart.rst b/doc/quickstart.rst index fcb74502..50401827 100644 --- a/doc/quickstart.rst +++ b/doc/quickstart.rst @@ -260,8 +260,6 @@ you use the ``fields`` module to describe the structure of your response. .. code-block:: python - from collections import OrderedDict - from flask import Flask from flask_restplus import fields, Api, Resource diff --git a/flask_restplus/api.py b/flask_restplus/api.py index 5817a2a5..588252b0 100644 --- a/flask_restplus/api.py +++ b/flask_restplus/api.py @@ -10,7 +10,11 @@ import six import sys -from collections import OrderedDict +try: + from collections.abc import OrderedDict +except ImportError: + # TODO Remove this to drop Python2 support + from collections import OrderedDict from functools import wraps, partial from types import MethodType diff --git a/flask_restplus/marshalling.py b/flask_restplus/marshalling.py index 5284fb14..84c8feb2 100644 --- a/flask_restplus/marshalling.py +++ b/flask_restplus/marshalling.py @@ -1,7 +1,11 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -from collections import OrderedDict +try: + from collections.abc import OrderedDict +except ImportError: + # TODO Remove this to drop Python2 support + from collections import OrderedDict from functools import wraps from six import iteritems diff --git a/flask_restplus/mask.py b/flask_restplus/mask.py index e4c1670c..ee61669d 100644 --- a/flask_restplus/mask.py +++ b/flask_restplus/mask.py @@ -5,7 +5,11 @@ import re import six -from collections import OrderedDict +try: + from collections.abc import OrderedDict +except ImportError: + # TODO Remove this to drop Python2 support + from collections import OrderedDict from inspect import isclass from .errors import RestError diff --git a/flask_restplus/model.py b/flask_restplus/model.py index ede02cb4..cbe1a3cf 100644 --- a/flask_restplus/model.py +++ b/flask_restplus/model.py @@ -5,7 +5,11 @@ import re import warnings -from collections import OrderedDict, MutableMapping +try: + from collections.abc import OrderedDict, MutableMapping +except ImportError: + # TODO Remove this to drop Python2 support + from collections import OrderedDict, MutableMapping from six import iteritems, itervalues from werkzeug.utils import cached_property diff --git a/flask_restplus/schemas/__init__.py b/flask_restplus/schemas/__init__.py index 11e394c1..90e5fc97 100644 --- a/flask_restplus/schemas/__init__.py +++ b/flask_restplus/schemas/__init__.py @@ -11,8 +11,11 @@ import json import pkg_resources -from collections import Mapping - +try: + from collections.abc import Mapping +except ImportError: + # TODO Remove this to drop Python2 support + from collections import Mapping from jsonschema import Draft4Validator from flask_restplus import errors diff --git a/flask_restplus/swagger.py b/flask_restplus/swagger.py index 77c364bf..64511ef0 100644 --- a/flask_restplus/swagger.py +++ b/flask_restplus/swagger.py @@ -5,11 +5,11 @@ import re from inspect import isclass, getdoc -from collections import OrderedDict try: - from collections.abc import Hashable + from collections.abc import OrderedDict, Hashable except ImportError: - from collections import Hashable + # TODO Remove this to drop Python2 support + from collections import OrderedDict, Hashable from six import string_types, itervalues, iteritems, iterkeys from flask import current_app diff --git a/flask_restplus/utils.py b/flask_restplus/utils.py index 9ec17721..a094d2dd 100644 --- a/flask_restplus/utils.py +++ b/flask_restplus/utils.py @@ -3,7 +3,11 @@ import re -from collections import OrderedDict +try: + from collections.abc import OrderedDict +except ImportError: + # TODO Remove this to drop Python2 support + from collections import OrderedDict from copy import deepcopy from six import iteritems diff --git a/tests/test_fields.py b/tests/test_fields.py index fbc156f0..837e607d 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -1,7 +1,11 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -from collections import OrderedDict +try: + from collections.abc import OrderedDict +except ImportError: + # TODO Remove this to drop Python2 support + from collections import OrderedDict from datetime import date, datetime from decimal import Decimal from functools import partial diff --git a/tests/test_fields_mask.py b/tests/test_fields_mask.py index 6328ae8e..7ffeefc8 100644 --- a/tests/test_fields_mask.py +++ b/tests/test_fields_mask.py @@ -4,7 +4,11 @@ import json import pytest -from collections import OrderedDict +try: + from collections.abc import OrderedDict +except ImportError: + # TODO Remove this to drop Python2 support + from collections import OrderedDict from flask_restplus import mask, Api, Resource, fields, marshal, Mask diff --git a/tests/test_marshalling.py b/tests/test_marshalling.py index 6e971267..e00b1620 100644 --- a/tests/test_marshalling.py +++ b/tests/test_marshalling.py @@ -7,7 +7,11 @@ marshal, marshal_with, marshal_with_field, fields, Api, Resource ) -from collections import OrderedDict +try: + from collections.abc import OrderedDict +except ImportError: + # TODO Remove this to drop Python2 support + from collections import OrderedDict # Add a dummy Resource to verify that the app is properly set. diff --git a/tests/test_model.py b/tests/test_model.py index 483a8c6f..677232e0 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -4,7 +4,11 @@ import copy import pytest -from collections import OrderedDict +try: + from collections.abc import OrderedDict +except ImportError: + # TODO Remove this to drop Python2 support + from collections import OrderedDict from flask_restplus import fields, Model, OrderedModel, SchemaModel