From 3a5b8bba4c376044caeb337bd7a4f65f08bbb7fa Mon Sep 17 00:00:00 2001 From: Kristian Evers Date: Thu, 13 Feb 2020 10:51:33 +0100 Subject: [PATCH 1/2] Avoid DeprecationWarning when importing OrderedDict in flask_restx/model.py and flask_restx/swagger.py logic is installed to circumvent differences in Python2 and Python3 when importing from the collections module. Some classes from collections has been moved to collections.abc in Python3. OrderedDict is not one of them, hence an ImportError is raised when trying to import OrderedDict from collections.abc. This then leads to importing both OrderedDict AND MutableMapping/Hashable from collections.abc which then raises a deprecation warning since MutableMapping/Hashable lives in collections.abc in Python3. This patch should fix those DeprecationWarnings to be raised. --- flask_restx/model.py | 5 +++-- flask_restx/swagger.py | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/flask_restx/model.py b/flask_restx/model.py index cbe1a3cf..6da9dbcd 100644 --- a/flask_restx/model.py +++ b/flask_restx/model.py @@ -5,11 +5,12 @@ import re import warnings +from collections import OrderedDict try: - from collections.abc import OrderedDict, MutableMapping + from collections.abc import MutableMapping except ImportError: # TODO Remove this to drop Python2 support - from collections import OrderedDict, MutableMapping + from collections import MutableMapping from six import iteritems, itervalues from werkzeug.utils import cached_property diff --git a/flask_restx/swagger.py b/flask_restx/swagger.py index c02feb44..c43ac818 100644 --- a/flask_restx/swagger.py +++ b/flask_restx/swagger.py @@ -5,11 +5,12 @@ import re from inspect import isclass, getdoc +from collections import OrderedDict try: - from collections.abc import OrderedDict, Hashable + from collections.abc import Hashable except ImportError: # TODO Remove this to drop Python2 support - from collections import OrderedDict, Hashable + from collections import Hashable from six import string_types, itervalues, iteritems, iterkeys from flask import current_app From 6b253c0f426589bec3d8bde6a5e337ceeae1eb53 Mon Sep 17 00:00:00 2001 From: Kristian Evers Date: Thu, 13 Feb 2020 10:51:59 +0100 Subject: [PATCH 2/2] Stop trying to import OrderedDict from collections.abc OrderedDict is a member of the collections module and not collections.abc. This goes for both Python2 and Python3. --- flask_restx/api.py | 6 +----- flask_restx/marshalling.py | 6 +----- flask_restx/mask.py | 6 +----- flask_restx/utils.py | 6 +----- tests/test_fields.py | 6 +----- tests/test_fields_mask.py | 6 +----- tests/test_marshalling.py | 6 +----- tests/test_model.py | 6 +----- 8 files changed, 8 insertions(+), 40 deletions(-) diff --git a/flask_restx/api.py b/flask_restx/api.py index d8e0d3a2..f66f3653 100644 --- a/flask_restx/api.py +++ b/flask_restx/api.py @@ -10,11 +10,7 @@ import six import sys -try: - from collections.abc import OrderedDict -except ImportError: - # TODO Remove this to drop Python2 support - from collections import OrderedDict +from collections import OrderedDict from functools import wraps, partial from types import MethodType diff --git a/flask_restx/marshalling.py b/flask_restx/marshalling.py index 899e86df..3d435d0b 100644 --- a/flask_restx/marshalling.py +++ b/flask_restx/marshalling.py @@ -1,11 +1,7 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -try: - from collections.abc import OrderedDict -except ImportError: - # TODO Remove this to drop Python2 support - from collections import OrderedDict +from collections import OrderedDict from functools import wraps from six import iteritems diff --git a/flask_restx/mask.py b/flask_restx/mask.py index ee61669d..e4c1670c 100644 --- a/flask_restx/mask.py +++ b/flask_restx/mask.py @@ -5,11 +5,7 @@ import re import six -try: - from collections.abc import OrderedDict -except ImportError: - # TODO Remove this to drop Python2 support - from collections import OrderedDict +from collections import OrderedDict from inspect import isclass from .errors import RestError diff --git a/flask_restx/utils.py b/flask_restx/utils.py index a094d2dd..9ec17721 100644 --- a/flask_restx/utils.py +++ b/flask_restx/utils.py @@ -3,11 +3,7 @@ import re -try: - from collections.abc import OrderedDict -except ImportError: - # TODO Remove this to drop Python2 support - from collections import OrderedDict +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 cb145bc4..9b7f9912 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -1,11 +1,7 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -try: - from collections.abc import OrderedDict -except ImportError: - # TODO Remove this to drop Python2 support - from collections import OrderedDict +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 74894678..fdd19cd9 100644 --- a/tests/test_fields_mask.py +++ b/tests/test_fields_mask.py @@ -4,11 +4,7 @@ import json import pytest -try: - from collections.abc import OrderedDict -except ImportError: - # TODO Remove this to drop Python2 support - from collections import OrderedDict +from collections import OrderedDict from flask_restx import mask, Api, Resource, fields, marshal, Mask diff --git a/tests/test_marshalling.py b/tests/test_marshalling.py index 8c339471..3ff8737a 100644 --- a/tests/test_marshalling.py +++ b/tests/test_marshalling.py @@ -7,11 +7,7 @@ marshal, marshal_with, marshal_with_field, fields, Api, Resource ) -try: - from collections.abc import OrderedDict -except ImportError: - # TODO Remove this to drop Python2 support - from collections import OrderedDict +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 d683bf81..52a69343 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -4,11 +4,7 @@ import copy import pytest -try: - from collections.abc import OrderedDict -except ImportError: - # TODO Remove this to drop Python2 support - from collections import OrderedDict +from collections import OrderedDict from flask_restx import fields, Model, OrderedModel, SchemaModel